slogutil

package
v0.8.0 Latest Latest
Warning

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

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

Documentation

Overview

Package slogutil provides configuration and setup utilities for slog.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidFormat = errors.New("invalid log format")

ErrInvalidFormat is returned when Format contains an unrecognized value.

View Source
var ErrInvalidLevel = errors.New("invalid log level")

ErrInvalidLevel is returned when Level contains an unrecognized value.

Functions

func Setup

func Setup(cfg Config) error

Setup configures the global slog logger based on cfg. It sets slog.SetDefault() with the configured handler writing to os.Stderr, wrapped in a ContextHandler. Returns error if Level or Format contains invalid values.

Types

type Config

type Config struct {
	// Level is the minimum log level.
	// Valid values: "debug", "info", "warn", "warning", "error".
	// Default: "info"
	Level string `koanf:"level"`

	// Format is the output format.
	// Valid values: "text", "json".
	// Default: "text"
	Format string `koanf:"format"`
}

Config holds configuration for slog setup.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a Config with sensible defaults.

type ContextHandler added in v0.8.0

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

ContextHandler is a slog.Handler that adds request_id, tenant_id, user_id, correlation_id, trace_id and span_id from the record's context before passing the record to the wrapped handler. Fields whose value is empty or missing are omitted. trace_id and span_id come from the active OpenTelemetry span context.

Fields are added to the record, so they are nested under any group opened with WithGroup.

func NewContextHandler added in v0.8.0

func NewContextHandler(next slog.Handler) *ContextHandler

NewContextHandler returns a ContextHandler that wraps next.

func (*ContextHandler) Enabled added in v0.8.0

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

Enabled reports whether the wrapped handler handles records at level.

func (*ContextHandler) Handle added in v0.8.0

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

Handle adds the context fields to r and passes it to the wrapped handler.

func (*ContextHandler) WithAttrs added in v0.8.0

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

WithAttrs returns a ContextHandler whose wrapped handler has attrs added.

func (*ContextHandler) WithGroup added in v0.8.0

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

WithGroup returns a ContextHandler whose wrapped handler opens group name.

Jump to

Keyboard shortcuts

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