logbuf

package
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package logbuf provides an in-memory ring buffer for structured log entries. It implements io.Writer for use with zerolog.MultiLevelWriter.

Index

Constants

View Source
const DefaultCapacity = 10000

DefaultCapacity is the default number of log entries the ring buffer holds.

Variables

This section is empty.

Functions

func EntryMatchesQuery

func EntryMatchesQuery(e LogEntry, query string) bool

EntryMatchesQuery returns true if any of the entry's text fields contain the query (case-insensitive).

func LevelAtLeast

func LevelAtLeast(entryLevel, minLevel string) bool

LevelAtLeast returns true if entryLevel is at least as severe as minLevel.

Types

type LogEntry

type LogEntry struct {
	Timestamp int64          `json:"timestamp"`
	Level     string         `json:"level"`
	Message   string         `json:"message"`
	Fields    map[string]any `json:"fields,omitempty"`
	Raw       string         `json:"raw"`
}

LogEntry represents a single structured log entry.

type RingBuffer

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

RingBuffer is a thread-safe circular buffer of log entries with pub/sub support.

func NewRingBuffer

func NewRingBuffer(capacity int) *RingBuffer

NewRingBuffer creates a new ring buffer with the given capacity. If capacity <= 0, DefaultCapacity is used.

func (*RingBuffer) Snapshot

func (rb *RingBuffer) Snapshot(n int, minLevel string, query string) []LogEntry

Snapshot returns the last n entries, optionally filtered by minimum level and query string. If n <= 0 or n > available entries, all available entries are returned. Entries are returned in chronological order (oldest first).

func (*RingBuffer) Subscribe

func (rb *RingBuffer) Subscribe() (chan LogEntry, func())

Subscribe returns a channel that receives new log entries and an unsubscribe function. The channel has a buffer of 64 entries; slow consumers will miss entries.

func (*RingBuffer) Write

func (rb *RingBuffer) Write(p []byte) (int, error)

Write implements io.Writer for zerolog. Each call receives one complete JSON log line. It parses the JSON into a LogEntry and appends it to the ring buffer.

Jump to

Keyboard shortcuts

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