handler

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package handler provides a buffered slog.Handler implementation that transforms log records and writes them to a provided store.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Buffered

type Buffered[T any] struct {
	// contains filtered or unexported fields
}

Buffered is a slog.Handler that buffers log records by transforming them and writing them to a provided store. It is useful for scenarios where logs need to be collected and processed in batches, for example, before sending them to an external service or database.

func NewBuffered

func NewBuffered[T any](transform Transform[T], store store.Store[T], options ...Option) *Buffered[T]

NewBuffered creates and returns a new Buffered handler.

The transform function is used to convert an slog.Record into the specific type T before it is written to the store.

The store is where the transformed logs will be written. It must implement the store.Store[T] interface.

func (*Buffered[T]) Enabled

func (b *Buffered[T]) Enabled(ctx context.Context, l slog.Level) bool

Enabled reports whether the handler will log a record with the given level.

func (*Buffered[T]) Handle

func (b *Buffered[T]) Handle(ctx context.Context, r slog.Record) error

Handle transforms a log record and writes it to the store.

func (*Buffered[T]) WithAttrs

func (b *Buffered[T]) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs implements slog.Handler interface

func (*Buffered[T]) WithGroup

func (b *Buffered[T]) WithGroup(name string) slog.Handler

WithGroup implements slog.Handler interface

type Config

type Config struct {
	Level slog.Level
}

type Option

type Option func(c *Config)

func WithLevel

func WithLevel(l slog.Level) Option

WithLevel returns an Option that sets the logging level for the handler. This allows you to control the verbosity of the logs the handler will process.

type Transform

type Transform[T any] func(r slog.Record, attrs []slog.Attr) (T, error)

Jump to

Keyboard shortcuts

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