Documentation
¶
Overview ¶
Package log is a small, opinionated slog extension. Composable handlers (filtering, fan-out, per-logger level), two custom levels (TRACE, FATAL).
Note: Fatal logs a FATAL record and returns. It does NOT call os.Exit; the decision to exit (and to flush or ship logs first) stays with the caller.
Index ¶
- Constants
- func Debug(msg string, args ...any)
- func Error(msg string, args ...any)
- func Fatal(msg string, args ...any)
- func HandlerOptions(level slog.Leveler) *slog.HandlerOptions
- func Info(msg string, args ...any)
- func Level() slog.Level
- func SetDefault(l Logger)
- func SetLevel(level slog.Level)
- func Trace(msg string, args ...any)
- func Warn(msg string, args ...any)
- type Filter
- type FilterHandler
- func (f *FilterHandler) AddFilter(filter Filter)
- func (f *FilterHandler) Enabled(ctx context.Context, level slog.Level) bool
- func (f *FilterHandler) Handle(ctx context.Context, record slog.Record) error
- func (f *FilterHandler) SetFilters(filters []Filter)
- func (f *FilterHandler) WithAttrs(attrs []slog.Attr) slog.Handler
- func (f *FilterHandler) WithGroup(name string) slog.Handler
- type Logger
- type MultiHandler
- type Option
Constants ¶
const ( // LevelTrace sits below slog.LevelDebug for the noisiest diagnostics. LevelTrace = slog.Level(-8) // LevelFatal sits above slog.LevelError. Logging at this level does not // exit the process; it only emits a FATAL record. LevelFatal = slog.Level(12) )
Variables ¶
This section is empty.
Functions ¶
func HandlerOptions ¶ added in v0.2.0
func HandlerOptions(level slog.Leveler) *slog.HandlerOptions
HandlerOptions returns slog.HandlerOptions wired to level with the custom-level name rendering, for building raw handlers passed to WithOutput.
Types ¶
type Filter ¶
type Filter struct {
// contains filtered or unexported fields
}
Filter selects log records and decides what happens to them, built fluently:
log.Deny().Below(slog.LevelInfo)
log.Deny().Attr("path", "/healthz*")
log.Shorten("body").Limit(200).Message("http response")
A record must match every set criterion (level, message, attributes) for the filter's action to apply; criteria left unset are ignored.
func Allow ¶
func Allow() Filter
Allow starts a filter that passes matching records through unchanged.
func Shorten ¶
Shorten starts a filter that truncates the given attribute keys on matching records. The default length limit is 100; change it with Limit.
func (Filter) Attr ¶
Attr matches when the record's attribute key equals val. A val ending in "*" matches by prefix. The record message is available under the "msg" key.
type FilterHandler ¶ added in v0.2.0
type FilterHandler struct {
// contains filtered or unexported fields
}
FilterHandler is a slog.Handler that applies an ordered list of filters to each record before passing it to a wrapped handler. Filters run in order; the first Deny match drops the record, and Shorten matches rewrite attributes.
func NewFilterHandler ¶ added in v0.2.0
func NewFilterHandler(handler slog.Handler, filters ...Filter) *FilterHandler
NewFilterHandler wraps handler with the given filters.
func (*FilterHandler) AddFilter ¶ added in v0.2.0
func (f *FilterHandler) AddFilter(filter Filter)
AddFilter appends a filter; safe to call concurrently with logging.
func (*FilterHandler) Enabled ¶ added in v0.2.0
Enabled reports whether the wrapped handler emits records at level. Filters are evaluated in Handle, not here, since they can match on message or attrs.
func (*FilterHandler) Handle ¶ added in v0.2.0
Handle applies each matching filter to the record and forwards the result to the wrapped handler. A matching Deny returns early and drops the record.
func (*FilterHandler) SetFilters ¶ added in v0.2.0
func (f *FilterHandler) SetFilters(filters []Filter)
SetFilters replaces the filter list; safe to call concurrently with logging.
type Logger ¶
type Logger interface {
slog.Handler
With(args ...any) Logger
// WithLevel returns a logger with a new minimum level, preserving the
// underlying outputs, format, and attributes.
WithLevel(level slog.Level) Logger
Trace(msg string, args ...any)
Debug(msg string, args ...any)
Info(msg string, args ...any)
Warn(msg string, args ...any)
Error(msg string, args ...any)
// Fatal logs at LevelFatal and returns; it does not exit the process.
Fatal(msg string, args ...any)
// Slog returns the wrapped *slog.Logger as an escape hatch.
Slog() *slog.Logger
}
Logger is the extended slog contract: a slog.Handler plus level-aware helpers and the custom Trace/Fatal levels.
func Default ¶
func Default() Logger
Default returns the process-wide Logger backing the package-level helpers.
func Discard ¶ added in v0.2.0
func Discard() Logger
Discard returns a Logger that drops every record. Useful as a default in tests or libraries that take a Logger but should stay silent.
type MultiHandler ¶
type MultiHandler struct {
// contains filtered or unexported fields
}
MultiHandler fans a record out to several handlers, so one logger can write to multiple outputs (e.g. a text console and a JSON file) at once.
func NewMultiHandler ¶
func NewMultiHandler(handlers ...slog.Handler) *MultiHandler
NewMultiHandler returns a handler that dispatches to each of handlers in order.
func (*MultiHandler) Enabled ¶
Enabled reports whether any child handler is enabled for the level, so a record is dropped only when every output would discard it.
func (*MultiHandler) Handle ¶
Handle dispatches the record to every enabled child handler and joins any errors, so one failing output does not stop the others.
type Option ¶
type Option func(*builder)
Option configures a Logger built by New.
func WithFilters ¶
WithFilters wraps the assembled outputs in a FilterHandler.
func WithJSON ¶
WithJSON adds a JSON handler writing to w. Pass a rotating writer (e.g. a lumberjack.Logger) here to keep that dependency out of this module.
func WithOutput ¶
WithOutput adds an arbitrary slog.Handler (a memory sink, an exporter, ...). The handler controls its own level; WithLevel does not affect it.