log

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewSlogLogger

func NewSlogLogger() log.Logger

NewSlogLogger builds a SlogLogger backed by the stdlib slog with sensible defaults: a text handler writing to stderr at INFO level.

Callers needing a different format / level / destination should either:

  • build their own *slog.Logger and wrap it: SlogLogger{L: myLogger}
  • or implement the [Logger] interface themselves and pass it to [SetLogger].

Types

type LevelFilter

type LevelFilter struct {
	// Logger is the underlying logger that receives forwarded messages.
	Logger log.Logger
	// Level is the minimum severity; messages below this level are discarded.
	Level log.Level
}

LevelFilter wraps a [Logger] and discards messages whose level is below the configured LevelFilter.Level. This allows callers to control verbosity at the call site while keeping the underlying logger unchanged — a natural fit for the composable (Lego-like) design philosophy of go-wind.

Usage:

l := log.LevelFilter{Logger: log.NewSlogLogger(), Level: log.LevelWarn}
l.Debug(ctx, "hidden")   // discarded
l.Error(ctx, "visible")  // forwarded

func (LevelFilter) Debug

func (f LevelFilter) Debug(ctx context.Context, msg string, args ...any)

Debug forwards to the underlying logger only when the filter threshold permits DEBUG level.

func (LevelFilter) Enabled

func (f LevelFilter) Enabled(level log.Level) bool

Enabled reports whether the underlying logger would emit at the given level AND that level is at or above the filter threshold.

func (LevelFilter) Error

func (f LevelFilter) Error(ctx context.Context, msg string, args ...any)

Error forwards to the underlying logger only when the filter threshold permits ERROR level.

func (LevelFilter) Info

func (f LevelFilter) Info(ctx context.Context, msg string, args ...any)

Info forwards to the underlying logger only when the filter threshold permits INFO level.

func (LevelFilter) Warn

func (f LevelFilter) Warn(ctx context.Context, msg string, args ...any)

Warn forwards to the underlying logger only when the filter threshold permits WARN level.

func (LevelFilter) With

func (f LevelFilter) With(args ...any) log.Logger

With returns a new LevelFilter wrapping the underlying logger's With result, preserving the current Level threshold.

type MultiLogger

type MultiLogger struct {
	// Loggers is the slice of loggers that receive fanned-out records.
	Loggers []log.Logger
}

MultiLogger fans out log records to multiple [Logger] instances in order. If any logger panics, the remaining loggers still receive the record via a deferred recover.

This is useful when you need to send logs to both stderr (for local debugging) and a remote collection service simultaneously:

ml := log.MultiLogger{Loggers: []log.Logger{
    log.NewSlogLogger(),
    myRemoteLogger,
}}
log.SetLogger(ml)

func (MultiLogger) Debug

func (m MultiLogger) Debug(ctx context.Context, msg string, args ...any)

func (MultiLogger) Enabled

func (m MultiLogger) Enabled(level log.Level) bool

Enabled reports whether ANY of the underlying loggers would emit at the given level.

func (MultiLogger) Error

func (m MultiLogger) Error(ctx context.Context, msg string, args ...any)

func (MultiLogger) Info

func (m MultiLogger) Info(ctx context.Context, msg string, args ...any)

func (MultiLogger) Warn

func (m MultiLogger) Warn(ctx context.Context, msg string, args ...any)

func (MultiLogger) With

func (m MultiLogger) With(args ...any) log.Logger

With returns a new MultiLogger whose underlying loggers have the given key-value pairs attached.

type SlogLogger

type SlogLogger struct {
	// L is the underlying slog logger. It MUST be non-nil; [NewSlogLogger]
	// always returns a ready-to-use instance.
	L *slog.Logger
}

SlogLogger adapts the stdlib *slog.Logger to the [Logger] interface.

This is the reference implementation returned by NewSlogLogger. Callers that already own a configured *slog.Logger can wrap it directly:

log.SetLogger(log.SlogLogger{L: mySlogLogger})

func (SlogLogger) Debug

func (s SlogLogger) Debug(ctx context.Context, msg string, args ...any)

Debug forwards to slog.Logger.DebugContext.

func (SlogLogger) Enabled

func (s SlogLogger) Enabled(level log.Level) bool

Enabled maps the wind [Level] to the equivalent slog level and reports whether the underlying *slog.Logger would emit a record at that level.

func (SlogLogger) Error

func (s SlogLogger) Error(ctx context.Context, msg string, args ...any)

Error forwards to slog.Logger.ErrorContext.

func (SlogLogger) Info

func (s SlogLogger) Info(ctx context.Context, msg string, args ...any)

Info forwards to slog.Logger.InfoContext.

func (SlogLogger) Warn

func (s SlogLogger) Warn(ctx context.Context, msg string, args ...any)

Warn forwards to slog.Logger.WarnContext.

func (SlogLogger) With

func (s SlogLogger) With(args ...any) log.Logger

With returns a new SlogLogger whose underlying *slog.Logger has the given key-value pairs attached. This is typically used to distinguish modules, e.g., logger.With("module", "registry"). The returned logger will include these attributes in every log record it produces.

Directories

Path Synopsis
aliyun module
charm module
cloudwatch module
fluent module
glog module
hclog module
logrus module
loki module
phuslu module
sentry module
tencent module
zap module
zerolog module

Jump to

Keyboard shortcuts

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