logger

package
v0.0.0-...-ea2487c Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2026 License: BSD-3-Clause, BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatTextRecord

func FormatTextRecord(record slog.Record) string

FormatTextRecord renders a record as a plain message followed by quoted key/value attributes. It omits metadata supplied by transports such as timestamps, levels, and logger names.

func IndentAndQuoteLines

func IndentAndQuoteLines(buffer, indent string) string

Just like IndentLines, but converts all non-space / non-ascii printable characters to \xHH sequences.

func IndentLines

func IndentLines(buffer, indent string) string

IndentLines indents each line in the buffer with the specified string.

func LogLines

func LogLines(logger Printer, buffer, indent string)

LogLines breaks a buffer into lines and logs each one of them with the specified indentation and printer.

func Logf

func Logf(log Logger, priority Priority, format string, args ...interface{})

Logf applies the early enabled check shared by printf-compatible loggers.

func NewRecordf

func NewRecordf(priority Priority, format string, args ...interface{}) slog.Record

NewRecordf creates a formatted record at the call time.

func ResolveAttr

func ResolveAttr(attr slog.Attr) slog.Attr

ResolveAttr recursively resolves slog.LogValuer values and removes empty attributes from groups. Sinks that do not use slog.Handler should call it before formatting attributes so lazy values, including redacted values, are rendered correctly.

func SetCtx

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

Types

type Accumulator

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

Accumulator is a thread safe object implementing the Logger interface that accumulates all messages being logged.

You can then use the Retrieve() or Forward() methods to access or print the accumulated messages.

func NewAccumulator

func NewAccumulator() *Accumulator

func (*Accumulator) Add

func (dl *Accumulator) Add(prio Priority, format string, args ...interface{})

func (*Accumulator) Debugf

func (dl *Accumulator) Debugf(format string, args ...interface{})

func (*Accumulator) Enabled

func (dl *Accumulator) Enabled(context.Context, slog.Level) bool

func (*Accumulator) Errorf

func (dl *Accumulator) Errorf(format string, args ...interface{})

func (*Accumulator) Forward

func (dl *Accumulator) Forward(log Logger)

func (*Accumulator) ForwardWithPrinter

func (dl *Accumulator) ForwardWithPrinter(log Logger, printer func(*ContextRecord, Logger))

func (*Accumulator) Handle

func (dl *Accumulator) Handle(ctx context.Context, record slog.Record) error

func (*Accumulator) Infof

func (dl *Accumulator) Infof(format string, args ...interface{})

func (*Accumulator) Retrieve

func (dl *Accumulator) Retrieve() []ContextRecord

Retrieve atomically drains and returns the accumulated records.

func (*Accumulator) SetOutput

func (dl *Accumulator) SetOutput(writer io.Writer)

func (*Accumulator) Shutdown

func (dl *Accumulator) Shutdown(context.Context) error

func (*Accumulator) Warnf

func (dl *Accumulator) Warnf(format string, args ...interface{})

type ContextRecord

type ContextRecord struct {
	Context context.Context
	slog.Record
}

ContextRecord pairs a log record with the context used to create it.

type DefaultLogger

type DefaultLogger struct {
	Printer Printer
	Setter  func(writer io.Writer)
}

DefaultLogger uses a single Printf to implement the Logger interface.

Printer must be provided. Use log.Printf to rely on default golang logging, with:

logger := &DefaultLoger{Printer: log.Printf}

... or just use the pre-defined `Go` variable bleow for default Go logging.

func (DefaultLogger) Debugf

func (dl DefaultLogger) Debugf(format string, args ...interface{})

func (DefaultLogger) Enabled

func (dl DefaultLogger) Enabled(context.Context, slog.Level) bool

func (DefaultLogger) Errorf

func (dl DefaultLogger) Errorf(format string, args ...interface{})

func (DefaultLogger) Handle

func (dl DefaultLogger) Handle(_ context.Context, record slog.Record) error

Handle preserves record level, message, and attributes. Printer cannot supply a record timestamp, so record.Time is intentionally lost and any timestamp added by the downstream logger reflects output time.

func (DefaultLogger) Infof

func (dl DefaultLogger) Infof(format string, args ...interface{})

func (DefaultLogger) Printf

func (dl DefaultLogger) Printf(format string, args ...interface{})

func (DefaultLogger) SetOutput

func (dl DefaultLogger) SetOutput(output io.Writer)

func (DefaultLogger) Shutdown

func (dl DefaultLogger) Shutdown(context.Context) error

func (DefaultLogger) Warnf

func (dl DefaultLogger) Warnf(format string, args ...interface{})

type Forwardable

type Forwardable interface {
	Forward(Logger)
}

Forwardable is the interface implemented by any logger capable of forwarding, upon request, buffered messages to another logger.

type IndentedError

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

IndentedError reformats the error to have indented new lines.

func NewIndentedError

func NewIndentedError(err error, indent string) *IndentedError

