Documentation
¶
Overview ¶
Package logger wraps log/slog with console output, daily rotating log files and context propagation.
A Logger embeds *slog.Logger, so the whole slog API is available on it. File output is opt-in through Options.File and is written alongside the console, never instead of it.
Files rotate when the date changes. Old files are kept indefinitely; pruning them is left to logrotate or an equivalent.
Index ¶
Constants ¶
const ( Debug = slog.LevelDebug Info = slog.LevelInfo Warn = slog.LevelWarn Error = slog.LevelError )
Log levels, re-exported so callers need not import log/slog for them.
Variables ¶
This section is empty.
Functions ¶
func SetDefault ¶
func SetDefault(l *Logger)
SetDefault makes l the logger used by the package-level slog functions.
Types ¶
type FileOptions ¶
type FileOptions struct {
// Dir holds the log files. Defaults to "./logs".
Dir string
// Prefix begins each file name. Defaults to "app".
Prefix string
// PerLevel writes each level to its own file rather than one combined
// file.
PerLevel bool
}
FileOptions configures log file output.
type Logger ¶
Logger is a *slog.Logger with optional file output attached.
func FromContext ¶
FromContext returns the Logger carried by ctx, or one wrapping the slog default when ctx carries none.
func New ¶
New creates a new Logger with the given options.
New does not fail: if the log directory cannot be created or a file cannot be opened, the problem is reported on stderr and logging continues to the console alone.
func NewDefault ¶
func NewDefault() *Logger
NewDefault returns a text logger at Info writing to stderr.
func NewWithCombinedFile ¶
NewWithCombinedFile returns a JSON logger at Info writing to stderr and to a single file in dir named after prefix.
func NewWithFile ¶
NewWithFile returns a JSON logger at Info writing to stderr and to one file per level in dir.
type Options ¶
type Options struct {
// Level is the minimum level that is logged. The zero value is Info.
Level Level
// JSON selects JSON output instead of text.
JSON bool
// AddSource includes the source file and line of the log call.
AddSource bool
// Writer receives console output. Defaults to os.Stderr.
Writer io.Writer
// File enables log files. Disabled when nil.
File *FileOptions
}
Options configures a Logger.