errorlog

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2026 License: GPL-3.0 Imports: 4 Imported by: 0

Documentation

Overview

Package errorlog captures recent WARN/ERROR slog records into a bounded in-memory ring buffer for display in the admin UI. It is intentionally narrow: no persistence, no search, no time-range queries. Deeper investigation belongs in an external log aggregator.

Index

Constants

View Source
const DefaultCapacity = 1000

DefaultCapacity is the ring-buffer size used when NewStore is called with capacity <= 0.

Variables

This section is empty.

Functions

This section is empty.

Types

type Attr

type Attr struct {
	Key   string
	Value string
}

Attr is a key/value pair from the record (or surrounding handler scope). Values are pre-rendered to strings so the UI does not need to know about slog.Value kinds.

type Entry

type Entry struct {
	Time    time.Time
	Level   slog.Level
	Message string
	Attrs   []Attr
}

Entry is a captured slog record flattened for UI rendering.

type Handler

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

Handler wraps a delegate slog.Handler and additionally captures records whose level is >= Level into Store. The delegate handler is always consulted (and invoked if it enables the record) so the existing JSON stderr output keeps working unchanged.

The delegate's own level filter determines what is written to stderr; Level controls what is captured into the ring buffer and may be more permissive.

func NewHandler

func NewHandler(delegate slog.Handler, store *Store, captureLevel slog.Level) *Handler

NewHandler returns a Handler that forwards to delegate and captures records at level >= captureLevel into store.

func (*Handler) Enabled

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

Enabled reports whether either the delegate or the capture path wants the record. Without this, slog short-circuits WARN records when the delegate is set to ERROR, starving the ring buffer.

func (*Handler) Handle

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

Handle forwards the record to the delegate and, if the record meets the capture threshold, appends a flattened copy to the ring buffer.

func (*Handler) WithAttrs

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

WithAttrs returns a new Handler whose captured records carry the supplied attrs in addition to whatever is passed at log time. When called inside a group, the group prefix is baked into the attr keys so later flattening at Handle time produces stable dotted names.

func (*Handler) WithGroup

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

WithGroup returns a new Handler whose record attrs are prefixed with the given group name (joined with "." when nested). Group prefixing is applied at capture time so the flattened Attr keys remain distinguishable.

type Store

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

Store is a bounded ring buffer of captured entries. Writes are non-blocking; the oldest entry is overwritten when the buffer is full.

Store is safe for concurrent use.

func NewStore

func NewStore(capacity int) *Store

NewStore returns a ring buffer sized to capacity. Non-positive values fall back to DefaultCapacity.

func (*Store) Append

func (s *Store) Append(e Entry)

Append stores an entry, evicting the oldest when the buffer is full.

func (*Store) Cap

func (s *Store) Cap() int

Cap returns the ring buffer capacity.

func (*Store) Len

func (s *Store) Len() int

Len returns the number of entries currently in the buffer.

func (*Store) Snapshot

func (s *Store) Snapshot() []Entry

Snapshot returns entries ordered newest-first.

Jump to

Keyboard shortcuts

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