Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zapslog: Drop HandlerOptions.New, use NewHandler #1315

Merged
merged 1 commit into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion exp/zapslog/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func Example_slog() {
logger := zap.NewExample(zap.IncreaseLevel(zap.InfoLevel))
defer logger.Sync()

sl := slog.New(zapslog.NewHandler(logger.Core()))
sl := slog.New(zapslog.NewHandler(logger.Core(), nil /* options */))
ctx := context.Background()

sl.Info("user", "name", "Al", "secret", Password("secret"))
Expand Down
20 changes: 9 additions & 11 deletions exp/zapslog/slog.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,19 @@ type HandlerOptions struct {
AddSource bool
}

// New builds a [Handler] that writes to the supplied [zapcore.Core].
// This handler may be supplied to [slog.New] to create a new [slog.Logger].
func (opts HandlerOptions) New(core zapcore.Core) *Handler {
// NewHandler builds a [Handler] that writes to the supplied [zapcore.Core]
// with the default options.
func NewHandler(core zapcore.Core, opts *HandlerOptions) *Handler {
if opts == nil {
opts = &HandlerOptions{}
}
return &Handler{
core: core,
name: opts.LoggerName,
addSource: opts.AddSource,
}
}

// NewHandler builds a [Handler] that writes to the supplied [zapcore.Core]
// with the default options.
func NewHandler(core zapcore.Core) *Handler {
var opts HandlerOptions
return opts.New(core)
}

var _ slog.Handler = (*Handler)(nil)

// groupObject holds all the Attrs saved in a slog.GroupValue.
Expand Down Expand Up @@ -100,7 +96,9 @@ func convertAttrToField(attr slog.Attr) zapcore.Field {
case slog.KindLogValuer:
return convertAttrToField(slog.Attr{
Key: attr.Key,
// TODO: resolve the value in a lazy way
// TODO: resolve the value in a lazy way.
// This probably needs a new Zap field type
// that can be resolved lazily.
Value: attr.Value.Resolve(),
})
default:
Expand Down
6 changes: 3 additions & 3 deletions exp/zapslog/slog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ import (
func TestAddSource(t *testing.T) {
r := require.New(t)
fac, logs := observer.New(zapcore.DebugLevel)
sl := slog.New(HandlerOptions{
sl := slog.New(NewHandler(fac, &HandlerOptions{
AddSource: true,
}.New(fac))
}))
sl.Info("msg")

r.Len(logs.AllUntimed(), 1, "Expected exactly one entry to be logged")
entry := logs.AllUntimed()[0]
r.Equal("msg", entry.Entry.Message, "Unexpected message")
r.Equal("msg", entry.Message, "Unexpected message")
r.Regexp(
`/slog_test.go:\d+$`,
entry.Caller.String(),
Expand Down