handlers

package
v0.76.2 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package handlers provides simple slog handlers

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewDisabled

func NewDisabled() slog.Handler

NewDisabled returns a handler which never writes anything.

func NewFormat

func NewFormat(formatter func(ctx context.Context, r slog.Record) string, writer io.Writer) slog.Handler

NewFormat returns a handler that formats records and writes them to an io.Writer.

func NewLevel

func NewLevel(lvl slog.Leveler, innerHandler slog.Handler) slog.Handler

NewLevel returns a handler that filters logs based on a level.

func NewLocking

func NewLocking(inner slog.Handler) slog.Handler

NewLocking returns a new locking handler that wraps the given inner handler. Only the Handle method is synchronized, Enabled is not.

func NewMulti

func NewMulti(handlers ...slog.Handler) slog.Handler

NewMulti creates a new Handler that writes to multiple handlers.

Types

type Async

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

Async is a slog handler that asynchronously writes logs to another slog handler.

func NewAsync

func NewAsync(innerHandler slog.Handler) *Async

NewAsync creates a new Async handler.

The Async must be closed to avoid leaks.

When using WithAttrs or WithGroup, a single goroutine and queue is still used to write the messages, to guarantee that the log messages are still handled in correct order and the inner handler is not called concurrently. It also avoids leaks by ensuring we don't have to close handlers returned by WithAttrs or WithGroup. For performance, we could have a different goroutine and queue for each derived handler.

func (*Async) Close

func (h *Async) Close()

Close closes the handler.

func (*Async) Enabled

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

Enabled returns true if the handler is enabled for the given level.

func (*Async) Flush

func (h *Async) Flush()

Flush writes all messages in the queue to the inner handler.

func (*Async) Handle

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

Handle writes a record to the handler.

func (*Async) WithAttrs

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

WithAttrs returns a new handler with the given attributes.

func (*Async) WithGroup

func (h *Async) WithGroup(_name string) slog.Handler

WithGroup returns a new handler with the given group name.

Jump to

Keyboard shortcuts

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