zlog

package module
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: May 22, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LevelTrace = slog.Level(-8 + 4*iota)
	LevelDebug
	LevelInfo
	LevelWarn
	LevelError
	LevelPanic
	LevelFatal
)

Variables

View Source
var DefaultStyle = Style{
	Format:     FormatPretty,
	TimeFormat: "[_2/01 15:04:05Z07]",
	Colors: ColorScheme{
		Levels: map[slog.Level]*color.Color{
			LevelTrace: color.RGB(67, 120, 198),
			LevelDebug: color.RGB(65, 185, 198),
			LevelInfo:  color.RGB(134, 180, 249),
			LevelWarn:  color.RGB(252, 172, 75),
			LevelError: color.RGB(252, 99, 75),
			LevelPanic: color.RGB(216, 52, 0),
			LevelFatal: color.RGB(73, 0, 0),
		},
		Time:    color.New(color.FgCyan, color.Italic),
		Message: color.RGB(170, 170, 170),
		Body:    color.New(color.FgHiWhite, color.Italic),
	},
}
View Source
var JSONStyle = Style{
	Format:     FormatJSON,
	TimeFormat: time.RFC3339,
}

Functions

func AddToContext

func AddToContext(ctx context.Context, attrs ...slog.Attr) context.Context

AddToContext stores slog attributes in the context for injection by ContextMiddleware.

func ContextMiddleware

func ContextMiddleware(ctx context.Context, r slog.Record) (context.Context, slog.Record)

ContextMiddleware pulls slog attributes stored in the context and injects them into the record. Use AddToContext to attach attributes to a context.

func FormatError

func FormatError(a slog.Attr) slog.Attr

FormatError is a replaceAttr-compatible function that expands error attributes into a group with msg, cause, and stack frames.

func FormatLogfmt

func FormatLogfmt(fields map[string]any) (string, error)

FormatLogfmt encodes fields as logfmt with dot-flattened nested maps and structs.

func MultiReplaceAttribute

func MultiReplaceAttribute(fns ...func(slog.Attr) slog.Attr) func(slog.Attr) slog.Attr

func New

func New(opts ...Options) *slog.Logger

New creates a *slog.Logger. Without arguments, outputs colored pretty-print to stdout.

func NewLevels

func NewLevels(a slog.Attr) slog.Attr

NewLevels is a function that maps custom level values to their names.

func SetLevel

func SetLevel(ctx context.Context, level slog.Level) context.Context

SetLevel stores a dynamic minimum log level in the context. The handler will use this level instead of its configured minimum.

Types

type ColorScheme

type ColorScheme struct {
	Levels  map[slog.Level]*color.Color
	Time    *color.Color
	Message *color.Color
	Body    *color.Color
}

ColorScheme defines the color palette.

type FieldFormatter

type FieldFormatter interface {
	FormatFields(fields map[string]any) (string, error)
}

FieldFormatter renders record attributes (for example logfmt key=value pairs).

type FieldFormatterFunc

type FieldFormatterFunc func(fields map[string]any) (string, error)

FieldFormatterFunc adapts a function to FieldFormatter.

func (FieldFormatterFunc) FormatFields

func (f FieldFormatterFunc) FormatFields(fields map[string]any) (string, error)

type Format

type Format int

Format selects a built-in formatter when Style.Formatter is nil.

const (
	FormatPretty Format = iota
	FormatJSON
)

type Formatter

type Formatter interface {
	Format(r slog.Record, fields map[string]any) (string, error)
}

Formatter renders a complete log line from a record and its attributes.

type FormatterFunc

type FormatterFunc func(r slog.Record, fields map[string]any) (string, error)

FormatterFunc adapts a function to Formatter.

func (FormatterFunc) Format

func (f FormatterFunc) Format(r slog.Record, fields map[string]any) (string, error)

type JSONFormatter

type JSONFormatter struct {
	TimeFormat string
}

JSONFormatter renders one JSON object per line.

func (*JSONFormatter) Format

func (j *JSONFormatter) Format(r slog.Record, fields map[string]any) (string, error)

type LogfmtFormatter

type LogfmtFormatter struct{}

LogfmtFormatter encodes fields as logfmt with dot-flattened nested maps and structs.

func (LogfmtFormatter) FormatFields

func (LogfmtFormatter) FormatFields(fields map[string]any) (string, error)

type Middleware

type Middleware func(ctx context.Context, r slog.Record) (context.Context, slog.Record)

Middleware is the unified pipeline type that replaces both modifiers and replaceAttr functions.

func ReplaceAttributeMiddleware

func ReplaceAttributeMiddleware(fn func(slog.Attr) slog.Attr) Middleware

ReplaceAttributeMiddleware adapts a per-attribute transform function into a Middleware.

type Options

type Options struct {
	// Middleware is applied globally to every record before routing to sinks.
	Middleware []Middleware
	// Sinks defines output destinations.
	Sinks []Sink
}

Options configures the logger.

type PrettyFormatter

type PrettyFormatter struct {
	TimeFormat string
	Colors     ColorScheme
	Fields     FieldFormatter
}

PrettyFormatter renders time, level, message, and formatted fields on one line.

func (*PrettyFormatter) Format

func (p *PrettyFormatter) Format(r slog.Record, fields map[string]any) (string, error)

type Sink

type Sink struct {
	Writer io.Writer
	Level  slog.Level
	Style  Style
}

Sink configures a single log output destination.

type Style

type Style struct {
	Formatter  Formatter
	Format     Format
	TimeFormat string
	Colors     ColorScheme
}

Style controls the visual presentation of a log sink. Set Formatter for full control; otherwise Format, TimeFormat, and Colors configure the built-in pretty or JSON formatter.

func NewStyle

func NewStyle(f Formatter) Style

NewStyle returns a Style that uses f for all rendering.

Jump to

Keyboard shortcuts

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