Documentation
¶
Overview ¶
Package log wraps slog with CoreC-specific formatting and level control, providing structured logging primitives used throughout the codebase.
Index ¶
- Constants
- Variables
- func Debugln(format string, v ...any)
- func DefaultLogger() core.Logger
- 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
- type SlogLogger
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 DefaultLogger ¶ added in v0.0.4
DefaultLogger returns the standard slog-backed Logger. Components that don't receive an explicit Logger should use this as their default instead of calling slog directly, so they can be tested with a mock Logger without touching the global slog state.
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.
type SlogLogger ¶ added in v0.0.4
type SlogLogger struct{}
SlogLogger adapts the standard log/slog package to the core.Logger port. It lets drivers and other components depend on core.Logger (an interface) while the actual logging goes through slog — the project's concrete backend. In tests, a NoopLogger or custom capture can be injected instead, decoupling the component from slog.
func (SlogLogger) Debug ¶ added in v0.0.4
func (SlogLogger) Debug(msg string, args ...any)
Debug logs at the slog.Debug level.
func (SlogLogger) Error ¶ added in v0.0.4
func (SlogLogger) Error(msg string, args ...any)
Error logs at the slog.Error level.
func (SlogLogger) Info ¶ added in v0.0.4
func (SlogLogger) Info(msg string, args ...any)
Info logs at the slog.Info level.
func (SlogLogger) Warn ¶ added in v0.0.4
func (SlogLogger) Warn(msg string, args ...any)
Warn logs at the slog.Warn level.