logger

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultContextMappings = []ContextKeyMapping{
	{CtxKey: public.TraceIDKey, LogKey: "trace_id"},
	{CtxKey: public.SpanIDKey, LogKey: "span_id"},
	{CtxKey: public.ParentSpanIDKey, LogKey: "parent_span_id"},
}

DefaultContextMappings are the keys extracted from the context by default. It includes common tracing keys used across the project.

View Source
var ErrNotContextLogger = errors.New("provided logger does not implement ContextLogger interface")
View Source
var ErrNotSLogger = errors.New("provided logger does not implement SLogger interface")

Functions

func GlobalSlog

func GlobalSlog(l SLogger) *slog.Logger

GlobalSlog returns a *slog.Logger that wraps the current logger, if available. If the logger is nil, it returns slog.Default().

Types

type ContextKeyMapping

type ContextKeyMapping struct {
	CtxKey any
	LogKey string
}

type ContextLogger

type ContextLogger interface {
	Logger
	ContextDebug(ctx context.Context, msg string, keysAndValues ...interface{})
	ContextInfo(ctx context.Context, msg string, keysAndValues ...interface{})
	ContextWarn(ctx context.Context, msg string, keysAndValues ...interface{})
	ContextError(ctx context.Context, msg string, keysAndValues ...interface{})

	ContextDebugf(ctx context.Context, msg string, keysAndValues ...interface{})
	ContextInfof(ctx context.Context, msg string, keysAndValues ...interface{})
	ContextWarnf(ctx context.Context, msg string, keysAndValues ...interface{})
	ContextErrorf(ctx context.Context, msg string, keysAndValues ...interface{})

	WithContextKeys(keys ...ContextKeyMapping) ContextLogger
}

func MustWrapContextLogger

func MustWrapContextLogger(lg Logger) ContextLogger

func NewZapContextLogger

func NewZapContextLogger(opts ...Option) (ContextLogger, error)

func WrapContextLogger

func WrapContextLogger(lg Logger) (ContextLogger, error)

type FullLogger

type FullLogger interface {
	Logger
	ContextLogger
	SLogger
}

type Logger

type Logger interface {
	Debug(msg string, keysAndValues ...interface{})
	Info(msg string, keysAndValues ...interface{})
	Warn(msg string, keysAndValues ...interface{})
	Error(msg string, keysAndValues ...interface{})
	Fatal(msg string, keysAndValues ...interface{})

	Debugf(msg string, keysAndValues ...interface{})
	Infof(msg string, keysAndValues ...interface{})
	Warnf(msg string, keysAndValues ...interface{})
	Errorf(msg string, keysAndValues ...interface{})
	Fatalf(msg string, keysAndValues ...interface{})

	With(keysAndValues ...interface{}) Logger
	Sync() error
}

Logger defines the abstract logging interface for the application. It supports structured logging via key-value pairs and allows context accumulation.

func NewZapLogger

func NewZapLogger(opts ...Option) (Logger, error)

NewZapLogger creates a new Logger using zap with the provided options.

type Option

type Option func(*options)

Option is a function that configures the zap logger.

func WithFileRotator

func WithFileRotator(filename string, maxSize, maxBackups, maxAge int, compress bool) Option

WithFileRotator configures the log file rotation settings. filename: path to the log file. maxSize: max size in megabytes before rotation. maxBackups: max number of old log files to keep. maxAge: max number of days to retain old log files. compress: whether to compress old log files (gzip).

func WithFormat

func WithFormat(format string) Option

WithFormat sets the encoding format ("json" or "console").

func WithLevel

func WithLevel(level string) Option

WithLevel sets the logging level (debug, info, warn, error, dpanic, panic, fatal).

func WithStdout

func WithStdout(enable bool) Option

WithStdout determines if logs should also be written to standard output. This is useful when writing to a file but also wanting to see logs in the console.

type SLogger

type SLogger interface {
	Logger
	// Slog returns a *slog.Logger representation of the logger.
	Slog() *slog.Logger
	// WithSlogContextKeys returns a new logger that extracts the provided keys from context in Slog.
	WithSlogContextKeys(keys ...ContextKeyMapping) SLogger
}

SLogger defines the abstract context logging interface which use slog to handle context

func MustWrapSLogger

func MustWrapSLogger(lg Logger) SLogger

func NewZapSLogger

func NewZapSLogger(opts ...Option) (SLogger, error)

func WrapSLogger

func WrapSLogger(lg Logger) (SLogger, error)

Jump to

Keyboard shortcuts

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