logger

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewRunID

func NewRunID() string

NewRunID generates a random 12-hex-character run identifier.

func WithContext

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

WithContext returns a derived context carrying the logger.

Types

type Logger

type Logger interface {
	Debug(msg string, keyvals ...any)
	Info(msg string, keyvals ...any)
	Warn(msg string, keyvals ...any)
	Error(msg string, keyvals ...any)
	With(keyvals ...any) Logger
}

Logger is a small facade over the underlying logging backend. Methods accept a message (event name in snake_case) and structured key/value fields.

func Fanout

func Fanout(sinks ...Logger) Logger

Fanout returns a Logger that forwards every log call to all provided sinks. If only one sink is provided, that sink is returned unchanged. This does not modify the behavior or configuration of the provided loggers; it simply aggregates them.

func FromContext

func FromContext(ctx context.Context) Logger

FromContext returns the logger from context or a no-op logger if absent.

func New

func New(opts Options) (Logger, io.Closer, error)

New constructs a Logger according to Options. It may create an additional file sink when Options.LogFile is provided. The returned closer should be invoked on process exit to flush/close any resources (it is a no-op if nil).

func Nop

func Nop() Logger

Nop returns a Logger that discards all logs.

type Options

type Options struct {
	// Out is the primary destination for human-facing logs. Defaults to os.Stderr.
	Out io.Writer
	// Level is one of: "debug", "info", "warn", "error". Defaults to "info".
	Level string
	// Format controls primary output: "auto" (default), "pretty", or "json".
	// When "auto", TTY → pretty; non-TTY → json.
	Format string
	// NoColor disables color in pretty output. For JSON it has no effect.
	NoColor bool
	// LogFile, when set, enables an additional JSON sink written to this path.
	LogFile string
	// ReportTimestamp toggles timestamps on the primary sink. Default: true.
	ReportTimestamp *bool
}

Options controls logger construction.

type Step

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

Step is a helper for emitting started/ok/failed events with consistent keys.

func StartStep

func StartStep(l Logger, action string, resource string, extra ...any) *Step

StartStep logs a started event and returns a Step that can be finalized with OK/Fail. Stable keys: action, target, status, changed, duration_ms.

func (*Step) Fail

func (s *Step) Fail(err error, extra ...any) error

Fail logs the failure once with error details and returns the provided error unchanged.

func (*Step) OK

func (s *Step) OK(changed bool, extra ...any)

OK marks the step as successful. Provide whether a change occurred and any extra fields.

Jump to

Keyboard shortcuts

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