log

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(msg string, attrs ...Attr)

func DebugContext added in v0.0.3

func DebugContext(ctx context.Context, msg string, attrs ...Attr)

func Debugf

func Debugf(format string, args ...any)

func Error

func Error(msg string, attrs ...Attr)

func ErrorContext added in v0.0.3

func ErrorContext(ctx context.Context, msg string, attrs ...Attr)

func Errorf

func Errorf(format string, args ...any)

func Info

func Info(msg string, attrs ...Attr)

func InfoContext added in v0.0.3

func InfoContext(ctx context.Context, msg string, attrs ...Attr)

func Infof

func Infof(format string, args ...any)

func InitWithBackends added in v0.0.3

func InitWithBackends(backends ...Backend)

InitWithBackends initializes global logger with custom backend(s).

func Sync

func Sync() error

func Warn

func Warn(msg string, attrs ...Attr)

func WarnContext added in v0.0.3

func WarnContext(ctx context.Context, msg string, attrs ...Attr)

func Warnf

func Warnf(format string, args ...any)

Types

type AddCallerStatus

type AddCallerStatus int

AddCallerStatus represents the state of caller information in log entries.

const (
	// AddCallerStatusKeep maintains the current caller setting without changes.
	AddCallerStatusKeep AddCallerStatus = iota
	// AddCallerStatusEnable adds caller information to log entries.
	AddCallerStatusEnable
	// AddCallerStatusDisable removes caller information from log entries.
	AddCallerStatusDisable
)

type Attr added in v0.0.3

type Attr = slog.Attr

Attr is a strongly-typed structured log field based on slog's value model.

func Any

func Any(key string, value any) Attr

Any creates a structured field with an arbitrary value.

func Bool

func Bool(key string, value bool) Attr

Bool creates a structured field with a boolean value.

func Duration

func Duration(key string, value time.Duration) Attr

Duration creates a structured field with a duration value.

func Err

func Err(err error) Attr

Err creates a structured field for error values using the standard "error" key.

func Float64

func Float64(key string, value float64) Attr

Float64 creates a structured field with a floating-point value.

func Group

func Group(key string, attrs ...Attr) Attr

Group creates a structured field that groups multiple attrs under a single key.

func Int

func Int(key string, value int) Attr

Int creates a structured field with an integer value.

func Int64

func Int64(key string, value int64) Attr

Int64 creates a structured field with a 64-bit integer value.

func MergeAttrs added in v0.0.3

func MergeAttrs(base []Attr, explicit []Attr) []Attr

MergeAttrs merges base attrs with explicit attrs. Explicit attrs override same-key values from base.

func String

func String(key, value string) Attr

String creates a structured field with a string value.

func Time

func Time(key string, value time.Time) Attr

Time creates a structured field with a time value.

func Uint64

func Uint64(key string, value uint64) Attr

Uint64 creates a structured field with an unsigned 64-bit integer value.

type Backend added in v0.0.3

type Backend interface {
	Log(ctx context.Context, level Level, msg string, attrs ...Attr)
	Sync() error
	With(options ...Option) Backend
}

Backend is the pluggable logging backend interface.

func NewMultiBackend added in v0.0.3

func NewMultiBackend(backends ...Backend) Backend

NewMultiBackend creates a backend that writes to all provided backends.

func NewNopBackend added in v0.0.3

func NewNopBackend() Backend

func WrapBackendWithContextMapMerge added in v0.0.3

func WrapBackendWithContextMapMerge(backend Backend, extractor ContextMapExtractor) Backend

WrapBackendWithContextMapMerge returns a backend wrapper using a map-based context extractor.

func WrapBackendWithContextMerge added in v0.0.3

func WrapBackendWithContextMerge(backend Backend, extractor ContextAttrExtractor) Backend

WrapBackendWithContextMerge returns a backend that merges attrs extracted from context.

type BaseLogger

type BaseLogger interface {
	Debug(msg string, attrs ...Attr)
	Info(msg string, attrs ...Attr)
	Warn(msg string, attrs ...Attr)
	Error(msg string, attrs ...Attr)
}

