logger

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 11, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package logger provides a small Logger interface over log/slog so production code logs to stderr/JSON while tests substitute Capture to assert on emitted records.

Production:

log := logger.New(logger.Config{Format: logger.FormatJSON, Level: slog.LevelInfo})
log.Info("server started", "addr", addr)

Tests:

cap := logger.NewCapture(slog.LevelDebug)
cap.Info("hello", "k", 1)
if !cap.HasMessage("hello") { t.Fatal("missing message") }

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Capture

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

Capture is a Logger that buffers records in memory for assertion. Safe for concurrent use.

func NewCapture

func NewCapture(level slog.Level) *Capture

NewCapture returns a Capture that records every entry at level or above.

func (*Capture) Debug

func (c *Capture) Debug(msg string, args ...any)

func (*Capture) Enabled

func (c *Capture) Enabled(_ context.Context, level slog.Level) bool

Enabled reports whether level is at or above the configured threshold.

func (*Capture) Error

func (c *Capture) Error(msg string, args ...any)

func (*Capture) FilterLevel

func (c *Capture) FilterLevel(level slog.Level) []Record

FilterLevel returns the subset of records at exactly level.

func (*Capture) HasMessage

func (c *Capture) HasMessage(substr string) bool

HasMessage reports whether any captured record's Msg contains substr.

func (*Capture) Info

func (c *Capture) Info(msg string, args ...any)

func (*Capture) Records

func (c *Capture) Records() []Record

Records returns a snapshot of every captured record.

func (*Capture) Reset

func (c *Capture) Reset()

Reset drops all captured records.

func (*Capture) Warn

func (c *Capture) Warn(msg string, args ...any)

func (*Capture) With

func (c *Capture) With(args ...any) Logger

With returns a child Capture that prepends args to every record. The child shares the parent's record buffer so all entries land in one place.

type Config

type Config struct {
	// Writer receives the encoded records. Defaults to os.Stderr.
	Writer io.Writer
	// Format selects text vs JSON. Defaults to JSON (good for production
	// log aggregators; switch to FormatText for local development).
	Format Format
	// Level is the minimum level recorded. Defaults to slog.LevelInfo.
	Level slog.Level
	// AddSource adds a source=file:line attribute to every record.
	AddSource bool
}

Config controls New. The zero value writes JSON at info level to stderr.

type Format

type Format int

Format selects the on-wire representation.

const (
	// FormatText writes human-readable key=value lines (slog.TextHandler).
	FormatText Format = iota
	// FormatJSON writes one JSON object per record (slog.JSONHandler).
	FormatJSON
)

type Logger

type Logger interface {
	Debug(msg string, args ...any)
	Info(msg string, args ...any)
	Warn(msg string, args ...any)
	Error(msg string, args ...any)
	// With returns a child Logger that prepends args to every record.
	With(args ...any) Logger
	// Enabled reports whether the underlying handler accepts records at level.
	Enabled(ctx context.Context, level slog.Level) bool
}

Logger is the structured-logging interface. The methods mirror slog's shape so callers can switch implementations without rewriting call sites.

func Discard

func Discard() Logger

Discard returns a Logger that drops everything. Useful for benchmarks.

func FromSlog

func FromSlog(l *slog.Logger) Logger

FromSlog wraps an existing *slog.Logger. Useful when the application root already constructed one and you want to inject it as the Logger interface.

func New

func New(cfg Config) Logger

New returns a Logger backed by slog with the given config.

type Record

type Record struct {
	Level slog.Level
	Msg   string
	Attrs map[string]any
}

Record is a captured log entry.

Jump to

Keyboard shortcuts

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