zap2slog

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 25, 2025 License: MIT Imports: 7 Imported by: 1

README

zap2slog is a library that adapts zap to slog, and slog to zap.

Usage

zap to slog

Use zap2slog.NewSlogCore to create a zapcore.Core that writes to a slog.Handler.

core := zapcore.NewCore(zap2slog.NewSlogCore(slog.Default().Handler()), nil, zap.NewAtomicLevelAt(zapcore.InfoLevel))
l := zap.New(core)
l.Info("hello, world")

Zap loggers have a name, which has no equivalent in slog. Set zap2slog.SlogCoreOptions.LoggerNameKey to add an attribute to slog.Records with the logger name.

slog to zap

Use zap2slog.NewZapHandler to create a slog.Handler that writes to a zapcore.Core.

    zl, _ := zap.NewProductionConfig().Build()
    h := zap2slog.NewZapHandler(zl.Core(), nil)
    slog.New(h).Info("hello, world")

Zap loggers have a name, which has no equivalent in slog. Set zap2slog.ZapHandlerOptions.LoggerNameKey extract one of the slog.Record's attributes and use it as the zap logger name.

ZapHandler also supports AddSource and ReplaceAttr options, which behavior like slog.HandlerOptions.AddSource and slog.HandlerOptions.ReplaceAttr.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SlogCore

type SlogCore struct {
	// contains filtered or unexported fields
}

func NewSlogCore

func NewSlogCore(h slog.Handler, opts *SlogCoreOptions) *SlogCore

func (*SlogCore) Check

func (*SlogCore) Enabled

func (c *SlogCore) Enabled(l zapcore.Level) bool

func (*SlogCore) Sync

func (c *SlogCore) Sync() error

func (*SlogCore) With

func (c *SlogCore) With(fields []zapcore.Field) zapcore.Core

func (*SlogCore) Write

func (c *SlogCore) Write(e zapcore.Entry, fields []zapcore.Field) error

type SlogCoreOptions

type SlogCoreOptions struct {
	// LoggerNameKey adds an attribute to slog.Records containing the zap logger name.
	// If LoggerNameKey is empty, or the zap logger name is empty, then no attribute is added.
	LoggerNameKey string
}

type ZapHandler

type ZapHandler struct {
	// contains filtered or unexported fields
}

func NewZapHandler

func NewZapHandler(core zapcore.Core, opts *ZapHandlerOptions) *ZapHandler

func (*ZapHandler) Enabled

func (h *ZapHandler) Enabled(ctx context.Context, level slog.Level) bool

func (*ZapHandler) Handle

func (h *ZapHandler) Handle(ctx context.Context, record slog.Record) error

func (*ZapHandler) WithAttrs

func (h *ZapHandler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*ZapHandler) WithGroup

func (h *ZapHandler) WithGroup(name string) slog.Handler

type ZapHandlerOptions

type ZapHandlerOptions struct {
	// AddSource adds a source field to the zap log entry.
	AddSource bool
	// ReplaceAttr allows for customizing the attributes of the slog.Record before they are written to the zap log entry.
	// For more information. see slog.HandlerOptions.ReplaceAttr.
	ReplaceAttr func(groups []string, a slog.Attr) slog.Attr
	// LoggerNameKey will search the slog.Record for an attribute with this key.  If found, the zap
	// entry's logger name will be set to the value of that attribute, and the attribute will be elided
	// from the zap entry's fields.
	LoggerNameKey string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL