Skip to content

Commit

Permalink
use slog.Handler
Browse files Browse the repository at this point in the history
  • Loading branch information
ktong committed Feb 2, 2024
1 parent 5b2aee0 commit d8ec471
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
4 changes: 2 additions & 2 deletions default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestGet_error(t *testing.T) {

assert.True(t, !konf.Get[bool]("config"))
expected := "WARN Could not read config, return empty value instead." +
" konf.error=\"decode: cannot parse '' as bool: strconv.ParseBool: parsing \\\"string\\\": invalid syntax\"" +
" konf.path=config konf.type=bool\n"
" error=\"decode: cannot parse '' as bool: strconv.ParseBool: parsing \\\"string\\\": invalid syntax\"" +
" path=config type=bool\n"
assert.Equal(t, expected, buf.String())
}
10 changes: 6 additions & 4 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ func WithDecodeHook(decodeHook mapstructure.DecodeHookFunc) Option {
}
}

// WithLogger provides the slog.Logger for Config.
// WithLogHandler provides the slog.Handler for logs from watch.
//
// By default, it uses slog.Default().
func WithLogger(logger *slog.Logger) Option {
// By default, it uses handler from slog.Default().
func WithLogHandler(handler slog.Handler) Option {
return func(options *options) {
options.logger = logger
if handler != nil {
options.logger = slog.New(handler)
}
}
}

Expand Down
10 changes: 6 additions & 4 deletions provider/file/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ func WithUnmarshal(unmarshal func([]byte, any) error) Option {
}
}

// WithLogger provides the slog.Logger for File loader.
// WithLogHandler provides the slog.Handler for logs from watch.
//
// By default, it uses slog.Default().
func WithLogger(logger *slog.Logger) Option {
// By default, it uses handler from slog.Default().
func WithLogHandler(handler slog.Handler) Option {
return func(options *options) {
options.logger = logger
if handler != nil {
options.logger = slog.New(handler)
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions watch.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) 2024 The konf authors
// Use of this source code is governed by a MIT license found in the LICENSE file.

package konf

import (
Expand Down

0 comments on commit d8ec471

Please sign in to comment.