testlogger

package
v2.3.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 15, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package testlogger provides a slog handler which can handle t.Parallel() tests while being a global logging handler, redirecting it to the correct underlying logger for each test thread.

This package also muffles certain log messages to reduce noise in the snapshots and to keep the snapshots consistent across runs.

Index

Constants

View Source
const BeginDirectoryScan = "---Begin Directory Scan---"
View Source
const EndDirectoryScan = "---End Directory Scan---"

Variables

This section is empty.

Functions

func BeginDirScanMarker added in v2.1.0

func BeginDirScanMarker()

BeginDirScanMarker prints out a directory scanning marker during testing to allow snapshots to sort the scanning order to allow for unsorted file walks.

func EndDirScanMarker added in v2.1.0

func EndDirScanMarker()

EndDirScanMarker prints out a directory scanning marker during testing to mark the end of directory walks

Types

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler can be set as the global logging handler before the test starts, and individual test cases can add their own instance/implementation of the cmdlogger.CmdLogger interface.

func New

func New() *Handler

func (*Handler) AddInstance

func (tl *Handler) AddInstance(logger cmdlogger.CmdLogger)

AddInstance adds a "global" logger to this specific test run.

func (*Handler) Delete

func (tl *Handler) Delete()

Delete removes the logger created by AddInstance() This **must** be called before a test ends, as the same memory address may be reused.

func (*Handler) Enabled

func (tl *Handler) Enabled(ctx context.Context, level slog.Level) bool

func (*Handler) Handle

func (tl *Handler) Handle(ctx context.Context, record slog.Record) error

func (*Handler) HasErrored

func (tl *Handler) HasErrored() bool

HasErrored returns true if there have been any calls to Handle with a level of slog.LevelError

func (*Handler) HasErroredBecauseInvalidConfig added in v2.1.0

func (tl *Handler) HasErroredBecauseInvalidConfig() bool

HasErroredBecauseInvalidConfig returns true if there have been any calls to Handle with a level of slog.LevelError due to a config file being invalid

func (*Handler) SendEverythingToStderr

func (tl *Handler) SendEverythingToStderr()

SendEverythingToStderr tells the logger to send all logs to stderr regardless of their level.

This is useful if we're expecting to output structured data to stdout such as JSON, which cannot be mixed with other output.

func (*Handler) SetHasErrored added in v2.3.0

func (tl *Handler) SetHasErrored()

func (*Handler) SetLevel

func (tl *Handler) SetLevel(level slog.Leveler)

func (*Handler) WithAttrs

func (tl *Handler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*Handler) WithGroup

func (tl *Handler) WithGroup(g string) slog.Handler

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL