Documentation
¶
Overview ¶
Package logging constructs the *slog.Logger a process writes through, from a configuration that takes part in the layered load.
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 normalizes (trims and lower-cases), applies defaults (info, text), reads the environment overrides named by the configuration's Env value, normalizes again, and 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. NewEnv composes the standard names from a prefix; an empty name disables that one override, and a zero Env means no environment overrides at all.
Level is a string. slog.Level parses it — "debug", "INFO", and offsets such as "warn+2", case-insensitively — through Level.Slog. An empty Level is unset, distinct from "set to info"; Finalize has replaced it with a default by the time validation runs.
Construction ¶
New returns a logger writing to the caller's io.Writer, with slog.NewJSONHandler selected by FormatJSON and slog.NewTextHandler otherwise. The caller supplies the writer; a test passes a buffer.
New returns no error; Finalize is the validation point. A Config literal that skipped Finalize yields an info-level logger.
Scope ¶
Subsystems take a *slog.Logger as an ordinary dependency. The HTTP request logger is in the web package.
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 names the environment variables Finalize reads; it is excluded from JSON, so only the composition root can set it.
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 the standard override names from a prefix: LOG_LEVEL and LOG_FORMAT under whatever prefix config.EnvName produces.
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.