log

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2026 License: AGPL-3.0 Imports: 5 Imported by: 0

Documentation

Overview

Package log provides slog-based structured logging for the sqi binaries.

New constructs a *slog.Logger from a level string and format string, registers it as the process-wide default, and returns it for injection into components that need an explicit logger.

Supported formats:

  • "json" (default) — machine-readable JSON, one object per line
  • "text" — human-readable key=value pairs (logfmt-style)

Supported levels: "debug", "info" (default), "warn", "error".

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(level, format string, w io.Writer) (*slog.Logger, error)

New constructs a *slog.Logger from a level string and format string, sets it as the process-wide slog default, and returns it.

level accepts "debug", "info", "warn", or "error" (case-insensitive). An empty string defaults to "info".

format accepts "json" or "text" (case-insensitive). An empty string or any unrecognized value defaults to "json".

w is the output destination; pass os.Stderr for normal use or a *bytes.Buffer in tests.

func NewWithSink

func NewWithSink(level, format string, w io.Writer, sink Sink) (*slog.Logger, error)

NewWithSink behaves like New (same level/format/destination semantics and SetDefault side effect) but, when sink is non-nil, also forwards every record that passes the level filter to sink. A nil sink is identical to New.

func ParseLevel

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

ParseLevel converts a level string to a slog.Level. Accepted values are "debug", "info", "warn", and "error" (case-insensitive). An empty string returns slog.LevelInfo. Any other value returns an error.

Types

type Sink

type Sink interface {
	Emit(r SinkRecord)
}

Sink receives every log record that passes the configured level, in addition to the normal stderr output.

IMPORTANT: a Sink implementation MUST NOT emit slog records itself (directly or indirectly). Doing so re-enters the fan-out handler and creates an infinite logging loop. Sink errors must be dropped or written straight to a raw io.Writer, never routed back through slog.

type SinkRecord

type SinkRecord struct {
	// Ts is the record time as a UTC RFC3339 timestamp with a fixed-width
	// 9-digit nanosecond fraction ("2006-01-02T15:04:05.000000000Z07:00"), so
	// it sorts lexically. Sink implementations that need a time.Time parse it
	// with that same layout (see internal/diag and internal/worker/diaglog).
	Ts string
	// Level is the slog level string: "DEBUG", "INFO", "WARN", "ERROR".
	Level string
	// Msg is the log message.
	Msg string
	// Attrs holds all attributes (WithAttrs-accumulated + per-record), stringified.
	Attrs map[string]string
}

SinkRecord is the flattened form of a slog record delivered to a Sink.

Jump to

Keyboard shortcuts

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