log

package
v0.12.19 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrLoggerNameEmpty indicates a logger definition without a name.
	ErrLoggerNameEmpty = errors.New("log: logger name cannot be empty")
	// ErrLoggerNameDuplicate indicates more than one logger used the same name.
	ErrLoggerNameDuplicate = errors.New("log: duplicate logger name")
)
View Source
var (
	// TRACE captures extremely fine-grained diagnostic events.
	TRACE = Severity{Representation: "TRACE", Index: 0, Fatal: false}
	// DEBUG captures verbose diagnostic events above TRACE.
	DEBUG = Severity{Representation: "DEBUG", Index: 10, Fatal: false}
	// INFO is for high-level informational events.
	INFO = Severity{Representation: "INFO", Index: 20, Fatal: false}
	// WARNING indicates a recoverable problem worth operator attention.
	WARNING = Severity{Representation: "WARNING", Index: 30, Fatal: false}
	// ERROR marks failures that typically require action.
	ERROR = Severity{Representation: "ERROR", Index: 40, Fatal: false}
	// CRITICAL signals system-breaking events and is marked fatal.
	CRITICAL = Severity{Representation: "CRITICAL", Index: 50, Fatal: true}
)

Built-in severities ordered by severity index.

View Source
var ErrLoggerClosed = errors.New("log: logger is shut down")

ErrLoggerClosed is returned when submitting to a logger that was shut down.

Functions

func BackendCaller added in v0.11.0

func BackendCaller() (string, string, int, bool)

BackendCaller returns the package, source file (base name) and line number of the calling site that invoked logging, from the perspective of a backend.

It attempts several skip depths to account for different call paths (direct backend usage in tests vs. going through Loggee.Log).

Types

type Backend

type Backend interface {
	// Write emits the entry and returns any backend specific error.
	Write(entry Entry) error
	// NotifyAboutSender allows a backend to adjust formatting/alignment.
	NotifyAboutSender(sender string)
	// NotifyAboutSeverity allows the backend to precalculate severity spacing.
	NotifyAboutSeverity(severity *Severity)
	// Shutdown releases backend resources.
	Shutdown()
	// SetGate configures the minimum severity accepted by the backend.
	SetGate(gate uint)
	// Configure applies backend-wide feature flags.
	Configure(cfg BackendConfig)
}

Backend defines the interface implemented by logging backends such as zerolog, stdlog, or sqlite writers.

type BackendConfig added in v0.11.0

type BackendConfig struct {
	IncludeTimestamp bool
	IncludePackage   bool
	IncludeFile      bool
	IncludeLine      bool
}

BackendConfig controls which optional fields a backend should emit. All backends must respect these flags to remain interchangeable.

func DefaultBackendConfig added in v0.11.0

func DefaultBackendConfig() BackendConfig

DefaultBackendConfig enables the full feature set supported by every backend.

type BackendEntry added in v0.12.0

type BackendEntry struct {
	Backend Backend
	// Config overrides the logger-level backend config. When nil, the logger's
	// configuration is used.
	Config *BackendConfig
}

BackendEntry allows specifying backend-specific configuration when building loggers.

type CallerInfo added in v0.11.0

type CallerInfo struct {
	Package string
	File    string
	Line    int
}

CallerInfo captures metadata about the source location of a log event.

type CommonBackend

type CommonBackend struct {
	// Gate is the minimum severity index the backend will emit.
	Gate uint
	// Config stores the currently applied backend configuration.
	Config BackendConfig
	// contains filtered or unexported fields
}

CommonBackend provides shared helpers such as gate tracking and config.

func (*CommonBackend) Configure added in v0.11.0

func (back *CommonBackend) Configure(cfg BackendConfig)

Configure stores the backend configuration.

func (*CommonBackend) EnsureConfig added in v0.11.0

func (back *CommonBackend) EnsureConfig()

EnsureConfig lazily initializes the configuration to defaults.

func (*CommonBackend) SetGate

func (back *CommonBackend) SetGate(gate uint)

SetGate implements Backend.SetGate for embedders.

type Entry

type Entry struct {

	// Severity references the severity metadata for the event.
	Severity *Severity
	// Message is the human readable text payload.
	Message string
	// Sender identifies the logical component or subsystem.
	Sender string
	// Package contains the originating Go import path package when caller
	// tracking is enabled (for example: example.com/project/subsystem).
	// Loggee computes this field from the call stack and ignores caller input.
	Package string
	// File contains the base filename of the call site.
	// Loggee computes this field from the call stack and ignores caller input.
	File string
	// Line contains the source line number of the call site.
	// Loggee computes this field from the call stack and ignores caller input.
	Line int
	// Meta holds arbitrary key/value pairs to attach to the entry payload.
	Meta map[string]any
	// contains filtered or unexported fields
}

Entry represents a single log line regardless of backend implementation.

func (Entry) Timestamp added in v0.12.13

