Documentation
¶
Index ¶
- Constants
- Variables
- func AddHandler(handler slog.Handler) bool
- func Init(opts Options) error
- func SetHandlers(handlers ...slog.Handler)
- type FanOutHandler
- func (h *FanOutHandler) Enabled(ctx context.Context, level slog.Level) bool
- func (h *FanOutHandler) Handle(ctx context.Context, r slog.Record) error
- func (h *FanOutHandler) Len() int
- func (h *FanOutHandler) WithAttrs(attrs []slog.Attr) slog.Handler
- func (h *FanOutHandler) WithGroup(name string) slog.Handler
- type MultiWriterHandler
- type Options
Constants ¶
const ( EnvVarNameLevel = "LOG_LEVEL" EnvVarNameFormat = "LOG_FORMAT" EnvVarNameAddSource = "LOG_ADD_SOURCE" )
The environment variables read when Options does not name others.
Variables ¶
Functions ¶
func AddHandler ¶
AddHandler stacks an extra handler onto the default FanOutHandler, keeping the existing ones. Use it to tee logs somewhere additional — a ring buffer, a shipper, a test capture — without replacing what init configured.
Call it EARLY. slog.Logger.With and WithGroup snapshot the handler chain at the moment they are called, so a logger derived BEFORE the Add never carries the new handler and silently drops those records. Handlers added before any derivation reach everything.
Reports whether the default logger was a FanOutHandler, i.e. whether this package configured it. False means something else replaced slog's default and the handler was stacked onto that instead — still added, but the caller has lost the stdout/stderr split this package sets up, which is worth noticing rather than discovering through absent logs.
func Init ¶
Init reconfigures the default logger, reading the variables named by opts.
The blank import already configures logging with the default names, so this is only needed to change them or their fallbacks:
slogconf.Init(slogconf.Options{
LevelEnvVar: "MYAPP_LOG_LEVEL",
FormatEnvVar: "MYAPP_LOG_FORMAT",
})
Call it as early in main as possible. It replaces slog's default logger, so any logger already derived through slog.Logger.With keeps pointing at the previous handler chain — the same ordering caveat AddHandler carries.
func SetHandlers ¶
SetHandlers replaces all handlers in the default FanOutHandler with the provided ones.
Types ¶
type FanOutHandler ¶
type FanOutHandler struct {
// contains filtered or unexported fields
}
FanOutHandler dispatches log records to multiple handlers. The default slog handler is always a FanOutHandler. Use SetHandlers to replace all handlers and AddHandler to stack extra ones on top.
func NewFanOutHandler ¶
func NewFanOutHandler(handlers ...slog.Handler) *FanOutHandler
NewFanOutHandler creates a handler that dispatches to all provided handlers.
func (*FanOutHandler) Enabled ¶
Enabled reports whether any of the underlying handlers handle records at the given level.
func (*FanOutHandler) Handle ¶
Handle dispatches the record to every underlying handler, and keeps going when one of them fails.
Returning at the first error would let a single broken sink silence all the ones after it: slog discards whatever Handle returns, so a Loki handler that cannot reach its server would take stdout down with it and nothing would say so. Every handler sees the record regardless of what the earlier ones did, and the failures are joined so none is lost.
func (*FanOutHandler) Len ¶
func (h *FanOutHandler) Len() int
Len reports how many handlers the fan-out dispatches to. Exported so callers can assert that AddHandler stacked onto the existing set rather than replacing it — the difference is invisible from the outside until logs go missing.
type MultiWriterHandler ¶
type MultiWriterHandler struct {
// contains filtered or unexported fields
}
MultiWriterHandler routes log records to different writers based on level. Error and Warn levels go to stderr, Info and Debug go to stdout.
func NewMultiWriterHandler ¶
func NewMultiWriterHandler(f format, opts *slog.HandlerOptions, stdout, stderr io.Writer) (*MultiWriterHandler, error)
NewMultiWriterHandler creates a handler that routes logs to stdout/stderr based on level.
func (*MultiWriterHandler) Enabled ¶
Enabled reports whether the handler handles records at the given level.
func (*MultiWriterHandler) Handle ¶
Handle handles the Record by routing it to the appropriate writer.
type Options ¶
type Options struct {
// LevelEnvVar overrides the variable read for the log level.
LevelEnvVar string
// FormatEnvVar overrides the variable read for the output format.
FormatEnvVar string
// AddSourceEnvVar overrides the variable read for source reporting.
AddSourceEnvVar string
// DefaultLevel applies when the level variable is unset or empty.
// Empty means "info".
DefaultLevel string
// DefaultFormat applies when the format variable is unset or empty.
// Empty means "text".
DefaultFormat string
// DefaultAddSource applies when the source variable is unset or empty.
DefaultAddSource bool
}
Options selects which environment variables configure logging and what they fall back to. The zero value reproduces this package's historical behaviour exactly — LOG_LEVEL / LOG_FORMAT / LOG_ADD_SOURCE, defaulting to info + text + no source — so the blank import is unaffected by this type existing.
The names are FIELDS rather than struct tags because a struct tag is fixed at compile time: nothing can hand one a value at runtime. That is why a caller who needed different variable names previously had to copy this whole package rather than configure it.