Documentation
¶
Overview ¶
Package logstuff provides a few minimal logging abstractions
- LogSink, minimal log interface for logging swapability.
- SlogCtx, slog wrapper that adds ctx key propagation and trace level.
- slogSink, the LogSink adapter for SlogCtx.
- Logger, adds logging cenvenience methods to a LogSink.
- CtxKeyer, LevelEnbaler and more.
Index ¶
- Variables
- type CtxKey
- type CtxKeyer
- type LevelEnabler
- type LogLevel
- type LogSink
- type Logger
- func (l *Logger) Debug(msg string, args ...any)
- func (l *Logger) DebugContext(ctx context.Context, msg string, args ...any)
- func (l *Logger) Enabled(ctx context.Context, lvl LogLevel) bool
- func (l *Logger) Error(msg string, args ...any)
- func (l *Logger) ErrorContext(ctx context.Context, msg string, args ...any)
- func (l *Logger) Info(msg string, args ...any)
- func (l *Logger) InfoContext(ctx context.Context, msg string, args ...any)
- func (l *Logger) Log(ctx context.Context, lvl LogLevel, msg string, args ...any)
- func (l *Logger) Trace(msg string, args ...any)
- func (l *Logger) TraceContext(ctx context.Context, msg string, args ...any)
- func (l *Logger) Warn(msg string, args ...any)
- func (l *Logger) WarnContext(ctx context.Context, msg string, args ...any)
- func (l *Logger) With(args ...any) *Logger
- type SlogCtx
- type SlogHandler
Constants ¶
This section is empty.
Variables ¶
var EnvLogLevel = "LOGLEVEL"
EnvLogLevel defines the env var to use when setting log level, default "LOGLEVEL"
Functions ¶
This section is empty.
Types ¶
type CtxKeyer ¶
CtxKeyer is a minimal interface that helps fork a logger and add ctx key capture to it.
type LevelEnabler ¶
LevelEnabler is a minimal interface to gate logging behind a conditional.
type LogLevel ¶
type LogLevel int
func LevelFromEnv ¶
func LevelFromEnv() LogLevel
LevelFromEnv reads log level form env. Reads exported var EnvLogLevel value, default "LOGLEVEL"
type LogSink ¶
LogSink is a minimal interface that helps logger swapping.
func NewDiscard ¶
func NewDiscard() LogSink
NewDiscard returns discardSink, a logger that does nothing and satisfies LogSink interface.
func NewSlogSink ¶
func NewSlogSink(l LogLevel, opts ...SlogHandler) LogSink
NewSlogSink returns a wrapper around SlogCtx that satisfies LogSink interface.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger is a LogSink wrapper that provides all expected convenience methods.
func (*Logger) DebugContext ¶
func (*Logger) ErrorContext ¶
func (*Logger) InfoContext ¶
func (*Logger) TraceContext ¶
func (*Logger) WarnContext ¶
type SlogCtx ¶
type SlogCtx struct {
// contains filtered or unexported fields
}
SlogCtx is a wrapper around slog.Logger that adds support for context keys.
func NewSlogCtx ¶
func NewSlogCtx(l LogLevel, opts ...SlogHandler) *SlogCtx
NewSlogCtx returns a *SlogCtx. variadic SlogHandler allows to pass 0, 1 or more build options.
- If no SlogHandler is passed, the default TEXT slog.Handler that outputs to stdout will be used.
- If more than one is passed, then a slog.Multihandler will be created.
func (*SlogCtx) WithCtxKeys ¶
type SlogHandler ¶
type SlogHandler func(*slog.HandlerOptions) slog.Handler
SlogHandler builds a slog.Handler from the given options.
func WithHandler ¶
func WithHandler(h slog.Handler) SlogHandler
WithHandler allows to passing directly a pre-built slog.Handler. NOTE: Handlers passed in WithHandler won't do ReplaceAttr (trace level).
func WithJSONHandler ¶
func WithJSONHandler(w io.Writer, logLevel ...LogLevel) SlogHandler
WithJSONHandler creates a JSON slog.Handler. If logLevel is passed it overwrites the base logLevel of constructor.
func WithTextHandler ¶
func WithTextHandler(w io.Writer, logLevel ...LogLevel) SlogHandler
WithTextHandler creates a TEXT slog.Handler. If logLevel is passed it overwrites the base logLevel of constructor.