func (e Entry) Timestamp() time.Time

Timestamp returns the event timestamp captured by Loggee.

type Loggee

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

Loggee coordinates multiple backends and relays log entries to each of them. It serializes all writes through a channel to remain concurrency-safe without relying on mutex locking.

func NewLoggee added in v0.11.0

func NewLoggee(cfg LoggerConfig, backends ...Backend) *Loggee

NewLoggee creates a configured logger that immediately starts its background processing goroutine. Additional backends can still be attached via AddBackend after construction.

func (*Loggee) AddBackend

func (l *Loggee) AddBackend(backend Backend)

AddBackend registers a backend for future log entries.

func (*Loggee) AddBackendWithConfig added in v0.12.0

func (l *Loggee) AddBackendWithConfig(backend Backend, cfg BackendConfig)

AddBackendWithConfig registers a backend using a specific configuration.

func (*Loggee) Log

func (l *Loggee) Log(entry *Entry) error

Log writes the entry to every backend and panics if the severity is fatal. It is shorthand for LogWithOffset(1, entry) so caller metadata skips this wrapper frame.

func (*Loggee) LogWithOffset added in v0.12.11

func (l *Loggee) LogWithOffset(offset int, entry *Entry) error

LogWithOffset writes the entry to every backend and panics if the severity is fatal. offset adds extra caller stack frames on top of LoggerConfig's CallerSkip so wrapper functions can preserve accurate caller metadata. Caller metadata fields on entry (Package/File/Line) are ignored.

func (*Loggee) NotifyAboutDefaults

func (l *Loggee) NotifyAboutDefaults()

NotifyAboutDefaults pre-registers the default severities with each backend.

func (*Loggee) NotifyAboutSenders

func (l *Loggee) NotifyAboutSenders(senders ...string)

NotifyAboutSenders informs backends about known senders so they can prepare alignment or metadata caches.

func (*Loggee) NotifyAboutSeverities

func (l *Loggee) NotifyAboutSeverities(severities ...*Severity)

NotifyAboutSeverities informs backends about available severity metadata.

func (*Loggee) SetGate

func (l *Loggee) SetGate(gate uint)

SetGate updates the severity gate across all backends.

func (*Loggee) Shutdown

func (l *Loggee) Shutdown()

Shutdown instructs every backend to release its resources and stops the logger.

type LoggerConfig added in v0.11.0

type LoggerConfig struct {
	// Name identifies the logger when building multiple instances.
	Name string
	// QueueSize controls the channel buffer length used to serialize writes.
	// When zero, a reasonable default buffer of 64 entries is used.
	QueueSize int
	// CallerSkip controls how many stack frames to skip before resolving
	// caller information. Zero uses the default internal offset.
	// Wrapper layers should call LogWithOffset to add their own frame depth
	// and keep package/file/line metadata accurate.
	CallerSkip int
	// Backend controls the optional fields each backend should emit.
	Backend BackendConfig
	// Gate sets the minimum severity index accepted by the logger (applied to
	// all attached backends). Defaults to zero (no filtering).
	Gate uint
}

LoggerConfig describes how a single logger instance should behave.

type LoggerDefinition added in v0.11.0

type LoggerDefinition struct {
	Config LoggerConfig
	// Backends is kept for backward compatibility and applies the logger-level
	// backend configuration to every backend listed here.
	Backends []Backend
	// BackendEntries allow specifying backend-specific configurations.
	BackendEntries []BackendEntry
}

LoggerDefinition couples a logger configuration with the backends it should drive.

type LoggingSystemConfig added in v0.11.0

type LoggingSystemConfig struct {
	Loggers []LoggerDefinition
}

LoggingSystemConfig enumerates all loggers a process should initialize.

func (LoggingSystemConfig) Build added in v0.11.0

func (cfg LoggingSystemConfig) Build() (map[string]*Loggee, error)

Build instantiates every logger defined in the configuration and returns them keyed by name. Logger names must be unique and non-empty.

type Severity

type Severity struct {
	// Representation is the printable textual label (e.g. "INFO").
	Representation string
	// Index orders severities for comparisons and gating.
	Index uint
	// Fatal indicates that logging at this level should panic after writing.
	Fatal bool
}

Severity describes a log level with a symbolic name, index and fatal flag.

func ParseSeverity added in v0.12.0

func ParseSeverity(name string) (*Severity, bool)

ParseSeverity returns the matching built-in severity by name (case insensitive). This helper primarily serves internal integrations (e.g. scripting); most library users should reference the predefined severities directly.

func (*Severity) Announce

func (severity *Severity) Announce(back Backend)

Announce notifies a backend about this severity so it can adjust formatting.

Source Files

  • Backend.go
  • Callers.go
  • Config.go
  • Entry.go
  • Loggee.go
  • Severity.go

Directories

Path Synopsis
backends

Jump to

Keyboard shortcuts

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