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: Handler options changes via WithOptions #1381

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 16 additions & 5 deletions exp/zapslog/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ func NewHandler(core zapcore.Core, opts ...Option) *Handler {
core: core,
addStackAt: slog.LevelError,
}
for _, v := range opts {
v.apply(h)
}
return h
return h.WithOptions(opts...)
}

var _ slog.Handler = (*Handler)(nil)
Expand Down Expand Up @@ -182,9 +179,23 @@ func (h *Handler) WithGroup(group string) slog.Handler {
return h.withFields(zap.Namespace(group))
}

// WithOptions returns a new Handler with the given options.
func (h *Handler) WithOptions(opts ...Option) *Handler {
cloned := h.clone()
for _, v := range opts {
v.apply(cloned)
}
return cloned
}

// withFields returns a cloned Handler with the given fields.
func (h *Handler) withFields(fields ...zapcore.Field) *Handler {
cloned := *h
cloned := h.clone()
cloned.core = h.core.With(fields)
return cloned
}

func (h *Handler) clone() *Handler {
cloned := *h
return &cloned
}