logging

package
v0.2.33 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

Level controls the level of the default logger. It exists to allow modifications to the level after the default logger is configured in NewAsDefault.

Functions

func New

func New(level slog.Leveler, options ...Option) *slog.Logger

New creates a new logger with the given level using a JSON and OpenTelemetry handler.

func NewAsDefault

func NewAsDefault(level, legacyLevel slog.Level, options ...Option) *slog.Logger

NewAsDefault updates the global level with the given value and calls New with the new value.

The level used by the "log" package is set with the given legacy level. If the global log level is less than or equal to than the legacy level, the logs emitted by log.Println, etc. will be output. Otherwise they will be dropped.

func NewAsDefaultFromConfig

func NewAsDefaultFromConfig(config Config, options ...Option) *slog.Logger

NewAsDefaultFromConfig calls NewAsDefault with the given configuration.

func NewFromConfig

func NewFromConfig(config Config, options ...Option) *slog.Logger

NewFromConfig calls New with the given configuration.

func Wrap added in v0.0.35

func Wrap(inner slog.Handler, extractors []Extractor) slog.Handler

Wrap wraps a slog handler with support for augmenting a log record with metadata from the context.

Types

type Config

type Config struct {
	// Level sets the level for the logger.
	Level slog.Level `koanf:"level" default:"info"`

	// LegacyLevel sets the level for logs from the "log" package. This is only applied if using
	// NewAsDefaultFromConfig.
	LegacyLevel slog.Level `koanf:"level" default:"debug"`
}

Config contains the configuration for the logger.

type Extractor added in v0.0.35

type Extractor interface {
	Extract(context.Context) []slog.Attr
}

Extractor implementations are used to add attributes to a log record based on context metadata. One or more extractors can be added to the logger using the WithExtractors option.

type Option added in v0.0.35

type Option interface {
	// contains filtered or unexported methods
}

Option implementations are used to apply optional configuration to a logger.

func WithExtractors added in v0.0.35

func WithExtractors(extractors ...Extractor) Option

WithExtractors adds context extractors to the logger.

func WithSuppressSource added in v0.0.35

func WithSuppressSource(suppress bool) Option

WithSuppressSource suppresses the source field of a log record. This is intended for testing where a deterministic log record is required.

func WithSuppressTime added in v0.0.35

func WithSuppressTime(suppress bool) Option

WithSuppressTime suppresses the time field of a log record. This is intended for testing where a deterministic log record is required.

func WithWriter added in v0.0.35

func WithWriter(w io.Writer) Option

WithWriter sets the output of a JSON logger. Defaults to os.Stdout.

Jump to

Keyboard shortcuts

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