Documentation
¶
Overview ¶
Package logging configures log/slog from the environment: a level, an encoding, a timestamp layout and an optional log file.
Text output goes through github.com/lmittmann/tint, which colorizes the level and an http-style "status" attr when the destination is a terminal.
NewHandler builds a handler and touches no global state. Setup is the same thing installed with slog.SetDefault, which is what a main usually wants.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewContextHandler ¶
See https://pkg.go.dev/log/slog#example-Handler-LevelHandler and https://github.com/golang/example/blob/master/slog-handler-guide/README.md
Wraps next so every record carries the attrs fn reports for its ctx. slog handlers ignore the ctx by default, so without this every *Context call site would have to repeat those attrs.
A nil fn returns next unchanged.
func NewHandler ¶
Builds the handler cfg describes. Handler wraps call to fn: it would add attrs which fn AttrsFunc returns from the context. The returned io.Closer closes the log file. It is a no-op when writing to stdout.
Types ¶
type AttrsFunc ¶
AttrsFunc pulls ctx-scoped attrs, e.g. a request correlation id. Returning nil or an empty slice leaves the record untouched.
type Config ¶
type Config struct {
Level slog.Level
Format Format
TimeFormat TimeFormat
File string // empty writes to stdout
}
Config is a logging setup. Its zero value resolves to the documented defaults.
func ConfigFromEnv ¶
Env vars:
- LOG_LEVEL: debug | info (default) | warn | error
- LOG_FORMAT: text (default) | json
- LOG_FILE: path; empty writes to stdout (default)
- LOG_TIME: sec | milli | nano | dt-sec | dt-milli (default) | rfc3339 | rfc3339nano; text timestamp layout ('dt-' prefixed ones add the date). json is unaffected, it always writes RFC3339Nano.
type Format ¶
type Format string
Format selects the log encoding: maps to the slog.Handler type. Configured via LOG_FORMAT. The zero value resolves to FormatText.
type TimeFormat ¶
type TimeFormat string
TimeFormat selects the text handler's timestamp layout. Configured via LOG_TIME. The zero value resolves to TimeDateMilli.
const ( TimeSec TimeFormat = "sec" TimeMilli TimeFormat = "milli" TimeNano TimeFormat = "nano" TimeDateSec TimeFormat = "dt-sec" TimeDateMilli TimeFormat = "dt-milli" TimeRFC3339 TimeFormat = "rfc3339" TimeRFC3339Nano TimeFormat = "rfc3339nano" )