log

package
v0.22.0 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: AGPL-3.0 Imports: 11 Imported by: 2 modules, 13 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package log provides structured runtime logging with context enrichment, configurable severity thresholds, and pluggable output backends.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterContextEnricher

func RegisterContextEnricher(fn ContextEnricher)

RegisterContextEnricher sets the global context enricher used by all loggers. Typically called from an init() in the package that owns PluginContext.

func SetDefault

func SetDefault(l Logger)

SetDefault atomically replaces the process-wide logger; nil resets it to NewNop.

Types

type Backend

type Backend interface {
	Write(ctx context.Context, record Record) error
	Sync(ctx context.Context) error
}

Backend emits log records to a concrete logging implementation.

type Config

type Config struct {
	Name    string
	Fields  []Field
	Backend Backend
	Level   *LevelController
	Now     func() time.Time
}

Config selects the logger's name, persistent fields, output, threshold, and clock.

type ContextEnricher

type ContextEnricher func(context.Context) []Field

ContextEnricher extracts structured fields from a context. Registered via RegisterContextEnricher to break import cycles.

type Field

type Field struct {
	Key   string
	Value any
}

Field is a structured key/value pair attached to log records.

func Any

func Any(key string, value any) Field

Any attaches value without copying or converting it.

func Bool

func Bool(key string, value bool) Field

Bool attaches a boolean value.

func Duration

func Duration(key string, value time.Duration) Field

Duration attaches a duration without converting it to a string.

func Error

func Error(err error) Field

Error attaches err under the reserved error field key without converting it.

func Int

func Int(key string, value int) Field

Int attaches a native-width integer value.

func Int64

func Int64(key string, value int64) Field

Int64 attaches a signed 64-bit integer value.

func String

func String(key, value string) Field

String attaches a string value without formatting it.

type HCLBackend

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

HCLBackend emits records to an hclog logger.

func NewDefaultHCLBackend

func NewDefaultHCLBackend(name string, level Level) *HCLBackend

NewDefaultHCLBackend creates an hclog backend with its default output options.

func NewHCLBackend

func NewHCLBackend(logger hclog.Logger) *HCLBackend

NewHCLBackend wraps logger, using a null logger when logger is nil.

func (*HCLBackend) Named

func (b *HCLBackend) Named(name string) *HCLBackend

Named returns a backend with a child logger, or this backend for an empty name.

func (*HCLBackend) SetLevel

func (b *HCLBackend) SetLevel(level Level)

SetLevel updates the underlying hclog severity threshold.

func (*HCLBackend) Sync

func (b *HCLBackend) Sync(_ context.Context) error

Sync returns nil because hclog exposes no flush operation.

func (*HCLBackend) Write

func (b *HCLBackend) Write(_ context.Context, record Record) error

Write emits a record through hclog; fatal records log errors without exiting.

type Level

type Level int32

Level is the logging severity level.

const (
	// LevelTrace enables the most detailed diagnostic records.
	LevelTrace Level = iota
	// LevelDebug enables debugging records and more severe records.
	LevelDebug
	// LevelInfo enables informational records and more severe records.
	LevelInfo
	// LevelWarn enables warning records and more severe records.
	LevelWarn
	// LevelError enables error and fatal records.
	LevelError
	// LevelFatal marks fatal conditions without requiring process termination.
	LevelFatal
)

func ParseLevel

func ParseLevel(s string) (Level, error)

ParseLevel accepts case-insensitive severity names and surrounding whitespace. Empty input selects info; "warning" aliases warn. Invalid input returns info and an error.

func (Level) String

func (l Level) String() string

String returns the lowercase severity name, or "unknown" for an invalid level.

type LevelController

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

LevelController manages the effective log level at runtime.

func NewLevelController

func NewLevelController(initial Level) *LevelController

NewLevelController creates a concurrency-safe threshold controller.

func (*LevelController) Enabled

func (c *LevelController) Enabled(level Level) bool

Enabled reports whether level meets the current severity threshold.

func (*LevelController) Level

func (c *LevelController) Level() Level

Level returns the current severity threshold.

func (*LevelController) Set

func (c *LevelController) Set(level Level)

Set atomically replaces the severity threshold.

type Logger

type Logger interface {
	Named(name string) Logger
	With(fields ...Field) Logger
	Enabled(ctx context.Context, level Level) bool
	Log(ctx context.Context, level Level, msg string, fields ...Field)
	Trace(ctx context.Context, msg string, fields ...Field)
	Debug(ctx context.Context, msg string, fields ...Field)
	Info(ctx context.Context, msg string, fields ...Field)
	Warn(ctx context.Context, msg string, fields ...Field)
	Error(ctx context.Context, msg string, fields ...Field)
	Fatal(ctx context.Context, msg string, fields ...Field)
	Tracew(ctx context.Context, msg string, kv ...any)
	Debugw(ctx context.Context, msg string, kv ...any)
	Infow(ctx context.Context, msg string, kv ...any)
	Warnw(ctx context.Context, msg string, kv ...any)
	Errorw(ctx context.Context, msg string, kv ...any)
	Fatalw(ctx context.Context, msg string, kv ...any)
	Sync(ctx context.Context) error
}

Logger is the SDK-wide logging interface for plugin runtime logs.

func Default

func Default() Logger

Default returns the process-wide logger, initially a logger that discards records.

func New

func New(cfg Config) Logger

New creates a logger with an info threshold, null backend, and time.Now by default. Fields are copied; Backend, Level, and values stored in fields remain shared.

func NewNop

func NewNop() Logger

NewNop returns a logger whose backend discards every record.

type MultiSink

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

MultiSink fans out writes to multiple sinks.

func NewMultiSink

func NewMultiSink(sinks ...Sink) *MultiSink

NewMultiSink snapshots the sink list and omits nil sinks.

func (*MultiSink) Write

func (m *MultiSink) Write(ctx context.Context, record Record) error

Write visits every sink in order and joins their errors without stopping early.

type Record

type Record struct {
	Timestamp time.Time
	Level     Level
	Logger    string
	Message   string
	Fields    []Field
}

Record is the normalized representation of a log entry.

func (Record) ToMap

func (r Record) ToMap() map[string]any

ToMap copies fields into a map, then overwrites reserved keys with record metadata. Field values are shared with the record.

type Sink

type Sink interface {
	Write(ctx context.Context, record Record) error
}

Sink receives normalized log records.

type SinkBackend

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

SinkBackend adapts a Sink to Backend.

func NewSinkBackend

func NewSinkBackend(sink Sink) *SinkBackend

NewSinkBackend wraps sink; a nil sink discards records.

func (*SinkBackend) Sync

func (b *SinkBackend) Sync(_ context.Context) error

Sync returns nil because Sink exposes no flush operation.

func (*SinkBackend) Write

func (b *SinkBackend) Write(ctx context.Context, record Record) error

Write forwards the record to the sink and returns its error.

Directories

Path Synopsis
Package logtest provides an in-memory log sink for assertions.
Package logtest provides an in-memory log sink for assertions.

Jump to

Keyboard shortcuts

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