Skip to content

Commit

Permalink
zapslog: Drop HandlerOptions.New, use NewHandler (#1315)
Browse files Browse the repository at this point in the history
The pattern of constructors for zapslog.Handler was previously:

    type HandlerOptions struct{ ... }

    func (*HandlerOptions) New(zapcore.Core) *Handler
    func NewHandler(zapcore.Core) *Handler

This was modeled after similar constructors in slog for JSON and Text
handlers.

slog has since dropped those constructors in favor of simple:

    func NewJSONHandler(io.Writer, *HandlerOptions) *JSONHandler
    func NewTextHandler(io.Writer, *HandlerOptions) *TextHandler

This change similarly drops the options.New method and default
no-argument constructor in favor of:

    func NewHandler(zapcore.Core, *HandlerOptions) *Handler

As with slog's JSON or Text handlers, the first argument is the
destination: an io.Writer for JSON and Text, a Zap core for us.

Refs golang/go#59339
  • Loading branch information
abhinav committed Aug 3, 2023
1 parent 7d42ce9 commit 9c3c581
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
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

0 comments on commit 9c3c581

Please sign in to comment.