Skip to content

Commit

Permalink
Remove konf.Logger in favor of slog
Browse files Browse the repository at this point in the history
  • Loading branch information
ktong committed Nov 11, 2023
1 parent a25863b commit 3e4299a
Show file tree
Hide file tree
Showing 9 changed files with 14 additions and 84 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
matrix:
run-on: [ 'ubuntu', 'macOS', 'windows' ]
go-version: [ 'oldstable', 'stable' ]
go-version: [ 'stable' ]
name: Test
runs-on: ${{ matrix.run-on }}-latest
steps:
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Removed

- Remove konf.Logger in favor of slog (#48).

## [v0.2.0] - 3/18/2023

### Removed
Expand Down
6 changes: 3 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"errors"
"fmt"
"log/slog"
"strings"
"sync"

Expand All @@ -18,7 +19,6 @@ import (
// Config is a registry which holds configuration loaded by Loader(s).
type Config struct {
delimiter string
logger Logger

values map[string]any
providers []*provider
Expand All @@ -45,7 +45,7 @@ func New(opts ...Option) (*Config, error) {
return nil, fmt.Errorf("[konf] load configuration: %w", err)
}
maps.Merge(config.values, values)
config.logger.Info(
slog.Info(
"Configuration has been loaded.",
"loader", loader,
)
Expand Down Expand Up @@ -170,7 +170,7 @@ func (c *Config) Watch(ctx context.Context, fns ...func(*Config)) error { //noli
ctx,
func(values map[string]any) {
provider.values = values
c.logger.Info(
slog.Info(
"Configuration has been changed.",
"watcher", provider.watcher,
)
Expand Down
22 changes: 0 additions & 22 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,25 +254,3 @@ type errorLoader struct{}
func (errorLoader) Load() (map[string]any, error) {
return nil, errors.New("load error")
}

func TestConfig_logger(t *testing.T) {
t.Parallel()

logger := &logger{}
_, err := konf.New(konf.WithLogger(logger), konf.WithLoader(mapLoader{}))
require.NoError(t, err)

require.Equal(t, "Configuration has been loaded.", logger.message)
require.Equal(t, []any{"loader", mapLoader{"configured": true}}, logger.keyAndValues)
}

type logger struct {
konf.Logger
message string
keyAndValues []any
}

func (l *logger) Info(message string, keyAndValues ...any) {
l.message = message
l.keyAndValues = keyAndValues
}
5 changes: 3 additions & 2 deletions global.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package konf

import (
"context"
"log/slog"
"reflect"
"sync"

Expand All @@ -21,9 +22,9 @@ func Get[T any](path string) T { //nolint:ireturn

var value T
if err := global.Unmarshal(path, &value); err != nil {
global.logger.Error(
slog.Error(
"Could not read config, return empty value instead.",
err,
"error", err,
"path", path,
"type", reflect.TypeOf(value),
)
Expand Down
4 changes: 2 additions & 2 deletions global_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func TestGet_error(t *testing.T) {
log.SetFlags(0)

require.False(t, konf.Get[bool]("config"))
expected := "Error Could not read config, return empty value instead." +
" error=[konf] decode: cannot parse '' as bool: strconv.ParseBool: parsing \"string\": invalid syntax" +
expected := "ERROR Could not read config, return empty value instead." +
" error=\"[konf] decode: cannot parse '' as bool: strconv.ParseBool: parsing \\\"string\\\": invalid syntax\"" +
" path=config type=bool\n"
require.Equal(t, expected, buf.String())
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/ktong/konf

go 1.20
go 1.21

require (
github.com/fsnotify/fsnotify v1.7.0
Expand Down
43 changes: 0 additions & 43 deletions logger.go

This file was deleted.

10 changes: 0 additions & 10 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,6 @@ func WithDelimiter(delimiter string) Option {
}
}

// WithLogger provides a Logger implementation to logger.
//
// The default implementation is using standard [log].
func WithLogger(logger Logger) Option {
return func(config *options) {
config.logger = logger
}
}

// Option configures the given Config.
type Option func(*options)

Expand All @@ -44,7 +35,6 @@ func apply(opts []Option) options {
option := &options{
Config: &Config{
delimiter: ".",
logger: stdlog{},
values: make(map[string]any),
},
}
Expand Down

0 comments on commit 3e4299a

Please sign in to comment.