Skip to content

Commit

Permalink
Remove konf.Logger in favor of slog (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
ktong authored Nov 11, 2023
1 parent a25863b commit f0cdfba
Show file tree
Hide file tree
Showing 13 changed files with 23 additions and 109 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
4 changes: 2 additions & 2 deletions provider/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package file
import (
"fmt"
"io/fs"
"log"
"log/slog"
"os"
)

Expand Down Expand Up @@ -47,7 +47,7 @@ func (f File) Load() (map[string]any, error) {
}
if err != nil {
if f.ignoreNotExist && os.IsNotExist(err) {
log.Printf("Config file %s does not exist.", f.path)
slog.Warn("Config file does not exist.", "file", f.path)

return make(map[string]any), nil
}
Expand Down
16 changes: 0 additions & 16 deletions provider/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
package file_test

import (
"bytes"
"context"
"errors"
"io/fs"
"log"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -122,20 +120,6 @@ func TestFile_Load(t *testing.T) {
}
}

func TestFile_log(t *testing.T) {
buf := new(bytes.Buffer)
log.SetOutput(buf)
log.SetFlags(0)

_, err := file.New(
"not_found.json",
file.IgnoreFileNotExit(),
).Load()

require.NoError(t, err)
require.Equal(t, "Config file not_found.json does not exist.\n", buf.String())
}

func TestFile_Watch(t *testing.T) {
testcases := []struct {
description string
Expand Down
10 changes: 5 additions & 5 deletions provider/file/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package file
import (
"context"
"fmt"
"log"
"log/slog"
"path/filepath"
"time"

Expand All @@ -26,7 +26,7 @@ func (f File) Watch(ctx context.Context, watchFunc func(map[string]any)) error {
}
defer func() {
if err := watcher.Close(); err != nil {
log.Printf("Error when closing watcher for %s: %v", f.path, err)
slog.Error("Error when closing file watcher.", "file", f.path, "error", err)
}
}()

Expand Down Expand Up @@ -73,12 +73,12 @@ func (f File) Watch(ctx context.Context, watchFunc func(map[string]any)) error {

switch {
case event.Has(fsnotify.Remove):
log.Printf("Config file %s has been removed.", f.path)
slog.Warn("Config file has been removed.", "file", f.path)
watchFunc(nil)
case event.Has(fsnotify.Create) || event.Has(fsnotify.Write):
values, err := f.Load()
if err != nil {
log.Printf("Error when reloading configuration from %s: %v", f.path, err)
slog.Error("Error when reloading config file", "file", f.path, "error", err)

continue
}
Expand All @@ -90,7 +90,7 @@ func (f File) Watch(ctx context.Context, watchFunc func(map[string]any)) error {
return nil
}

log.Printf("Error when watching file %s: %v", f.path, err)
slog.Error("Error when watching file", "file", f.path, "error", err)

case <-ctx.Done():
return nil
Expand Down
4 changes: 2 additions & 2 deletions provider/file/watch_unsupported.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ package file

import (
"context"
"log"
"log/slog"
"runtime"
)

func (f File) Watch(context.Context, func(map[string]any)) error {
log.Printf("File.Watch does not supported on %s.", runtime.GOOS)
slog.Warn("File.Watch does not supported on runtime.", "runtime", runtime.GOOS)

return nil
}

0 comments on commit f0cdfba

Please sign in to comment.