Documentation
¶
Overview ¶
Package logging constructs the *slog.Logger a process writes through, from a configuration that takes part in the layered load.
The package exports:
- Config, the logger's configuration, loaded with the config package
- Env and NewEnv, the environment override names Config reads
- Level and its constants LevelDebug, LevelInfo, LevelWarn, and LevelError, the minimum level the logger emits
- Format and its constants FormatText and FormatJSON, the handler the logger writes through
- New, which constructs the logger
Configuration ¶
Config holds a Level and a Format and implements the config package's Merge and Finalize contract, so it loads as part of an application's configuration rather than on its own. Finalize runs, in order:
- composes its override names from the prefix it receives with NewEnv, recording them on Env
- normalizes (trims and lower-cases)
- applies defaults (info, text)
- reads the overrides
- normalizes again
- validates
A value from a file or a shell therefore validates identically in any casing, and a blank or whitespace-only value takes the default. An empty prefix composes no names, disabling the overrides.
Construction ¶
New returns a logger writing to the caller's io.Writer, with a JSON handler for FormatJSON and a text handler otherwise. Finalize is the validation point: New returns no error, and a Config that skipped Finalize yields an info-level logger.
Record times ¶
Both handlers write each record's time in time.UTC, whatever time.Local is, so it ends in "Z" in either format. Only the record's own time is converted: a time.Time the caller passes as an attribute is written as the caller built it.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Config ¶
Config selects the level and format the process logger is built with. Env records the environment-variable names Finalize composed and read; it is excluded from JSON.
type Env ¶
Env names the environment variables Config.Finalize reads. An empty name disables that one override; the zero value disables both.
func NewEnv ¶
NewEnv composes <PREFIX>_LOG_LEVEL and <PREFIX>_LOG_FORMAT with config.EnvName; an empty prefix returns the zero Env.
type Level ¶
type Level string
Level is the minimum level the logger emits, in the vocabulary slog.Level parses: "debug", "INFO", and offsets such as "warn+2", case-insensitively. An empty Level is unset, distinct from "set to info".
const ( LevelDebug Level = "debug" LevelInfo Level = "info" LevelWarn Level = "warn" LevelError Level = "error" )
The standard levels; any string slog.Level parses is valid.