slogconf

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	EnvVarNameLevel     = "LOG_LEVEL"
	EnvVarNameFormat    = "LOG_FORMAT"
	EnvVarNameAddSource = "LOG_ADD_SOURCE"
)

The environment variables read when Options does not name others.

Variables

View Source
var (
	ErrInvalidLogLevel     = errors.New("invalid log level")
	ErrInvalidLogFormat    = errors.New("invalid log format")
	ErrInvalidLogAddSource = errors.New("invalid log add-source value")
)

Functions

func AddHandler

func AddHandler(handler slog.Handler) bool

AddHandler stacks an extra handler onto the default FanOutHandler, keeping the existing ones. Use it to tee logs somewhere additional — a ring buffer, a shipper, a test capture — without replacing what init configured.

Call it EARLY. slog.Logger.With and WithGroup snapshot the handler chain at the moment they are called, so a logger derived BEFORE the Add never carries the new handler and silently drops those records. Handlers added before any derivation reach everything.

Reports whether the default logger was a FanOutHandler, i.e. whether this package configured it. False means something else replaced slog's default and the handler was stacked onto that instead — still added, but the caller has lost the stdout/stderr split this package sets up, which is worth noticing rather than discovering through absent logs.

func Init

func Init(opts Options) error

Init reconfigures the default logger, reading the variables named by opts.

The blank import already configures logging with the default names, so this is only needed to change them or their fallbacks:

slogconf.Init(slogconf.Options{
    LevelEnvVar:  "MYAPP_LOG_LEVEL",
    FormatEnvVar: "MYAPP_LOG_FORMAT",
})

Call it as early in main as possible. It replaces slog's default logger, so any logger already derived through slog.Logger.With keeps pointing at the previous handler chain — the same ordering caveat AddHandler carries.

func SetHandlers

func SetHandlers(handlers ...slog.Handler)

SetHandlers replaces all handlers in the default FanOutHandler with the provided ones.

Types

type FanOutHandler

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

FanOutHandler dispatches log records to multiple handlers. The default slog handler is always a FanOutHandler. Use SetHandlers to replace all handlers and AddHandler to stack extra ones on top.

func NewFanOutHandler

func NewFanOutHandler(handlers ...slog.Handler) *FanOutHandler

NewFanOutHandler creates a handler that dispatches to all provided handlers.

func (*FanOutHandler) Enabled

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

Enabled reports whether any of the underlying handlers handle records at the given level.

func (*FanOutHandler) Handle

func (h *FanOutHandler) Handle(ctx context.Context, r slog.Record) error

Handle dispatches the record to every underlying handler, and keeps going when one of them fails.

Returning at the first error would let a single broken sink silence all the ones after it: slog discards whatever Handle returns, so a Loki handler that cannot reach its server would take stdout down with it and nothing would say so. Every handler sees the record regardless of what the earlier ones did, and the failures are joined so none is lost.

func (*FanOutHandler) Len

func (h *FanOutHandler) Len() int

Len reports how many handlers the fan-out dispatches to. Exported so callers can assert that AddHandler stacked onto the existing set rather than replacing it — the difference is invisible from the outside until logs go missing.

func (*FanOutHandler) WithAttrs

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

WithAttrs returns a new FanOutHandler with the given attributes added to all handlers.

func (*FanOutHandler) WithGroup

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

WithGroup returns a new FanOutHandler with the given group name applied to all handlers.

type MultiWriterHandler

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

MultiWriterHandler routes log records to different writers based on level. Error and Warn levels go to stderr, Info and Debug go to stdout.

func NewMultiWriterHandler

func NewMultiWriterHandler(f format, opts *slog.HandlerOptions, stdout, stderr io.Writer) (*MultiWriterHandler, error)

NewMultiWriterHandler creates a handler that routes logs to stdout/stderr based on level.

func (*MultiWriterHandler) Enabled

func (h *MultiWriterHandler) Enabled(_ context.Context, level slog.Level) bool

Enabled reports whether the handler handles records at the given level.

func (*MultiWriterHandler) Handle

func (h *MultiWriterHandler) Handle(ctx context.Context, r slog.Record) error

Handle handles the Record by routing it to the appropriate writer.

func (*MultiWriterHandler) WithAttrs

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

WithAttrs returns a new Handler with the given attributes added.

func (*MultiWriterHandler) WithGroup

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

WithGroup returns a new Handler with the given group name.

type Options

type Options struct {
	// LevelEnvVar overrides the variable read for the log level.
	LevelEnvVar string

	// FormatEnvVar overrides the variable read for the output format.
	FormatEnvVar string

	// AddSourceEnvVar overrides the variable read for source reporting.
	AddSourceEnvVar string

	// DefaultLevel applies when the level variable is unset or empty.
	// Empty means "info".
	DefaultLevel string

	// DefaultFormat applies when the format variable is unset or empty.
	// Empty means "text".
	DefaultFormat string

	// DefaultAddSource applies when the source variable is unset or empty.
	DefaultAddSource bool
}

Options selects which environment variables configure logging and what they fall back to. The zero value reproduces this package's historical behaviour exactly — LOG_LEVEL / LOG_FORMAT / LOG_ADD_SOURCE, defaulting to info + text + no source — so the blank import is unaffected by this type existing.

The names are FIELDS rather than struct tags because a struct tag is fixed at compile time: nothing can hand one a value at runtime. That is why a caller who needed different variable names previously had to copy this whole package rather than configure it.

Jump to

Keyboard shortcuts

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