Spaces:
Running
Running
File size: 1,532 Bytes
84ffb7a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | package logs
import (
"context"
"fmt"
"log/slog"
"strings"
)
// StreamHandler wraps another slog.Handler (the real stdout sink, text or JSON)
// and additionally publishes a human-readable rendering of every record to the
// Hub, so the live log stream mirrors the terminal output.
type StreamHandler struct {
inner slog.Handler
hub *Hub
}
// NewStreamHandler wraps inner so handled records are also published to hub.
func NewStreamHandler(inner slog.Handler, hub *Hub) slog.Handler {
return &StreamHandler{inner: inner, hub: hub}
}
func (h *StreamHandler) Enabled(ctx context.Context, level slog.Level) bool {
return h.inner.Enabled(ctx, level)
}
func (h *StreamHandler) Handle(ctx context.Context, r slog.Record) error {
h.hub.Publish(formatRecord(&r))
return h.inner.Handle(ctx, r)
}
func (h *StreamHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
return &StreamHandler{inner: h.inner.WithAttrs(attrs), hub: h.hub}
}
func (h *StreamHandler) WithGroup(name string) slog.Handler {
return &StreamHandler{inner: h.inner.WithGroup(name), hub: h.hub}
}
// formatRecord renders a record as a plain, terminal-friendly line:
//
// 2026-08-18 01:36:00.000 INFO database ready key=value
func formatRecord(r *slog.Record) string {
var b strings.Builder
fmt.Fprintf(&b, "%s %-7s %s", r.Time.Format("2006-01-02 15:04:05.000"), r.Level.String(), r.Message)
r.Attrs(func(a slog.Attr) bool {
if a.Key == "" {
return true
}
b.WriteByte(' ')
b.WriteString(a.String())
return true
})
return b.String()
}
|