Documentation
¶
Index ¶
- Constants
- Variables
- func AddToContext(ctx context.Context, attrs ...slog.Attr) context.Context
- func ContextMiddleware(ctx context.Context, r slog.Record) (context.Context, slog.Record)
- func FormatError(a slog.Attr) slog.Attr
- func FormatLogfmt(fields map[string]any) (string, error)
- func MultiReplaceAttribute(fns ...func(slog.Attr) slog.Attr) func(slog.Attr) slog.Attr
- func New(opts ...Options) *slog.Logger
- func NewLevels(a slog.Attr) slog.Attr
- func SetLevel(ctx context.Context, level slog.Level) context.Context
- type ColorScheme
- type FieldFormatter
- type FieldFormatterFunc
- type Format
- type Formatter
- type FormatterFunc
- type JSONFormatter
- type LogfmtFormatter
- type Middleware
- type Options
- type PrettyFormatter
- type Sink
- type Style
Constants ¶
const ( LevelTrace = slog.Level(-8 + 4*iota) LevelDebug LevelInfo LevelWarn LevelError LevelPanic LevelFatal )
Variables ¶
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), }, }
var JSONStyle = Style{ Format: FormatJSON, TimeFormat: time.RFC3339, }
Functions ¶
func AddToContext ¶
AddToContext stores slog attributes in the context for injection by ContextMiddleware.
func ContextMiddleware ¶
ContextMiddleware pulls slog attributes stored in the context and injects them into the record. Use AddToContext to attach attributes to a context.
func FormatError ¶
FormatError is a replaceAttr-compatible function that expands error attributes into a group with msg, cause, and stack frames.
func FormatLogfmt ¶
FormatLogfmt encodes fields as logfmt with dot-flattened nested maps and structs.
func MultiReplaceAttribute ¶
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 ¶
FieldFormatter renders record attributes (for example logfmt key=value pairs).
type FieldFormatterFunc ¶
FieldFormatterFunc adapts a function to FieldFormatter.
func (FieldFormatterFunc) FormatFields ¶
func (f FieldFormatterFunc) FormatFields(fields map[string]any) (string, error)
type FormatterFunc ¶
FormatterFunc adapts a function to Formatter.
type JSONFormatter ¶
type JSONFormatter struct {
TimeFormat string
}
JSONFormatter renders one JSON object per line.
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 ¶
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.