slogconsolehandler

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2024 License: MIT Imports: 13 Imported by: 1

README

slog-console-handler

Package slog-console-handler 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.

go get github.com/jxskiss/slog-console-handler@latest

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{
    // ...
})))

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

View Source
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

func FormatSourceShort(s slog.Source) string

FormatSourceShort formats source in a short format.

func FormatTimeShort added in v0.2.1

func FormatTimeShort(t time.Time) string

FormatTimeShort formats t to format "01/02 15:04:05.000".

func SetLevel

func SetLevel(l slog.Level)

SetLevel sets the Default handler's level to l.

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.

func (*ConsoleHandler) Enabled

func (h *ConsoleHandler) Enabled(ctx context.Context, level slog.Level) bool

func (*ConsoleHandler) Handle

func (h *ConsoleHandler) Handle(ctx context.Context, record slog.Record) error

func (*ConsoleHandler) WithAttrs

func (h *ConsoleHandler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*ConsoleHandler) WithGroup

func (h *ConsoleHandler) WithGroup(name string) slog.Handler

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.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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