Documentation
¶
Overview ¶
Package slogtest provides support for recording structured log slog messages during tests and verifying that the expected messages have been emitted.
`slogtest.Recorder` is a full-featured `slog.Handler` that records every structured log message and supports both shared attributes with `With()` and nested groups with `WithGroup()`.
`slogtest.GetAllAttrs` and `slogtest.GetFlattenAttrs` are helper functions to retrieve all the attributes of a record as a map, with fully resolved values.
`slogtest.WithSlogRecorder()` evaluate a function with a temporary `slogtest.Recorder` installed as the default logger of the slog packages.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetAllAttrs ¶
GetAllAttrs returns all the attributes of a record as a map, preserving the hierarchy of groups, with fully resolved values.
func GetFlattenAttrs ¶
GetFlattenAttrs returns all the attributes of a record as a single-level map, where keys of nested groups are expanded to a dot-separated syntax, and all the values are fully resolved.
func WithSlogRecorder ¶
func WithSlogRecorder(f func(logs *Recorder))
WithSlogRecorder creates and install a new log recorder as the global slog.Default(), and passes the recorder to the nested function. Upon return, it restores the prior slog.Default() and attempts to restore the state of the log package as well -- if a log.SetOutput() was installed after a call to slog.SetDefault(), the output will not be restored. Note that this function has global side-effects and may not behave as expected with concurrently executing tests.
Types ¶
type Recorder ¶
type Recorder struct {
// contains filtered or unexported fields
}
Recorder is an implementation of slog.Handler that records each log entry for later verification during unit tests.