Documentation
¶
Overview ¶
Package slogconsolehandler implements a zero-dependency slog.Handler that writes colorized logs to console.
Its output format is friendly for human reading in console. The output format can be configured using HandlerOptions, which is a drop-in replacement for slog.HandlerOptions.
Usage:
// Use the default handler. slog.SetDefault(slog.New(slogconsolehandler.Default)) // Or, use custom HandlerOptions. slog.SetDefault(slog.New(slogconsolehandler.New(os.Stderr, &slogconsolehandler.HandlerOptions{ // ... })))
Index ¶
Constants ¶
This section is empty.
Variables ¶
var Default = New(os.Stderr, &HandlerOptions{ AddSource: true, Level: levelVar, ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr { switch a.Key { case slog.TimeKey: if a.Value.Kind() == slog.KindTime { if t, ok := a.Value.Any().(time.Time); ok { if t.IsZero() { return slog.Attr{} } return slog.String(slog.TimeKey, FormatTimeShort(t)) } } case slog.SourceKey: if a.Value.Kind() == slog.KindAny { if s, ok := a.Value.Any().(*slog.Source); ok && s != nil { if s.File == "" { return slog.Attr{} } return slog.String(slog.SourceKey, FormatSourceShort(*s)) } } } return a }, DisableColor: false, })
Default is a default handler configured at debug level, color is enabled, time and source are formatted in a short form. The level can be changed on-the-fly by calling SetLevel.
Functions ¶
func FormatSourceShort ¶ added in v0.2.1
FormatSourceShort formats source in a short format.
func FormatTimeShort ¶ added in v0.2.1
FormatTimeShort formats t to format "01/02 15:04:05.000".
Types ¶
type ConsoleHandler ¶
type ConsoleHandler struct {
// contains filtered or unexported fields
}
ConsoleHandler is a slog.Handler that writes log records to console, in a human-friendly format. It prints log message with color if the writer is a terminal that supports color.
func New ¶
func New(w io.Writer, opts *HandlerOptions) *ConsoleHandler
New creates a ConsoleHandler that writes to w, using the given options. If opts is nil, the default options are used. By default, color is enabled if w is a terminal and supports color.
type HandlerOptions ¶
type HandlerOptions struct { // AddSource causes the handler to compute the source code position // of the log statement and add a SourceKey attribute to the output. AddSource bool // Level reports the minimum record level that will be logged. // The handler discards records with lower levels. // If Level is nil, the handler assumes LevelInfo. // The handler calls Level.Level for each record processed; // to adjust the minimum level dynamically, use a LevelVar. Level slog.Leveler // ReplaceAttr is called to rewrite each non-group attribute before it is logged. // The attribute's value has been resolved (see [Value.Resolve]). // If ReplaceAttr returns a zero Attr, the attribute is discarded. // // See slog.HandlerOptions for more details. ReplaceAttr func(groups []string, a slog.Attr) slog.Attr // DisableColor disables color output. DisableColor bool }
HandlerOptions are options for a ConsoleHandler.