Documentation
¶
Overview ¶
Package slogx is a standard structured-logging setup, extracted from the Go apps that all installed the same log/slog handler by hand.
It is a thin, cohesive helper around log/slog — not a logging framework and not a replacement handler:
- Setup installs slog's default logger the standard way: a leveled text (logfmt) or JSON handler with UTC-normalized timestamps, returning the *slog.LevelVar so the level can be set after config is read or flipped at runtime. NewHandler is the same without the SetDefault, for composition.
- ParseLevel maps a LOG_LEVEL string to an slog.Level, adding the long-form "warning" alias slog lacks and reporting whether the value was recognized.
- UTCTime is the exported ReplaceAttr that renders timestamps in UTC, for consumers that build their own slog.HandlerOptions.
It deliberately does not own log-level environment-variable names, secret redaction, audit-event schemas, or per-app attribute conventions — those stay in the consuming app. It carries no dependencies beyond the standard library.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewHandler ¶
NewHandler builds a leveled slog.Handler with UTCTime applied and returns it alongside the *slog.LevelVar backing its level. Hold the LevelVar to change the level after install: install a handler before config is read (so early warnings emit at the default level), then Set the parsed level; or flip it at runtime for a debug toggle. Setup wraps this and installs the result as the default logger.
func ParseLevel ¶
ParseLevel maps a log-level string to an slog.Level. It is case-insensitive, trims surrounding space, and maps the long-form "warning" to "warn" (which slog.Level.UnmarshalText does not accept); otherwise it delegates to UnmarshalText, so offset syntax such as "warn+1" or "debug-2" also works.
An empty string returns def with ok=true — an unset level is not an error. A non-empty unparseable value returns def with ok=false, so the caller can warn. Parse the level BEFORE installing the handler (via Setup), then warn on ok==false afterward, so the warning is emitted through the configured handler.
Example ¶
package main
import (
"fmt"
"log/slog"
"github.com/cplieger/slogx"
)
func main() {
lvl, ok := slogx.ParseLevel("warning", slog.LevelInfo)
fmt.Println(lvl, ok)
_, ok = slogx.ParseLevel("banana", slog.LevelInfo)
fmt.Println(ok)
}
Output: WARN true false
func Setup ¶
Setup builds a handler per opts and installs it as slog's default logger, returning the *slog.LevelVar backing its level for later changes (install early, then Set the level once config is read; or flip it at runtime). It is the one-call standard logger setup most apps make once at startup.
func UTCTime ¶
UTCTime is a slog ReplaceAttr that renders a record's built-in time key in UTC, so log-line timestamps are zone-stable regardless of the container's TZ. It rewrites only the top-level time attribute; a user attribute that happens to share the "time" key inside a group is left untouched. Setup and NewHandler apply it automatically; it is exported for consumers that build their own slog.HandlerOptions.
Types ¶
type Format ¶
type Format int
Format selects the slog handler's output encoding.
const ( // Text emits canonical logfmt (time=… level=… msg=… k=v). It is the // default for a container's own lifecycle and diagnostic logs. Text Format = iota // JSON emits one JSON object per line, for apps whose structured log events // are the product (shipped to Loki and rendered as dashboard columns). JSON )
func ParseFormat ¶ added in v1.2.0
ParseFormat maps a log-format string to a Format. It is case-insensitive, trims surrounding space, and accepts "text" and "json" — the same contract ParseLevel gives a log-level string.
An empty string returns def with ok=true — an unset format is not an error. A non-empty unrecognized value returns def with ok=false, so the caller can warn (a config may hold an expanded secret in the wrong field, so warn field-name-only when that is a possibility). Parse the format BEFORE installing the handler (via Setup), then warn on ok==false afterward, so the warning is emitted through the configured handler.
type Options ¶
type Options struct {
// Output is the destination writer; nil means os.Stderr. Apps whose JSON log
// events are the product typically set os.Stdout.
Output io.Writer
// Format is Text (default) or JSON.
Format Format
// Level is the initial level; the zero value is slog.LevelInfo. It is held in
// the *slog.LevelVar that NewHandler and Setup return, so it can be changed
// after the handler is installed.
Level slog.Level
// AddSource records the source file:line of each call site. Off by default —
// useful when debugging, noisy for production.
AddSource bool
}
Options configures a handler built by NewHandler or Setup. The zero value is valid and yields a text handler at Info level on stderr.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package capture provides a slog.Handler that records log records for assertion in tests, plus helpers to install it as the default logger.
|
Package capture provides a slog.Handler that records log records for assertion in tests, plus helpers to install it as the default logger. |