BaseLogger defines structured logging methods.

type ContextAttrExtractor added in v0.0.3

type ContextAttrExtractor func(ctx context.Context) []Attr

ContextAttrExtractor extracts attributes from context for context-aware logging.

func MapContextAttrExtractor added in v0.0.3

func MapContextAttrExtractor(extractor ContextMapExtractor) ContextAttrExtractor

MapContextAttrExtractor adapts map extractors into attr extractors.

type ContextLogger added in v0.0.3

type ContextLogger interface {
	DebugContext(ctx context.Context, msg string, attrs ...Attr)
	InfoContext(ctx context.Context, msg string, attrs ...Attr)
	WarnContext(ctx context.Context, msg string, attrs ...Attr)
	ErrorContext(ctx context.Context, msg string, attrs ...Attr)
}

ContextLogger defines context-aware structured logging methods.

type ContextMapExtractor added in v0.0.3

type ContextMapExtractor func(ctx context.Context) map[string]any

ContextMapExtractor extracts key-value pairs from context for logging.

type Field

type Field = Attr

Field is kept as an alias for backward compatibility.

type FormatLogger added in v0.0.3

type FormatLogger interface {
	Debugf(format string, args ...any)
	Infof(format string, args ...any)
	Warnf(format string, args ...any)
	Errorf(format string, args ...any)
}

FormatLogger defines printf-style logging methods.

type Level added in v0.0.3

type Level int8

Level is a backend-agnostic log level.

const (
	LevelDebug Level = iota
	LevelInfo
	LevelWarn
	LevelError
)

type Logger

type Logger interface {
	BaseLogger
	ContextLogger
	FormatLogger

	Backend() Backend
	With(options ...Option) Logger
	Sync() error
}

Logger combines quick-use APIs with type-safe attrs and context support.

func With

func With(options ...Option) Logger

type MultiBackend added in v0.0.3

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

MultiBackend fan-outs each log entry to all configured backends.

func (*MultiBackend) Backends added in v0.0.3

func (m *MultiBackend) Backends() []Backend

Backends returns a snapshot of child backends.

func (*MultiBackend) Log added in v0.0.3

func (m *MultiBackend) Log(ctx context.Context, level Level, msg string, attrs ...Attr)

func (*MultiBackend) Sync added in v0.0.3

func (m *MultiBackend) Sync() error

func (*MultiBackend) With added in v0.0.3

func (m *MultiBackend) With(options ...Option) Backend

type Option

type Option = func(*Options)

Option is a function type for configuring logger options.

func AddCaller

func AddCaller() Option

AddCaller enables caller information in log entries. This includes file names and line numbers where the log call was made.

func DisableCaller

func DisableCaller() Option

DisableCaller removes caller information from log entries. This can improve performance when caller information is not needed.

func WithAttrs

func WithAttrs(attrs map[string]any) Option

WithAttrs adds structured attributes to all log messages from this logger. These attributes provide consistent context across all log entries.

func WithName

func WithName(name string) Option

WithName sets the logger name for identification purposes. The name appears in log output to help distinguish between different loggers.

func WithStackAt

func WithStackAt(level Level) Option

WithStackAt enables stack trace logging at the specified level and above. Stack traces help debug issues by showing the full call chain.

type Options added in v0.0.3

type Options struct {
	Name       string
	AddCaller  AddCallerStatus
	AddStackAt *Level
	Attrs      map[string]any
}

Options is the materialized option set consumed by backend adapters.

func NewOptions added in v0.0.3

func NewOptions(opts ...Option) Options

NewOptions materializes options so backend adapters can consume them.

type StdioBackend added in v0.0.3

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

func NewStdioBackend added in v0.0.3

func NewStdioBackend(options ...Option) *StdioBackend

func (*StdioBackend) Log added in v0.0.3

func (b *StdioBackend) Log(_ context.Context, level Level, msg string, attrs ...Attr)

func (*StdioBackend) Sync added in v0.0.3

func (b *StdioBackend) Sync() error

func (*StdioBackend) With added in v0.0.3

func (b *StdioBackend) With(options ...Option) Backend

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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