Documentation
¶
Index ¶
- func FormatTextRecord(record slog.Record) string
- func IndentAndQuoteLines(buffer, indent string) string
- func IndentLines(buffer, indent string) string
- func LogLines(logger Printer, buffer, indent string)
- func Logf(log Logger, priority Priority, format string, args ...interface{})
- func NewRecordf(priority Priority, format string, args ...interface{}) slog.Record
- func ResolveAttr(attr slog.Attr) slog.Attr
- func SetCtx(ctx context.Context, l Logger) context.Context
- type Accumulator
- func (dl *Accumulator) Add(prio Priority, format string, args ...interface{})
- func (dl *Accumulator) Debugf(format string, args ...interface{})
- func (dl *Accumulator) Enabled(context.Context, slog.Level) bool
- func (dl *Accumulator) Errorf(format string, args ...interface{})
- func (dl *Accumulator) Forward(log Logger)
- func (dl *Accumulator) ForwardWithPrinter(log Logger, printer func(*ContextRecord, Logger))
- func (dl *Accumulator) Handle(ctx context.Context, record slog.Record) error
- func (dl *Accumulator) Infof(format string, args ...interface{})
- func (dl *Accumulator) Retrieve() []ContextRecord
- func (dl *Accumulator) SetOutput(writer io.Writer)
- func (dl *Accumulator) Shutdown(context.Context) error
- func (dl *Accumulator) Warnf(format string, args ...interface{})
- type ContextRecord
- type DefaultLogger
- func (dl DefaultLogger) Debugf(format string, args ...interface{})
- func (dl DefaultLogger) Enabled(context.Context, slog.Level) bool
- func (dl DefaultLogger) Errorf(format string, args ...interface{})
- func (dl DefaultLogger) Handle(_ context.Context, record slog.Record) error
- func (dl DefaultLogger) Infof(format string, args ...interface{})
- func (dl DefaultLogger) Printf(format string, args ...interface{})
- func (dl DefaultLogger) SetOutput(output io.Writer)
- func (dl DefaultLogger) Shutdown(context.Context) error
- func (dl DefaultLogger) Warnf(format string, args ...interface{})
- type Forwardable
- type IndentedError
- type Logger
- type NilLogger
- func (dl NilLogger) Debugf(format string, args ...interface{})
- func (dl NilLogger) Enabled(context.Context, slog.Level) bool
- func (dl NilLogger) Errorf(format string, args ...interface{})
- func (dl NilLogger) Handle(context.Context, slog.Record) error
- func (dl NilLogger) Infof(format string, args ...interface{})
- func (dl NilLogger) Printf(format string, args ...interface{})
- func (dl NilLogger) SetOutput(output io.Writer)
- func (dl NilLogger) Shutdown(context.Context) error
- func (dl NilLogger) Warnf(format string, args ...interface{})
- type Printer
- type Priority
- type Proxy
- type Structured
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FormatTextRecord ¶
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 ¶
Just like IndentLines, but converts all non-space / non-ascii printable characters to \xHH sequences.
func IndentLines ¶
IndentLines indents each line in the buffer with the specified string.
func LogLines ¶
LogLines breaks a buffer into lines and logs each one of them with the specified indentation and printer.
func NewRecordf ¶
NewRecordf creates a formatted record at the call time.
func ResolveAttr ¶
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.
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) 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) 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) Warnf ¶
func (dl *Accumulator) Warnf(format string, args ...interface{})
type ContextRecord ¶
ContextRecord pairs a log record with the context used to create it.
type DefaultLogger ¶
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) Errorf ¶
func (dl DefaultLogger) Errorf(format string, args ...interface{})
func (DefaultLogger) Handle ¶
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) 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.
var Go Logger = &DefaultLogger{Printer: log.Printf}
Go is a pre-defined logger that will log using default log library.
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.
type NilLogger ¶
type NilLogger struct{}
NilLogger is a logger that discards all messages.
Prefer using logger.Nil to instantiating your copy of &NilLogger{}.
type Printer ¶
type Printer func(format string, args ...interface{})
Printer is a Printf like function.
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.
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.