silog

package
v0.23.0 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2026 License: GPL-3.0 Imports: 14 Imported by: 0

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

View Source
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

Levels is a list of all supported log levels.

Functions

func MaybeQuote added in v0.17.1

func MaybeQuote(s string) string

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.

func (*ByLevel[T]) Get

func (b *ByLevel[T]) Get(lvl Level) T

Get returns the value associated with the given log level.

type Level

type Level slog.Level

Level is a log level.

func (Level) Dec

func (l Level) Dec(n int) Level

Dec lowers the log level by N steps.

func (Level) Level

func (l Level) Level() slog.Level

Level returns the level as a slog.Level.

func (Level) String

func (l Level) String() string

String returns the string representation of the log level.

type LeveledLogger

type LeveledLogger interface {
	Log(lvl Level, msg string, kvs ...any)
}

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

func New(w io.Writer, opts *Options) *Logger

New creates a new logger that writes to the given writer. Options customize the behavior of the logger if specified.

func Nop

func Nop(options ...*Options) *Logger

Nop returns a no-op logger that discards all log messages.

func (*Logger) Clone

func (l *Logger) Clone() *Logger

Clone returns a new logger with the same configuration as the original logger.

func (*Logger) Debug

func (l *Logger) Debug(msg string, kvs ...any)

Debug posts a structured log message with the level LevelDebug.

func (*Logger) Debugf

func (l *Logger) Debugf(format string, args ...any)

Debugf posts a printf-style log message with the level LevelDebug.

func (*Logger) Downgrade

func (l *Logger) Downgrade() *Logger

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

func (l *Logger) Error(msg string, kvs ...any)

Error posts a structured log message with the level LevelError.

func (*Logger) Errorf

func (l *Logger) Errorf(format string, args ...any)

Errorf posts a printf-style log message with the level LevelError.

func (*Logger) Fatal

func (l *Logger) Fatal(msg string, kvs ...any)

Fatal posts a structured log message with the level LevelFatal. It also exits the program with a non-zero status code.

func (*Logger) Fatalf

func (l *Logger) Fatalf(format string, args ...any)

Fatalf posts a printf-style log message with the level LevelFatal. It also exits the program with a non-zero status code.

func (*Logger) Info

func (l *Logger) Info(msg string, kvs ...any)

Info posts a structured log message with the level LevelInfo.

func (*Logger) Infof

func (l *Logger) Infof(format string, args ...any)

Infof posts a printf-style log message with the level LevelInfo.

func (*Logger) Level

func (l *Logger) Level() Level

Level returns the current log level of the logger.

func (*Logger) Log

func (l *Logger) Log(lvl Level, msg string, kvs ...any)

Log logs a message at the given level with the given key-value pairs.

func (*Logger) Logf

func (l *Logger) Logf(lvl Level, format string, args ...any)

Logf logs a message at the given level with the given format and arguments.

func (*Logger) SetLevel

func (l *Logger) SetLevel(lvl Level)

SetLevel changes the log level of the logger and all loggers cloned from it.

func (*Logger) Warn

func (l *Logger) Warn(msg string, kvs ...any)

Warn posts a structured log message with the level LevelWarn.

func (*Logger) Warnf

func (l *Logger) Warnf(format string, args ...any)

Warnf posts a printf-style log message with the level LevelWarn.

func (*Logger) With

func (l *Logger) With(attrs ...any) *Logger

With returns a copy of the logger with the given attributes added.

func (*Logger) WithGroup

func (l *Logger) WithGroup(name string) *Logger

WithGroup returns a copy of the logger with the given group name added.

func (*Logger) WithLevel

func (l *Logger) WithLevel(lvl Level) *Logger

WithLevel returns a copy of this logger that will log at the given level.

func (*Logger) WithPrefix

func (l *Logger) WithPrefix(prefix string) *Logger

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.

Directories

Path Synopsis
Package silogtest provides a logger for testing.
Package silogtest provides a logger for testing.

Jump to

Keyboard shortcuts

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