Documentation
¶
Index ¶
- Constants
- Variables
- func Debugln(format string, v ...any)
- func Dropped() int64
- func Errorln(format string, v ...any)
- func Infoln(format string, v ...any)
- func Init(l slog.Level, format string)
- func InitBuffer(bufferSize int)
- func Level() slog.Level
- func ParseLevel(s string) (slog.Level, bool)
- func SetLevel(l slog.Level)
- func Subscribe() (ch <-chan Event, unsub func())
- func SubscribeWithBuffer(size int) (ch <-chan Event, unsub func())
- func Warnln(format string, v ...any)
- type Event
- type ObservableHandler
Constants ¶
const DefaultLogBufferSize = 1024
DefaultLogBufferSize is the default capacity of the source log event channel.
const LevelSilent = slog.LevelError + 10
LevelSilent suppresses all logging. It is higher than slog.LevelError so no record passes the Enabled check.
Variables ¶
var LevelMapping = map[string]slog.Level{ "debug": slog.LevelDebug, "info": slog.LevelInfo, "warn": slog.LevelWarn, "warning": slog.LevelWarn, "error": slog.LevelError, "silent": LevelSilent, }
LevelMapping maps string log levels to slog.Level values.
Functions ¶
func Dropped ¶
func Dropped() int64
Dropped returns the total number of log events dropped due to slow consumers. This includes both source-channel drops (logCh full) and subscriber fan-out drops (individual subscriber buffer full).
func Init ¶
Init initializes the global slog logger with an observable handler that writes to stdout and publishes events to all subscribers. This ensures ALL slog.Info/slog.Error calls throughout the codebase are captured by the log bus — no code changes needed.
format selects the console output format: "json" produces structured JSON lines via slog.NewJSONHandler; any other value (including the default "") falls back to slog.NewTextHandler. The published Event bus payload is unaffected by format — only the stdout rendering.
func InitBuffer ¶
func InitBuffer(bufferSize int)
InitBuffer initializes the log event channel with the given buffer size. If size <= 0, DefaultLogBufferSize is used. Must be called before Init() or any logging produces events; subsequent calls are no-ops. If never called, the channel is lazily initialized with the default size on first use.
func ParseLevel ¶
ParseLevel converts a string to an slog.Level. Returns ok=false for unrecognized strings.
func Subscribe ¶
func Subscribe() (ch <-chan Event, unsub func())
Subscribe returns a channel of log events and an unsubscribe function.
func SubscribeWithBuffer ¶
SubscribeWithBuffer is like Subscribe but lets the caller choose the subscriber channel buffer size.
Types ¶
type Event ¶
type Event struct {
Level slog.Level `json:"level"`
Type string `json:"type"` // "info", "warning", etc.
Payload string `json:"payload"`
Timestamp time.Time `json:"timestamp"`
}
Event represents a log event for subscribers.
type ObservableHandler ¶
type ObservableHandler struct {
// contains filtered or unexported fields
}
ObservableHandler is a slog.Handler that writes to an underlying handler and simultaneously publishes structured events to the log bus.
func NewObservableHandler ¶
NewObservableHandler creates a new ObservableHandler wrapping a handler selected by format. When format is "json" (case-insensitive) the inner handler is a slog.JSONHandler; otherwise a slog.TextHandler is used. The returned handler publishes Event records to the log bus regardless of the chosen console format.
func (*ObservableHandler) Handle ¶
Handle implements slog.Handler. It writes to the inner handler and publishes an Event to the log bus.