prefixed

package
v0.0.0-...-75be81d Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ColorizePrefix

func ColorizePrefix(f func(prefixes []slog.Value) string) func(prefixes []slog.Value) string

ColorizePrefix wraps a prefix formatter function to colorize its output with cyan ANSI codes.

func DefaultPrefixFormatter

func DefaultPrefixFormatter(prefixes []slog.Value) string

DefaultPrefixFormatter constructs a prefix string by joining all detected prefix values using ":". A " > " suffix is added at the end of the prefix string.

Types

type Handler

type Handler struct {
	Next slog.Handler // The next log handler in the chain.
	// contains filtered or unexported fields
}

Handler is a custom slog handler that wraps another slog.Handler to prepend a prefix to the log messages. The prefix is sourced from the log record's attributes using the keys specified in PrefixKeys.

func NewHandler

func NewHandler(next slog.Handler, opts *HandlerOptions) *Handler

NewHandler creates a new prefix logging handler. The new handler will prepend a prefix sourced from the log record's attributes to each log message before passing the record to the next handler.

func (*Handler) Enabled

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

func (*Handler) Handle

func (h *Handler) Handle(ctx context.Context, r slog.Record) error

Handle processes a log record, prepending a prefix to its message if needed, and then passes the record to the next handler.

func (*Handler) WithAttrs

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

func (*Handler) WithGroup

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

type HandlerOptions

type HandlerOptions struct {
	PrefixKeys []string // A list of keys used to fetch prefix values from the log record.

	// PrefixFormatter is a function to format the prefix of the log record.
	// If it's not set, the DefaultPrefixFormatter with ColorizePrefix wrapper is used.
	PrefixFormatter func(prefixes []slog.Value) string
}

Jump to

Keyboard shortcuts

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