logging

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package logging provides context-based logging utilities and a generic logging interface for the toolkit application.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithContext

func WithContext(ctx context.Context, logger Logger) context.Context

WithContext returns a new context with the provided Logger attached.

Types

type Entry added in v0.9.0

type Entry struct {
	Time    time.Time
	Level   Level
	Message string
	Fields  []any // alternating key, value — as passed to the ...w methods
}

Entry is a single captured log record.

type Level added in v0.9.0

type Level int8

Level identifies the severity of a captured log entry.

const (
	LevelDebug Level = iota
	LevelInfo
	LevelWarn
	LevelError
)

Severity levels for captured entries, ascending.

type Logger

type Logger interface {
	Debugw(msg string, kv ...any)
	Infow(msg string, kv ...any)
	Warnw(msg string, kv ...any)
	Errorw(msg string, kv ...any)
	WithFields(kv ...any) Logger
	DebugEnabled() bool
	Sync() error
}

Logger is an abstract logging interface for use throughout the codebase.

It is implemented by both zapLogger and slogLogger, allowing the application to switch between zap and slog backends as needed.

func FromContext

func FromContext(ctx context.Context) Logger

FromContext retrieves the Logger from the context, or returns a no-op logger if not found.

func MustNewFileLogger

func MustNewFileLogger(debug bool, filename string) Logger

MustNewFileLogger returns a file Logger or panics if creation fails.

func MustNewLogger

func MustNewLogger(debug bool) Logger

MustNewLogger creates a new Logger or panics if creation fails.

func NewFileLogger

func NewFileLogger(debug bool, filename string, logFormat string) (Logger, error)

NewFileLogger returns a Logger that writes to filename through lumberjack rotation (size + age + gzip backups); see NewFileLoggerWithLevel for the rotation settings. If debug is true, uses development encoder config, else production config. logFormat: "console", "json", or "slog"

func NewFileLoggerWithLevel added in v0.2.0

func NewFileLoggerWithLevel(debug bool, filename string, logFormat string, logLevel string) (Logger, error)

NewFileLoggerWithLevel returns a Logger that writes to a rotating file with a specified minimum level. - debug controls encoder config (development vs production) and is used when level is empty. - logFormat: "console", "json", or "slog" - logLevel: "debug" | "info" | "warn" | "error" (empty falls back to debug flag)

func NewLogger

func NewLogger(debug bool) (Logger, error)

NewLogger creates a new Logger. If debug is true, uses zap.NewDevelopment, else zap.NewProduction. By default, uses zap backend. To use slog, call NewSlogLogger directly.

func NewNoOpLogger

func NewNoOpLogger() Logger

NewNoOpLogger returns a Logger that does nothing (for tests).

func NewSlogLogger

func NewSlogLogger(s *slog.Logger, debug bool) Logger

NewSlogLogger returns a Logger backed by a slog.Logger. The debug flag controls DebugEnabled().

func NewTee added in v0.9.0

func NewTee(loggers ...Logger) Logger

NewTee returns a Logger that fans every call out to all loggers.

func NewZapLogger

func NewZapLogger(s *zap.SugaredLogger, debug bool) Logger

NewZapLogger returns a Logger backed by a zap.SugaredLogger. The debug flag controls DebugEnabled().

type RingSink added in v0.9.0

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

RingSink is a Logger that retains the most recent entries in a fixed-capacity, mutex-guarded ring buffer. It is safe for concurrent writes from loader / watch goroutines. Snapshot returns a copy for rendering. DebugEnabled is always true so it captures every level regardless of the file logger's configured level.

func NewRingSink added in v0.9.0

func NewRingSink(capacity int) *RingSink

NewRingSink returns a RingSink retaining the last capacity entries.

func (*RingSink) DebugEnabled added in v0.9.0

func (r *RingSink) DebugEnabled() bool

DebugEnabled reports true: the ring always captures debug entries.

func (*RingSink) Debugw added in v0.9.0

func (r *RingSink) Debugw(msg string, kv ...any)

Debugw logs a debug message with key-value fields.

func (*RingSink) Errorw added in v0.9.0

func (r *RingSink) Errorw(msg string, kv ...any)

Errorw logs an error message with key-value fields.

func (*RingSink) Infow added in v0.9.0

func (r *RingSink) Infow(msg string, kv ...any)

Infow logs an info message with key-value fields.

func (*RingSink) Snapshot added in v0.9.0

func (r *RingSink) Snapshot() []Entry

Snapshot returns the retained entries oldest-first as a fresh slice.

func (*RingSink) Sync added in v0.9.0

func (r *RingSink) Sync() error

Sync is a no-op (in-memory).

func (*RingSink) Warnw added in v0.9.0

func (r *RingSink) Warnw(msg string, kv ...any)

Warnw logs a warning message with key-value fields.

func (*RingSink) WithFields added in v0.9.0

func (r *RingSink) WithFields(kv ...any) Logger

WithFields returns a Logger that records into the same ring with kv prepended to every entry's fields.

Jump to

Keyboard shortcuts

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