Documentation
¶
Overview ¶
Package logflightrecorder provides a slog.Handler that keeps the last N log records in a circular buffer. It is designed for exposing recent logs via health check or admin HTTP endpoints.
Index ¶
- type Handler
- func (h *Handler) All() iter.Seq[slog.Record]
- func (h *Handler) Capacity() int
- func (h *Handler) Clear()
- func (h *Handler) Enabled(_ context.Context, level slog.Level) bool
- func (h *Handler) Handle(_ context.Context, r slog.Record) error
- func (h *Handler) JSON() ([]byte, error)
- func (h *Handler) Len() int
- func (h *Handler) Records() []slog.Record
- func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler
- func (h *Handler) WithGroup(name string) slog.Handler
- func (h *Handler) WriteTo(w io.Writer) (int64, error)
- type Options
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler is a slog.Handler that stores log records in a fixed-size ring buffer. Handlers returned by Handler.WithAttrs and Handler.WithGroup share the same underlying buffer.
func New ¶
New creates a Handler with the given buffer capacity. It panics if size < 1.
Example ¶
package main
import (
"fmt"
"log/slog"
lfr "github.com/alexrios/logflightrecorder"
)
func main() {
h := lfr.New(100, nil)
logger := slog.New(h)
logger.Info("server started", "port", 8080)
logger.Warn("high latency", "ms", 250)
fmt.Println(h.Len())
}
Output: 2
func (*Handler) All ¶
All returns an iterator over stored records from oldest to newest. The snapshot is taken under a read lock; the iteration itself holds no lock.
Example ¶
package main
import (
"fmt"
"log/slog"
lfr "github.com/alexrios/logflightrecorder"
)
func main() {
h := lfr.New(10, nil)
logger := slog.New(h)
logger.Info("alpha")
logger.Info("beta")
for r := range h.All() {
fmt.Println(r.Message)
}
}
Output: alpha beta
func (*Handler) Handle ¶
Handle stores a clone of r in the ring buffer. If FlushOn/FlushTo are configured and the record's level reaches the FlushOn threshold, all records since the last flush are forwarded to FlushTo.
func (*Handler) JSON ¶
JSON returns the buffered records as a JSON array suitable for HTTP responses.
Example ¶
package main
import (
"fmt"
"log/slog"
lfr "github.com/alexrios/logflightrecorder"
)
func main() {
h := lfr.New(10, &lfr.Options{Level: slog.LevelError})
logger := slog.New(h)
logger.Info("ignored") // below Error level
logger.Error("failure", "code", 500)
data, err := h.JSON()
if err != nil {
panic(err)
}
fmt.Println(h.Len())
fmt.Println(len(data) > 0)
}
Output: 1 true
func (*Handler) Len ¶
Len returns the number of records physically stored in the buffer. It does not apply MaxAge filtering.
func (*Handler) Records ¶
Records returns a snapshot of stored records from oldest to newest. If MaxAge is set, records older than MaxAge are excluded.
Example ¶
package main
import (
"fmt"
"log/slog"
lfr "github.com/alexrios/logflightrecorder"
)
func main() {
h := lfr.New(10, nil)
logger := slog.New(h)
logger.Info("first")
logger.Info("second")
for _, r := range h.Records() {
fmt.Println(r.Message)
}
}
Output: first second
func (*Handler) WithAttrs ¶
WithAttrs returns a new Handler whose records will include the given attrs. The new handler shares the same ring buffer.
func (*Handler) WithGroup ¶
WithGroup returns a new Handler that qualifies later attrs with the given group name. The new handler shares the same ring buffer.
func (*Handler) WriteTo ¶
WriteTo writes the buffered records as a JSON array to w. It implements io.WriterTo, making it composable with [http.ResponseWriter].
Example ¶
package main
import (
"bytes"
"encoding/json"
"fmt"
"log/slog"
lfr "github.com/alexrios/logflightrecorder"
)
func main() {
h := lfr.New(10, nil)
logger := slog.New(h)
logger.Info("first")
logger.Info("second")
var buf bytes.Buffer
if _, err := h.WriteTo(&buf); err != nil {
panic(err)
}
var entries []map[string]any
if err := json.Unmarshal(buf.Bytes(), &entries); err != nil {
panic(err)
}
for _, e := range entries {
fmt.Println(e["msg"])
}
}
Output: first second
type Options ¶
type Options struct {
// Level reports the minimum record level that will be stored.
// The handler discards records with lower levels.
// If Level is nil, the handler assumes LevelInfo.
Level slog.Leveler
// FlushOn sets the level threshold that triggers a flush of buffered records
// to FlushTo. Both FlushOn and FlushTo must be set for flush to be active.
FlushOn slog.Leveler
// FlushTo is the destination handler for flushed records.
// Both FlushOn and FlushTo must be set for flush to be active.
FlushTo slog.Handler
// MaxAge excludes records older than this duration from read operations
// (Records, All, JSON, WriteTo). Zero means no age filtering.
// Len returns the physical count regardless of MaxAge.
MaxAge time.Duration
}
Options configure a Handler.