Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var (
DefaultLogger = NewDiscardLogger()
)
Functions ¶
func NewDiscardLogger ¶
NewDiscardLogger returns a logger whose output is discarded. This is useful for tests where log output should be suppressed.
func NewLogger ¶
func NewLogger(cfg LoggerConfig) *slog.Logger
NewLogger creates a configured *slog.Logger and a shutdown function. The shutdown function is a no-op in this implementation but is returned to make it easy to add asynchronous or file-based writers later. Call the shutdown function on process exit if you add asynchronous writers.
func OrDefault ¶ added in v1.0.0
OrDefault returns lg unless it is nil, in which case the default logger is returned.
func ParseLevel ¶
ParseLevel maps common textual level names to slog.Level. The function is case-insensitive and ignores surrounding whitespace. If an unrecognized value is provided, slog.LevelInfo is returned.
Types ¶
type LoggedEntry ¶
type LoggedEntry struct {
Time time.Time `json:"time"`
Level slog.Level `json:"level"`
Msg string `json:"msg"`
Attrs map[string]any `json:"attrs"`
Source *slog.Source `json:"source"`
PC uintptr `json:"pc"`
}
LoggedEntry represents a captured structured log entry for assertions in tests. It contains the timestamp, level, message, attributes, source location, and performance details.
func FindEntries ¶
func FindEntries(th *TestHandler, pred func(LoggedEntry) bool) []LoggedEntry
FindEntries returns a copy of entries from the TestHandler that satisfy the provided predicate. The handler's internal slice is copied under lock to avoid races.
func RequireEntry ¶
func RequireEntry(t *testing.T, th *TestHandler, pred func(LoggedEntry) bool, timeout time.Duration) LoggedEntry
RequireEntry fails the test if a matching entry isn't observed within the given timeout. When a matching entry is found it is returned. If the timeout elapses, the test is failed and the captured entries are included in the failure message to aid debugging.
type LoggerConfig ¶
type LoggerConfig struct {
Version string
// If Out is nil, stdout is used.
Out io.Writer
Level slog.Level
JSON bool // true => JSON output, false => text
Source bool
}
LoggerConfig is a minimal, convenient set of options for creating a new slog.Logger.
Fields:
- Version: application or build version included with each log entry.
- Out: destination writer for log output. If nil, os.Stdout is used.
- Level: minimum logging level.
- JSON: when true, output is JSON; otherwise, human-readable text is used.
type SlogWriter ¶
type SlogWriter struct {
// contains filtered or unexported fields
}
type TestHandler ¶
type TestHandler struct {
Entries []LoggedEntry
T testingT
// contains filtered or unexported fields
}
TestHandler captures structured entries so tests can assert on logs. It is safe for concurrent use.
func NewTestHandler ¶
func NewTestHandler(t testingT) *TestHandler
NewTestHandler creates an empty TestHandler. Optionally pass a testing.T to have the handler echo captured entries to the test log (via Logf).
func NewTestLogger ¶
func NewTestLogger(t testingT, level slog.Level) ( *slog.Logger, *TestHandler)
NewTestLogger returns a *slog.Logger that writes to a TestHandler and the handler itself for assertions. The returned logger has a default attribute ("test"="true") to make it easier to identify test logs.
func (*TestHandler) Enabled ¶
Enabled returns true for all levels. Filtering is expected to be handled by the caller or the logger's handler options.
func (*TestHandler) Handle ¶
Handle captures the provided record as a LoggedEntry and appends it to the handler's Entries slice. If a testingT was provided, a human-readable line is also logged to the test output.