zlog

package module
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2026 License: MIT Imports: 11 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 added in v0.5.0

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

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

func ContextMiddleware added in v0.8.0

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 added in v0.5.0

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 MultiReplaceAttribute added in v0.8.0

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

func New added in v0.5.0

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

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

func NewLevels added in v0.5.0

func NewLevels(a slog.Attr) slog.Attr

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

func SetLevel added in v0.5.0

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 added in v0.8.0

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

ColorScheme defines the color palette.

type Format added in v0.8.0

type Format int

Format controls how log lines are rendered.

const (
	FormatPretty Format = iota
	FormatJSON
)

type Middleware added in v0.8.0

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 added in v0.8.0

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

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

type Options added in v0.8.0

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 Sink added in v0.8.0

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

Sink configures a single log output destination.

type Style added in v0.8.0

type Style struct {
	Format     Format
	TimeFormat string
	Colors     ColorScheme
}

Style controls the visual presentation of a log sink.

Jump to

Keyboard shortcuts

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