logger

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package logger provides a small, leveled, structured logging interface for the proxy along with a zerolog-backed implementation and a no-op implementation for tests.

Package-level functions (Debug, Info, Warn, Error, Fatal, With) delegate to a default Logger that writes to os.Stderr at LevelInfo.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(msg string, tags ...tag.Tag)

Debug logs msg and tags at LevelDebug using the default logger.

func Error

func Error(msg string, tags ...tag.Tag)

Error logs msg and tags at LevelError using the default logger.

func Fatal

func Fatal(msg string, tags ...tag.Tag)

Fatal logs msg and tags using the default logger. Depending on the implementation, it may call os.Exit(1).

func Info

func Info(msg string, tags ...tag.Tag)

Info logs msg and tags at LevelInfo using the default logger.

func SetDefault

func SetDefault(l Logger)

SetDefault replaces the Logger used by the package-level functions. A nil logger is ignored. It is not safe to call concurrently with the package-level logging functions.

func Warn

func Warn(msg string, tags ...tag.Tag)

Warn logs msg and tags at LevelWarn using the default logger.

Types

type Level

type Level byte

Level controls the minimum severity an entry must have to be logged.

const (
	// LevelDebug logs everything, including verbose debugging detail.
	LevelDebug Level = iota
	// LevelInfo logs informational entries and above. It is the default.
	LevelInfo
	// LevelWarn logs warnings and above.
	LevelWarn
	// LevelError logs only errors (and fatals).
	LevelError
)

func ParseLevel

func ParseLevel(lvl string) Level

ParseLevel converts a level name ("debug", "info", "warn", "error", case insensitive) into a Level. Unrecognized values default to LevelInfo.

type Logger

type Logger interface {
	// Debug logs msg and tags at [LevelDebug].
	Debug(msg string, tags ...tag.Tag)
	// Error logs msg and tags at [LevelError].
	Error(msg string, tags ...tag.Tag)
	// Fatal logs msg and tags, then typically calls os.Exit(1).
	Fatal(msg string, tags ...tag.Tag)
	// Info logs msg and tags at [LevelInfo].
	Info(msg string, tags ...tag.Tag)
	// Warn logs msg and tags at [LevelWarn].
	Warn(msg string, tags ...tag.Tag)

	// With returns a child Logger that includes tags on every entry.
	With(tags ...tag.Tag) Logger
}

Logger emits leveled, structured log entries. Each method takes a human readable message and zero or more tag.Tag key/value pairs.

func Default

func Default() Logger

Default returns a default Logger which writes to os.Stderr at LevelInfo.

func With

func With(tags ...tag.Tag) Logger

With returns a child Logger derived from the default logger that includes tags on every entry.

type NoopLogger

type NoopLogger struct{}

NoopLogger is a Logger that discards every entry. It is useful in tests and anywhere a non-nil Logger is required but output is not wanted. Unlike other implementations, its Fatal does not exit the process.

func NewNoopLogger

func NewNoopLogger() *NoopLogger

NewNoopLogger returns a NoopLogger.

func (*NoopLogger) Debug

func (n *NoopLogger) Debug(string, ...tag.Tag)

func (*NoopLogger) Error

func (n *NoopLogger) Error(string, ...tag.Tag)

func (*NoopLogger) Fatal

func (n *NoopLogger) Fatal(string, ...tag.Tag)

func (*NoopLogger) Info

func (n *NoopLogger) Info(string, ...tag.Tag)

func (*NoopLogger) Warn

func (n *NoopLogger) Warn(string, ...tag.Tag)

func (*NoopLogger) With

func (n *NoopLogger) With(...tag.Tag) Logger

type TestLogger

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

TestLogger is a Logger that records every entry in memory so tests can assert on what was logged. It is safe for concurrent use.

Loggers derived via TestLogger.With share the recording store of the logger they were derived from, so entries logged through a derived logger are visible from the original.

A TestLogger must be created with NewTestLogger; the zero value is not usable.

func NewTestLogger

func NewTestLogger() *TestLogger

NewTestLogger returns an empty TestLogger.

func (*TestLogger) Contains

func (t *TestLogger) Contains(msg string) bool

Contains reports whether an entry with the given message was logged, regardless of its level or tags.

func (*TestLogger) ContainsEntry

func (t *TestLogger) ContainsEntry(l Level, msg string, tags ...tag.Tag) bool

ContainsEntry reports whether an entry was logged with exactly the given level, message, and tags. Tags must match in order, and their values are compared by equality, so tag values must be comparable.

func (*TestLogger) Debug

func (t *TestLogger) Debug(msg string, tags ...tag.Tag)

func (*TestLogger) Error

func (t *TestLogger) Error(msg string, tags ...tag.Tag)

func (*TestLogger) Fatal

func (t *TestLogger) Fatal(msg string, tags ...tag.Tag)

func (*TestLogger) Info

func (t *TestLogger) Info(msg string, tags ...tag.Tag)

func (*TestLogger) Warn

func (t *TestLogger) Warn(msg string, tags ...tag.Tag)

func (*TestLogger) With

func (t *TestLogger) With(tags ...tag.Tag) Logger

With returns a derived Logger that shares the receiver's recording store and prepends the receiver's tags, followed by tags, to every entry it logs.

type ZeroLogger

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

ZeroLogger is a Logger backed by zerolog. It writes structured, leveled JSON log entries, each stamped with a timestamp.

func NewZeroLogger

func NewZeroLogger(w io.Writer, lvl Level) *ZeroLogger

NewZeroLogger returns a ZeroLogger that writes to w and discards any entry below lvl.

func (*ZeroLogger) Debug

func (l *ZeroLogger) Debug(msg string, tags ...tag.Tag)

func (*ZeroLogger) Error

func (l *ZeroLogger) Error(msg string, tags ...tag.Tag)

func (*ZeroLogger) Fatal

func (l *ZeroLogger) Fatal(msg string, tags ...tag.Tag)

func (*ZeroLogger) Info

func (l *ZeroLogger) Info(msg string, tags ...tag.Tag)

func (*ZeroLogger) Warn

func (l *ZeroLogger) Warn(msg string, tags ...tag.Tag)

func (*ZeroLogger) With

func (l *ZeroLogger) With(tags ...tag.Tag) Logger

Directories

Path Synopsis
Package tag provides typed key/value pairs used to attach structured context to log entries.
Package tag provides typed key/value pairs used to attach structured context to log entries.

Jump to

Keyboard shortcuts

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