logger
import "github.com/TheManticoreProject/Manticore/logger"
Package logger is Manticore’s logging facility. It is built on log/slog: a custom slog.Handler renders the human-readable “[timestamp] LEVEL: message” format, adds colour to level tags, can strip ANSI sequences, and can tee output to a file.
Following the standard-library pattern (slog.Default / slog.New), the package exposes convenience functions backed by a default logger holding the global configuration (Info, Infof, SetLevel, SetOutput, SetNoColors, LogToFile, …), and a Logger type that can be constructed with New and passed around for isolated configuration.
The same method set is available with and without timestamps: the package-level functions timestamp their output, and Plain (which shares the same configuration) does not — logger.Info(“x”) vs logger.Plain.Info(“x”).
Index
- Constants
- Variables
- func CloseLogFile() error
- func DatePrintf(format string, message …any)
- func DatePrintfMicroseconds(format string, message …any)
- func DatePrintfMilliseconds(format string, message …any)
- func DatePrintfNanoseconds(format string, message …any)
- func Debug(msg string)
- func DebugMicroseconds(message string)
- func DebugMilliseconds(message string)
- func DebugNanoseconds(message string)
- func Debugf(format string, a …any)
- func Error(msg string)
- func ErrorMicroseconds(message string)
- func ErrorMilliseconds(message string)
- func ErrorNanoseconds(message string)
- func Errorf(format string, a …any)
- func Info(msg string)
- func InfoMicroseconds(message string)
- func InfoMilliseconds(message string)
- func InfoNanoseconds(message string)
- func Infof(format string, a …any)
- func Lock()
- func LogToFile(path string) error
- func Print(msg string)
- func PrintMicroseconds(message string)
- func PrintMilliseconds(message string)
- func PrintNanoseconds(message string)
- func Printf(format string, a …any)
- func SetColorMode(m ColorMode)
- func SetLevel(l Level)
- func SetNoColors(b bool)
- func SetOutput(w io.Writer)
- func SetTimePrecision(p TimePrecision)
- func SetUTC(b bool)
- func Unlock()
- func Warn(msg string)
- func WarnMicroseconds(message string)
- func WarnMilliseconds(message string)
- func WarnNanoseconds(message string)
- func Warnf(format string, a …any)
- type ColorMode
- type Level
- type Logger
- func Default() *Logger
- func New(opts …Option) *Logger
- func (l *Logger) Debug(msg string)
- func (l *Logger) Debugf(format string, a …any)
- func (l *Logger) Error(msg string)
- func (l *Logger) Errorf(format string, a …any)
- func (l *Logger) Info(msg string)
- func (l *Logger) Infof(format string, a …any)
- func (l *Logger) Print(msg string)
- func (l *Logger) Printf(format string, a …any)
- func (l *Logger) Slog() *slog.Logger
- func (l *Logger) Warn(msg string)
- func (l *Logger) Warnf(format string, a …any)
- func (l *Logger) WithoutTimestamp() *Logger
- type Option
- type TimePrecision
Constants
const (
LevelDebug = slog.LevelDebug // -4
LevelInfo = slog.LevelInfo // 0
LevelWarn = slog.LevelWarn // 4
LevelError = slog.LevelError // 8
// LevelPrint is above all others so Print output is never filtered and carries no
// level tag.
LevelPrint = slog.Level(12)
)
Variables
LoggerLock and Lock/Unlock are retained for compatibility. Logging is now internally synchronised, so they are no longer required to avoid interleaved output.
var LoggerLock sync.Mutex
var (
// Plain logs through the same configuration as the default logger but without a
// timestamp prefix (the "same functions without timestamps").
Plain = &Logger{sl: slog.New(&handler{cfg: sharedConfig, timestamp: false}), cfg: sharedConfig}
)
func CloseLogFile
func CloseLogFile() error
CloseLogFile stops file logging and closes the current log file, if any.
func DatePrintf
func DatePrintf(format string, message ...any)
func DatePrintfMicroseconds
func DatePrintfMicroseconds(format string, message ...any)
func DatePrintfMilliseconds
func DatePrintfMilliseconds(format string, message ...any)
func DatePrintfNanoseconds
func DatePrintfNanoseconds(format string, message ...any)
func Debug
func Debug(msg string)
func DebugMicroseconds
func DebugMicroseconds(message string)
func DebugMilliseconds
func DebugMilliseconds(message string)
func DebugNanoseconds
func DebugNanoseconds(message string)
func Debugf
func Debugf(format string, a ...any)
func Error
func Error(msg string)
func ErrorMicroseconds
func ErrorMicroseconds(message string)
func ErrorMilliseconds
func ErrorMilliseconds(message string)
func ErrorNanoseconds
func ErrorNanoseconds(message string)
func Errorf
func Errorf(format string, a ...any)
func Info
func Info(msg string)
func InfoMicroseconds
func InfoMicroseconds(message string)
func InfoMilliseconds
func InfoMilliseconds(message string)
func InfoNanoseconds
func InfoNanoseconds(message string)
func Infof
func Infof(format string, a ...any)
func Lock
func Lock()
func LogToFile
func LogToFile(path string) error
LogToFile additionally writes every log line to path (ANSI sequences always stripped, so the file is plain text). A previously opened log file is closed first. The file is opened for append.
func Print
func Print(msg string)
func PrintMicroseconds
func PrintMicroseconds(message string)
func PrintMilliseconds
func PrintMilliseconds(message string)
func PrintNanoseconds
func PrintNanoseconds(message string)
func Printf
func Printf(format string, a ...any)
func SetColorMode
func SetColorMode(m ColorMode)
SetColorMode sets when colour is emitted: ColorAuto (terminal only, the default), ColorAlways, or ColorNever.
func SetLevel
func SetLevel(l Level)
SetLevel sets the minimum level emitted by the default logger.
func SetNoColors
func SetNoColors(b bool)
SetNoColors is a convenience over SetColorMode: true selects ColorNever (no colour, ANSI stripped), false restores ColorAuto (colour only when the output is a terminal).
func SetOutput
func SetOutput(w io.Writer)
SetOutput sets the primary writer of the default logger (and Plain). Default: stderr. Whether colour is emitted in ColorAuto mode is re-evaluated against the new writer.
func SetTimePrecision
func SetTimePrecision(p TimePrecision)
SetTimePrecision sets the timestamp precision of the default logger.
func SetUTC
func SetUTC(b bool)
SetUTC selects UTC timestamps for the default logger.
func Unlock
func Unlock()
func Warn
func Warn(msg string)
func WarnMicroseconds
func WarnMicroseconds(message string)
func WarnMilliseconds
func WarnMilliseconds(message string)
func WarnNanoseconds
func WarnNanoseconds(message string)
func Warnf
func Warnf(format string, a ...any)
type ColorMode
ColorMode controls when colour (and the ANSI it implies) is emitted.
type ColorMode int
const (
// ColorAuto emits colour only when the output is a terminal (the default).
ColorAuto ColorMode = iota
// ColorAlways always emits colour.
ColorAlways
// ColorNever never emits colour and strips ANSI sequences from the output.
ColorNever
)
type Level
Level is a logging level (an alias of slog.Level so the slog ecosystem interoperates).
type Level = slog.Level
type Logger
Logger is a logging handle. Use New to create one, or the package-level functions for the default logger.
type Logger struct {
// contains filtered or unexported fields
}
func Default
func Default() *Logger
Default returns the package default logger.
func New
func New(opts ...Option) *Logger
New creates an independent logger with its own configuration (default: stderr, all levels, microsecond timestamps, colours on). Pass it where isolated logging is needed.
func (*Logger) Debug
func (l *Logger) Debug(msg string)
func (*Logger) Debugf
func (l *Logger) Debugf(format string, a ...any)
func (*Logger) Error
func (l *Logger) Error(msg string)
func (*Logger) Errorf
func (l *Logger) Errorf(format string, a ...any)
func (*Logger) Info
func (l *Logger) Info(msg string)
func (*Logger) Infof
func (l *Logger) Infof(format string, a ...any)
func (*Logger) Print
func (l *Logger) Print(msg string)
func (*Logger) Printf
func (l *Logger) Printf(format string, a ...any)
func (*Logger) Slog
func (l *Logger) Slog() *slog.Logger
Slog returns the underlying *slog.Logger for interoperability with slog-based code.
func (*Logger) Warn
func (l *Logger) Warn(msg string)
func (*Logger) Warnf
func (l *Logger) Warnf(format string, a ...any)
func (*Logger) WithoutTimestamp
func (l *Logger) WithoutTimestamp() *Logger
WithoutTimestamp returns a logger that shares this logger’s configuration but emits no timestamp. (Plain is the package default’s no-timestamp sibling.)
type Option
Option configures a Logger created with New.
type Option func(*config, *handler)
func WithColorMode
func WithColorMode(m ColorMode) Option
func WithLevel
func WithLevel(l Level) Option
func WithNoColors
func WithNoColors(b bool) Option
WithNoColors disables colour (ColorNever) when true, otherwise restores ColorAuto.
func WithOutput
func WithOutput(w io.Writer) Option
func WithTimePrecision
func WithTimePrecision(p TimePrecision) Option
func WithTimestamp
func WithTimestamp(b bool) Option
func WithUTC
func WithUTC(b bool) Option
type TimePrecision
TimePrecision selects the fractional precision of the timestamp.
type TimePrecision int
const (
Seconds TimePrecision = iota
Milliseconds
Microseconds
Nanoseconds
)