Documentation
¶
Overview ¶
Package logtest provides test helpers for the log package.
Use NewRecorder to capture structured log output and assert on individual fields without timestamp noise or JSON parsing boilerplate:
rec := logtest.NewRecorder(t)
logger, _ := log.NewLogger(log.WithOutput(rec), log.WithFormat(log.FormatJSON))
logger.Info(ctx, "user created", slog.String("user_id", "u-1"))
entry := rec.Last()
assert.Equal(t, "user created", entry["message"])
assert.Equal(t, "u-1", entry["user_id"])
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Recorder ¶
type Recorder struct {
// contains filtered or unexported fields
}
Recorder captures log output written by a log.Logger configured with log.FormatJSON and log.WithOutput(rec). Each written record is parsed into a map with the "timestamp" key stripped for stable assertions.
func New ¶
New returns a logging.Logger and a Recorder pre-configured for testing. The logger writes JSON at DEBUG level so all records are captured.
func NewRecorder ¶
NewRecorder returns a Recorder ready for use in t.
Click to show internal directories.
Click to hide internal directories.