Documentation ¶
Overview ¶
Package zapslog provides an implementation of slog.Handler which writes to the supplied zapcore.Core.
Use of this package requires at least Go 1.21.
Example (Slog) ¶
//go:build go1.21 package main import ( "context" "log/slog" "net" "time" "go.uber.org/zap" "go.uber.org/zap/exp/zapslog" ) type Password string func (p Password) LogValue() slog.Value { return slog.StringValue("REDACTED") } func main() { logger := zap.NewExample(zap.IncreaseLevel(zap.InfoLevel)) defer logger.Sync() sl := slog.New(zapslog.NewHandler(logger.Core())) ctx := context.Background() sl.Info("user", "name", "Al", "secret", Password("secret")) sl.Error("oops", "err", net.ErrClosed, "status", 500) sl.LogAttrs( ctx, slog.LevelError, "oops", slog.Any("err", net.ErrClosed), slog.Int("status", 500), ) sl.Info("message", slog.Group("group", slog.Float64("pi", 3.14), slog.Duration("1min", time.Minute), ), ) sl.WithGroup("s").LogAttrs( ctx, slog.LevelWarn, "warn msg", // message slog.Uint64("u", 1), slog.Any("m", map[string]any{ "foo": "bar", })) sl.LogAttrs(ctx, slog.LevelDebug, "not show up") }
Output: {"level":"info","msg":"user","name":"Al","secret":"REDACTED"} {"level":"error","msg":"oops","err":"use of closed network connection","status":500} {"level":"error","msg":"oops","err":"use of closed network connection","status":500} {"level":"info","msg":"message","group":{"pi":3.14,"1min":"1m0s"}} {"level":"warn","msg":"warn msg","s":{"u":1,"m":{"foo":"bar"}}}
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler implements the slog.Handler by writing to a zap Core.
func NewHandler ¶
func NewHandler(core zapcore.Core, opts ...HandlerOption) *Handler
NewHandler builds a Handler that writes to the supplied zapcore.Core with options.
type HandlerOption ¶
type HandlerOption interface {
// contains filtered or unexported methods
}
A HandlerOption configures a slog Handler.
func AddStacktraceAt ¶
func AddStacktraceAt(lvl slog.Level) HandlerOption
AddStacktraceAt configures the Logger to record a stack trace for all messages at or above a given level.
func WithCaller ¶
func WithCaller(enabled bool) HandlerOption
WithCaller configures the Logger to include the filename and line number of the caller in log messages--if available.
func WithCallerSkip ¶
func WithCallerSkip(skip int) HandlerOption
WithCallerSkip increases the number of callers skipped by caller annotation (as enabled by the WithCaller option).
When building wrappers around the Logger, supplying this Option prevents Zap from always reporting the wrapper code as the caller.
func WithName ¶
func WithName(name string) HandlerOption
WithName configures the Logger to annotate each message with the logger name.