Documentation
¶
Overview ¶
Package weftslognats provides a slog.Handler that fans logs out to both stderr (or any base handler) and a NATS subject. Errors and warnings published on NATS are what weft-doctor's NATSIngester subscribes to ; the stderr path stays untouched so existing tooling (operator tailing, kubectl-like CLI sniff, journald) keeps working.
Wire it once in main.go :
conn, _ := nats.Connect("nats://nats.weft.svc:4222")
log := slog.New(weftslognats.NewHandler(weftslognats.Options{
Base: slog.NewJSONHandler(os.Stderr, nil),
Conn: conn,
Subject: "weft.agent." + hostUUID + ".log",
}))
slog.SetDefault(log)
Everything that already calls slog.{Info,Warn,Error} keeps working unchanged ; WARN+ERROR records additionally flow onto NATS in slog.JSONHandler-compatible format so weft-doctor can parse them.
Index ¶
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 fan-out. Exported so callers can type- assert if needed ; constructed via NewHandler.
func NewHandler ¶
NewHandler builds the fan-out. Panics on nil Base — see Options.Base rationale.
func (*Handler) Enabled ¶
Enabled : the union of base + nats. Anything either side wants to see, we evaluate. In practice base usually allows INFO+ ; nats allows WARN+ ; the union is INFO+.
func (*Handler) Handle ¶
Handle dispatches the record to base (always) and to NATS (when conn != nil AND lvl >= minLevel). Errors from base are propagated ; NATS publish errors are surfaced via OnPublishError and otherwise swallowed — we never want logging itself to fail the caller.
type Options ¶
type Options struct {
// Base receives every record (stderr / journald / whatever the
// operator wired). Required ; nil panics — there's no use case
// for "publish to NATS only", and silent drop on a malformed
// config would mask outages.
Base slog.Handler
// Conn is the NATS connection. nil is ALLOWED and means
// "publish nowhere, Base only" : useful for dev, tests, and
// graceful degrade when NATS is unreachable at startup.
Conn Publisher
// Subject is the NATS subject WARN+ERROR records are published
// to. Required when Conn is set. Convention :
// weft.<component>.<host_id>.log
// e.g. weft.agent.dc1-r1-h1.log, weft.microvm-agent.<vm_uuid>.log.
Subject string
// MinLevel is the floor at which NATS publishing kicks in.
// Default slog.LevelWarn — keeps the diagnosis pipeline focused
// on actionable signal, not DEBUG/INFO chatter.
MinLevel slog.Level
// OnPublishError is called when a NATS publish fails. Default :
// no-op (we don't want logging to itself cascade). Set to a
// callback when you want visibility (e.g. bump a Prom metric).
OnPublishError func(err error)
}
Options configures the handler.