Documentation
¶
Overview ¶
Package silog implements a structured logger for CLI usage. It's a wrapper around log/slog that provides:
- printf-style functions in addition to structured logging
- additional log levels
- message prefixing
- differently leveled sub-loggers
Index ¶
- Constants
- Variables
- func MaybeQuote(s string) string
- func NonZero[T comparable](name string, value T) slog.Attr
- func Writer(log LeveledLogger, lvl Level) (w io.Writer, done func())
- type ByLevel
- type Level
- type LeveledLogger
- type Logger
- func (l *Logger) Clone() *Logger
- func (l *Logger) Debug(msg string, kvs ...any)
- func (l *Logger) Debugf(format string, args ...any)
- func (l *Logger) Downgrade() *Logger
- func (l *Logger) Error(msg string, kvs ...any)
- func (l *Logger) Errorf(format string, args ...any)
- func (l *Logger) Fatal(msg string, kvs ...any)
- func (l *Logger) Fatalf(format string, args ...any)
- func (l *Logger) Info(msg string, kvs ...any)
- func (l *Logger) Infof(format string, args ...any)
- func (l *Logger) Level() Level
- func (l *Logger) Log(lvl Level, msg string, kvs ...any)
- func (l *Logger) Logf(lvl Level, format string, args ...any)
- func (l *Logger) SetLevel(lvl Level)
- func (l *Logger) Warn(msg string, kvs ...any)
- func (l *Logger) Warnf(format string, args ...any)
- func (l *Logger) With(attrs ...any) *Logger
- func (l *Logger) WithGroup(name string) *Logger
- func (l *Logger) WithLevel(lvl Level) *Logger
- func (l *Logger) WithPrefix(prefix string) *Logger
- type Options
Constants ¶
const ( LevelDebug = Level(slog.LevelDebug) LevelInfo = Level(slog.LevelInfo) LevelWarn = Level(slog.LevelWarn) LevelError = Level(slog.LevelError) LevelFatal = Level(slog.LevelError + 4) )
Supported log levels.
Variables ¶
var Levels = []Level{ LevelDebug, LevelInfo, LevelWarn, LevelError, LevelFatal, }
Levels is a list of all supported log levels.
Functions ¶
func MaybeQuote ¶ added in v0.17.1
MaybeQuote quotes the given string and escapes special characters if it would benefit from quoting. Otherwise, it returns the string unchanged.
func NonZero ¶
func NonZero[T comparable](name string, value T) slog.Attr
NonZero returns a slog attribute for a non-zero value. If the value is zero, the attribute is not included in the log.
func Writer ¶
func Writer(log LeveledLogger, lvl Level) (w io.Writer, done func())
Writer builds and returns an io.Writer that writes messages to the given logger. If the logger is nil, a no-op writer is returned.
The done function must be called when the writer is no longer needed. It will flush any buffered text to the logger.
The returned writer is not thread-safe.
Types ¶
type ByLevel ¶
type ByLevel[T any] struct { Debug T Info T Warn T Error T Fatal T }
ByLevel is a struct that contains fields for each log level.
type LeveledLogger ¶
LeveledLogger is any logger that can log at a specific level.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger is a logger that provided structured and printf-style logging. It supports the following levels: Trace, Debug, Info, Warn, Error. For each level, the logger provides a structured logging method (e.g. Info) and a printf-style method (e.g. Infof).
func New ¶
New creates a new logger that writes to the given writer. Options customize the behavior of the logger if specified.
func (*Logger) Clone ¶
Clone returns a new logger with the same configuration as the original logger.
func (*Logger) Debug ¶
Debug posts a structured log message with the level LevelDebug.
func (*Logger) Debugf ¶
Debugf posts a printf-style log message with the level LevelDebug.
func (*Logger) Downgrade ¶
Downgrade returns a copy of the logger that will downgrade all log messages to the next lower level.
For example, messages logged at LevelInfo will be downgraded to LevelDebug, Levels at the minimum level will be discarded.
func (*Logger) Error ¶
Error posts a structured log message with the level LevelError.
func (*Logger) Errorf ¶
Errorf posts a printf-style log message with the level LevelError.
func (*Logger) Fatal ¶
Fatal posts a structured log message with the level LevelFatal. It also exits the program with a non-zero status code.
func (*Logger) Fatalf ¶
Fatalf posts a printf-style log message with the level LevelFatal. It also exits the program with a non-zero status code.
func (*Logger) SetLevel ¶
SetLevel changes the log level of the logger and all loggers cloned from it.
func (*Logger) WithLevel ¶
WithLevel returns a copy of this logger that will log at the given level.
func (*Logger) WithPrefix ¶
WithPrefix returns a copy of the logger that will add the given prefix to all log messages. Any existing prefix will be replaced with the new one. If the prefix is empty, an existing prefix will be removed. If the prefix is non-empty, a ": " delimiter will be added.
type Options ¶
type Options struct {
// Level is the minimum log level to log.
// It must be one of the supported log levels.
// The default is LevelInfo.
Level Level
// OnFatal is a function that will be called
// when a fatal log message is logged.
//
// This SHOULD stop control flow.
// If it does not, the default implementation will panic.
//
// If unset, the program will exit with a non-zero status code
// when a fatal log message is logged.
OnFatal func() // optional
// Style is the style to use for the logger.
// If unset, the style will be picked based on whether
// the output is a terminal or not.
Style *silog.Style // optional
}
Options defines options for the logger.