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 ¶
- func FileProblem(file, example string) string
- func New(level, format string, w io.Writer) (*slog.Logger, error)
- func NewWithSink(level, format string, w io.Writer, sink Sink) (*slog.Logger, error)
- func Output(path string, maxSizeMB, maxBackups int) (io.WriteCloser, error)
- func ParseLevel(s string) (slog.Level, error)
- type RotatingFile
- type Sink
- type SinkRecord
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FileProblem ¶ added in v0.4.0
FileProblem describes what makes file unusable as a log file for OpenRotatingFile, or returns "". It must name a file (not end in a path separator, not be an existing directory) in a directory that exists; example is the file name the suggested fix uses. Config validation reports it under log.file, so the messages name that key. An empty file is no problem: it means stderr.
func New ¶
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 ¶
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 Output ¶ added in v0.4.0
func Output(path string, maxSizeMB, maxBackups int) (io.WriteCloser, error)
Output returns the log destination for a configured log file: a RotatingFile when path is set, otherwise stderr behind a Close that does nothing (the process must keep its stderr).
func ParseLevel ¶
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 RotatingFile ¶ added in v0.4.0
type RotatingFile struct {
// contains filtered or unexported fields
}
RotatingFile appends to a log file and rotates it by size: path becomes path.1, path.1 becomes path.2, and so on up to maxBackups, oldest dropped.
Rotation never costs a log line. On Windows a rename fails while another process holds the file open without FILE_SHARE_DELETE (an operator tailing it, a log shipper); the writer then keeps appending to the current file and retries only after another maxBytes of output, rather than retrying on every write or returning the error — slog would drop the record.
func OpenRotatingFile ¶ added in v0.4.0
func OpenRotatingFile(path string, maxSizeMB, maxBackups int) (*RotatingFile, error)
OpenRotatingFile opens (creating or appending to) path, rotating it once it exceeds maxSizeMB megabytes and keeping at most maxBackups rotated files.
func (*RotatingFile) Close ¶ added in v0.4.0
func (r *RotatingFile) Close() error
Close closes the current file. It is idempotent, and it returns nil when a failed reopen left no file open, so a shutdown path can always call it.
func (*RotatingFile) Write ¶ added in v0.4.0
func (r *RotatingFile) Write(p []byte) (int, error)
Write appends p, rotating first when p would take the file past its limit.
A failed reopen (a full disk, a transient lock) loses the record that hit it and returns the error, but leaves the writer without an open file rather than with a closed one: the next Write retries the open, so logging resumes as soon as the condition clears. Write after RotatingFile.Close is an error.
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.
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.