NewIndentedError wraps an error into an object that ensures that when the error is converted to a string and printed, via the Error method, every line of the error message is indented with indent.

For example, let's say you have an error err that converted with fmt.Printf(... %s ...) leads to the following output:

An error occured in executing function, stack trace follows.
  Function1() ...
  Function2() ...

Using NewIndentedError(err, "| "), would lead to:

| An error occured in executing function, stack trace follows.
|   Function1() ...
|   Function2() ...

Further, the error returned by NewIntendedError implements the Unwrap interface, allowing programmatic access to the original error.

func (*IndentedError) Error

func (ie *IndentedError) Error() string

func (*IndentedError) Unwrap

func (ie *IndentedError) Unwrap() error

type Logger

type Logger interface {
	// Use for messages that are pretty much only useful when debugging
	// specific behaviors. Should be used rarely.
	// Expect debug messages to be discarded.
	Debugf(format string, args ...interface{})
	// Use for messages that clearly a) outline a problem that b) the operator MUST act upon.
	// Errors are generally reserved for things that cause the program to fail and must be fixed.
	Errorf(format string, args ...interface{})
	// Use for messages that a) outline a problem that b) is actionable, and c) the operator
	// may want to act upon - even though the program may have recovered or continued without
	// intervention (in degraded mode, or without some functionality, ...).
	Warnf(format string, args ...interface{})
	// Use for messages that are either not actionable, or require no action by the operator.
	Infof(format string, args ...interface{})

	Enabled(context.Context, slog.Level) bool
	Handle(context.Context, slog.Record) error
	SetOutput(writer io.Writer)

	// Callers must stop log-producing goroutines before calling Shutdown and must
	// not use the logger after shutdown begins. A nil result means accepted events
	// were handed to their downstream sinks and resources were closed. A context
	// error only stops the caller from waiting; cleanup may continue, and pending
	// events are not guaranteed if the process exits. The context must not be nil;
	// use context.Background for an unbounded wait.
	Shutdown(context.Context) error
}

Logger is the interface used by the enkit libraries to log messages.

Implementations that do not own resources should implement Shutdown as a no-op. The golang log library is used by default through DefaultLogger, which can also adapt an arbitrary Printf-like function.

Go is a pre-defined logger that will log using default log library.

var Nil Logger = &NilLogger{}

Nil is a pre-defined logger that will discard all the output.

Replacing the Nil global with something else can allow seeing log messages that would normally be discarded.

func GetCtx

func GetCtx(ctx context.Context) Logger

type NilLogger

type NilLogger struct{}

NilLogger is a logger that discards all messages.

Prefer using logger.Nil to instantiating your copy of &NilLogger{}.

func (NilLogger) Debugf

func (dl NilLogger) Debugf(format string, args ...interface{})

func (NilLogger) Enabled

func (dl NilLogger) Enabled(context.Context, slog.Level) bool

func (NilLogger) Errorf

func (dl NilLogger) Errorf(format string, args ...interface{})

func (NilLogger) Handle

func (dl NilLogger) Handle(context.Context, slog.Record) error

func (NilLogger) Infof

func (dl NilLogger) Infof(format string, args ...interface{})

func (NilLogger) Printf

func (dl NilLogger) Printf(format string, args ...interface{})

func (NilLogger) SetOutput

func (dl NilLogger) SetOutput(output io.Writer)

func (NilLogger) Shutdown

func (dl NilLogger) Shutdown(context.Context) error

func (NilLogger) Warnf

func (dl NilLogger) Warnf(format string, args ...interface{})

type Printer

type Printer func(format string, args ...interface{})

Printer is a Printf like function.

type Priority

type Priority = slog.Level

Priority is the severity used by the printf-compatible logging methods.

const (
	DebugPriority Priority = slog.LevelDebug
	InfoPriority  Priority = slog.LevelInfo
	WarnPriority  Priority = slog.LevelWarn
	ErrorPriority Priority = slog.LevelError
)

type Proxy

type Proxy struct {
	Logger
}

Proxy is a simple logger that as long as it is passed by pointer, allows to change the underlying logger implementation.

This allows to initialize some code to use a logging object, to then replace it later after the initialization stage.

func (*Proxy) Replace

func (p *Proxy) Replace(newl Logger)

func (*Proxy) Shutdown

func (p *Proxy) Shutdown(ctx context.Context) error

type Structured

type Structured interface {
	Debug(context.Context, string, ...any)
	Info(context.Context, string, ...any)
	Warn(context.Context, string, ...any)
	Error(context.Context, string, ...any)
}

Structured is the context-aware structured logging API. Arguments after message follow slog key/value conventions; message is not a format string.

func ToStructured

func ToStructured(log Logger) Structured

ToStructured exposes log through the structured logging API. The original Logger retains ownership and must be shut down by its caller.

Directories

Path Synopsis
Package lifecycle owns application policy for waiting on logger shutdown.
Package lifecycle owns application policy for waiting on logger shutdown.

Jump to

Keyboard shortcuts

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