logger

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2026 License: MIT Imports: 9 Imported by: 0

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

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

func WithContext

func WithContext(ctx context.Context, l *Logger) context.Context

WithContext returns a copy of ctx carrying l.

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 Level

type Level = slog.Level

Level is an alias for slog.Level.

type Logger

type Logger struct {
	*slog.Logger
	// contains filtered or unexported fields
}

Logger is a *slog.Logger with optional file output attached.

func FromContext

func FromContext(ctx context.Context) *Logger

FromContext returns the Logger carried by ctx, or one wrapping the slog default when ctx carries none.

func New

func New(opts Options) *Logger

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 NewJSON

func NewJSON() *Logger

NewJSON returns a JSON logger at Info writing to stderr.

func NewWithCombinedFile

func NewWithCombinedFile(dir, prefix string) *Logger

NewWithCombinedFile returns a JSON logger at Info writing to stderr and to a single file in dir named after prefix.

func NewWithFile

func NewWithFile(dir string) *Logger

NewWithFile returns a JSON logger at Info writing to stderr and to one file per level in dir.

func With

func With(l *Logger, args ...any) *Logger

With returns a copy of l that adds args to every record.

func (*Logger) Close

func (l *Logger) Close() error

Close closes any open log files. It is safe to call more than once, and on a Logger without file output.

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.

Jump to

Keyboard shortcuts

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