handler

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2025 License: MIT Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func StyleForLevel added in v1.0.3

func StyleForLevel(l slog.Level) pterm.RGBStyle

StyleForLevel returns a pterm rgb style for the given slog level.

Types

type Builder added in v1.0.3

type Builder struct {
	// contains filtered or unexported fields
}

Builder implements LogBuilder to build formatted log entries.

func NewBuilder added in v1.0.3

func NewBuilder(opts Opts, r *slog.Record) *Builder

NewBuilder creates a new Builder instance for the given record and options.

func (*Builder) Build added in v1.0.3

func (b *Builder) Build() string

Build finalizes the log entry and returns it as a string.

func (*Builder) WriteAttrs added in v1.0.3

func (b *Builder) WriteAttrs()

WriteAttrs appends log attributes and optional caller information.

func (*Builder) WriteLevel added in v1.0.3

func (b *Builder) WriteLevel()

WriteLevel appends the log level label with styling.

func (*Builder) WriteMessage added in v1.0.3

func (b *Builder) WriteMessage()

WriteMessage appends the main log message.

func (*Builder) WriteTime added in v1.0.3

func (b *Builder) WriteTime()

WriteTime appends the formatted log timestamp.

type Handler

type Handler struct {
	// Opts defines configuration options for log formatting and behavior.
	Opts Opts
	// W is the output destination for log entries.
	W io.Writer
}

Handler implements slog.Handler and writes formatted log records.

func (*Handler) Enabled

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

Enabled reports whether the given level is enabled by cfg.Level.

func (*Handler) Handle

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

Handle formats the record as a and writes it to stdout. Output includes timestamp, level, message, attrs, and optional caller info. The context is ignored.

func (*Handler) WithAttrs

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

WithAttrs returns h unchanged

func (*Handler) WithGroup

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

WithGroup returns h unchanged

type LogBuilder added in v1.0.3

type LogBuilder interface {
	// WriteTime writes the formatted timestamp to the log output.
	WriteTime()

	// WriteLevel writes the log level representation.
	WriteLevel()

	// WriteMessage writes the main log message text.
	WriteMessage()

	// WriteAttrs writes structured attributes and optional caller info.
	WriteAttrs()

	// Build finalizes and returns the formatted log string.
	Build() string
}

LogBuilder defines methods for constructing formatted log output.

type Opts

type Opts interface {
	// GetLevel returns the minimum log level for output.
	GetLevel() slog.Level

	// IsCallerEnabled checks if caller information should be included for the given level.
	IsCallerEnabled(level slog.Level) bool

	// GetPadding returns the amount of padding to apply in log messages.
	GetPadding() int

	// GetTimeFormat returns the time format string used for log timestamps.
	GetTimeFormat() string
}

Opts defines the interface for retrieving handler configuration settings.

Jump to

Keyboard shortcuts

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