slogconf

package
v1.7.2 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	EnvVarNameLevel     = "LOG_LEVEL"
	EnvVarNameFormat    = "LOG_FORMAT"
	EnvVarNameAddSource = "LOG_ADD_SOURCE"
)

The environment variables read when Options does not name others.

Variables

View Source
var (
	ErrInvalidLogLevel     = errors.New("invalid log level")
	ErrInvalidLogFormat    = errors.New("invalid log format")
	ErrInvalidLogAddSource = errors.New("invalid log add-source value")
)

Functions

func AddSink added in v1.7.0

func AddSink(handler slog.Handler) bool

AddSink stacks an extra handler alongside the existing ones — a ring buffer, a shipper, a test capture — without touching where the process already prints.

Call it early, for the same snapshotting reason as SetOutput.

Reports whether the default logger was one this package configured. False means something else replaced slog's default and the sink was stacked onto that instead: still added, but the stdout/stderr split this package sets up is gone.

func Init

func Init(opts Options) error

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

func SetHandlers(hs ...slog.Handler)

SetHandlers replaces the ENTIRE chain — output slot and every sink.

This is the escape hatch, not the everyday call: it discards anything AddSink put there. Reach for it when you genuinely want to start over, which in practice means tests pointing everything at a buffer or io.Discard. To change where logs print while keeping your sinks, use SetOutput.

func SetOutput added in v1.7.0

func SetOutput(handler slog.Handler) bool

SetOutput replaces the process's output handler, leaving every sink added with AddSink in place.

This is the call for "send my logs somewhere else". Adding a second handler that also writes to the terminal does NOT replace the first one — both would receive every record and print it twice.

Call it early. slog.Logger.With and WithGroup snapshot the handler chain at the moment they are called, so a logger derived BEFORE this never sees the new output.

Reports whether the default logger was one this package configured. False means something else replaced slog's default, so there was no slot to swap and the whole chain was replaced instead — worth noticing rather than discovering through absent logs.

Types

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.

Jump to

Keyboard shortcuts

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