log

package
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 25, 2026 License: MIT Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const DefaultLogBufferSize = 1024

DefaultLogBufferSize is the default capacity of the source log event channel.

View Source
const LevelSilent = slog.LevelError + 10

LevelSilent suppresses all logging. It is higher than slog.LevelError so no record passes the Enabled check.

Variables

View Source
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 Debugln

func Debugln(format string, v ...any)

Debugln logs a message at DEBUG level.

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 Errorln

func Errorln(format string, v ...any)

Errorln logs a message at ERROR level.

func Infoln

func Infoln(format string, v ...any)

Infoln logs a message at INFO level.

func Init

func Init(l slog.Level, format string)

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 Level

func Level() slog.Level

Level returns the current global log level.

func ParseLevel

func ParseLevel(s string) (slog.Level, bool)

ParseLevel converts a string to an slog.Level. Returns ok=false for unrecognized strings.

func SetLevel

func SetLevel(l slog.Level)

SetLevel sets the global log level.

func Subscribe

func Subscribe() (ch <-chan Event, unsub func())

Subscribe returns a channel of log events and an unsubscribe function.

func SubscribeWithBuffer

func SubscribeWithBuffer(size int) (ch <-chan Event, unsub func())

SubscribeWithBuffer is like Subscribe but lets the caller choose the subscriber channel buffer size.

func Warnln

func Warnln(format string, v ...any)

Warnln logs a message at WARNING level.

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

func NewObservableHandler(w io.Writer, l slog.Level, format string) *ObservableHandler

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) Enabled

func (h *ObservableHandler) Enabled(_ context.Context, lvl slog.Level) bool

Enabled implements slog.Handler.

func (*ObservableHandler) Handle

func (h *ObservableHandler) Handle(ctx context.Context, r slog.Record) error

Handle implements slog.Handler. It writes to the inner handler and publishes an Event to the log bus.

func (*ObservableHandler) WithAttrs

func (h *ObservableHandler) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs implements slog.Handler.

func (*ObservableHandler) WithGroup

func (h *ObservableHandler) WithGroup(name string) slog.Handler

WithGroup implements slog.Handler.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL