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 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 Format
- type Middleware
- type Options
- 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 ¶ added in v0.5.0
AddToContext stores slog attributes in the context for injection by ContextMiddleware.
func ContextMiddleware ¶ added in v0.8.0
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
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 New ¶ added in v0.5.0
New creates a *slog.Logger. Without arguments, outputs colored pretty-print to stdout.
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 Middleware ¶ added in v0.8.0
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 Style ¶ added in v0.8.0
type Style struct {
Format Format
TimeFormat string
Colors ColorScheme
}
Style controls the visual presentation of a log sink.