Documentation
¶
Overview ¶
Package log provides structured JSON Lines (JSONL) logging and size-based log file rotation for TaskSmith.
Silent by Default ¶
Because TaskSmith operates both as a background daemon and as an interactive CLI/TUI, accidental writes to standard output or standard error can corrupt terminal UI rendering or machine-readable command output. The package-level default logger therefore writes to io.Discard at LevelInfo until explicitly configured via SetDefault.
Structured JSONL Output ¶
Loggers created with New or NewRotating emit structured JSON Lines records backed by the standard library's log/slog JSON handler. Callers attach strongly typed key-value metadata using Attr constructors such as String, Int, Int64, Uint64, Float64, Bool, Duration, Time, Err, and Any.
Component and Attribute Scoping ¶
Subsystems can derive scoped loggers using Interface.ForComponent (which attaches a standardized "component" attribute) and Interface.With (which pre-binds arbitrary attributes to all subsequent log entries).
Size-Based Rotation and Retention Pruning ¶
RotatingWriter implements a thread-safe io.WriteCloser that writes to an active stream file (<Dir>/<Stream>.jsonl) with 0600 permissions inside a 0700 log directory (defaulting to xdg.LogsDir). Before a write that would cause the active file to exceed MaxSizeBytes (default 10 MB), RotatingWriter closes and renames the current file to a timestamped backup (<Stream>-<YYYYMMDD-HHMMSS.000>.jsonl), opens a fresh active file, and prunes backup files for that stream that exceed MaxBackups (default 5) or are older than MaxAge (default 7 days).
Index ¶
- Constants
- func Debug(msg string, attrs ...Attr)
- func Error(msg string, attrs ...Attr)
- func Info(msg string, attrs ...Attr)
- func NewRotating(cfg RotatingWriterConfig, level Level) (*Logger, *RotatingWriter, error)
- func SetDefault(l Interface)
- func Warn(msg string, attrs ...Attr)
- type Attr
- func Any(k string, v any) Attr
- func Bool(k string, v bool) Attr
- func Duration(k string, v time.Duration) Attr
- func Err(err error) Attr
- func Float64(k string, v float64) Attr
- func Int(k string, v int) Attr
- func Int64(k string, v int64) Attr
- func String(k, v string) Attr
- func Time(k string, v time.Time) Attr
- func Uint64(k string, v uint64) Attr
- type Interface
- type Level
- type Logger
- func (l *Logger) Debug(msg string, attrs ...Attr)
- func (l *Logger) Error(msg string, attrs ...Attr)
- func (l *Logger) ForComponent(component string) Interface
- func (l *Logger) Info(msg string, attrs ...Attr)
- func (l *Logger) Warn(msg string, attrs ...Attr)
- func (l *Logger) With(attrs ...Attr) Interface
- type RotatingWriter
- type RotatingWriterConfig
Constants ¶
const ( // DefaultStream is the default log stream name when RotatingWriterConfig.Stream is empty. DefaultStream = "tasksmith" // DefaultMaxSizeBytes is the default maximum size (10 MB) of an active log file before rotation. DefaultMaxSizeBytes int64 = 10 * 1024 * 1024 // DefaultMaxBackups is the default maximum number of rotated backup files retained per stream. DefaultMaxBackups = 5 // DefaultMaxAge is the default maximum retention duration (7 days) for rotated backup files. DefaultMaxAge = 7 * 24 * time.Hour )
const ComponentKey = "component"
ComponentKey is the attribute key used by ForComponent.
const ErrorKey = "error"
ErrorKey is the attribute key used by Err.
Variables ¶
This section is empty.
Functions ¶
func NewRotating ¶
func NewRotating(cfg RotatingWriterConfig, level Level) (*Logger, *RotatingWriter, error)
NewRotating creates a size-based RotatingWriter from cfg and returns both a Logger writing to it and the underlying RotatingWriter so the caller can close or sync it.
func SetDefault ¶
func SetDefault(l Interface)
SetDefault replaces the package-level default Logger. If l is nil, a silent logger writing to io.Discard is installed.
Types ¶
type Attr ¶
Attr represents a structured key-value pair attached to a log record.
func Err ¶
Err constructs an Attr under the "error" key for the given error. If err is nil, the attribute value is nil.
type Interface ¶
type Interface interface {
Debug(msg string, attrs ...Attr)
Info(msg string, attrs ...Attr)
Warn(msg string, attrs ...Attr)
Error(msg string, attrs ...Attr)
ForComponent(component string) Interface
With(attrs ...Attr) Interface
}
Interface defines the structured logging contract for TaskSmith components.
func Default ¶
func Default() Interface
Default returns the package-level default Logger. By default, it writes to io.Discard so uninitialized logging in a CLI or TUI never corrupts standard output.
func ForComponent ¶
ForComponent returns a component-scoped Logger derived from the default logger.
type Level ¶
type Level int
Level represents the minimum severity threshold for log messages.
const ( // LevelDebug enables verbose diagnostic messages and all higher severities. LevelDebug Level = -4 // LevelInfo enables standard informational messages and all higher severities. LevelInfo Level = 0 // LevelWarn enables warning and error messages. LevelWarn Level = 4 // LevelError enables only error messages. LevelError Level = 8 )
func ParseLevel ¶
ParseLevel parses a case-insensitive level string ("debug", "info", "warn", "warning", "error") into a Level. Unrecognized or empty strings default to LevelInfo.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger is a structured JSON Lines (JSONL) logger backed by log/slog.
func New ¶
New creates a new structured JSONL Logger writing to w at the specified minimum level. If w is nil, io.Discard is used.
func (*Logger) ForComponent ¶
ForComponent returns a child Logger scoped to the given component name under the "component" attribute key.
type RotatingWriter ¶
type RotatingWriter struct {
// contains filtered or unexported fields
}
RotatingWriter is a thread-safe io.WriteCloser that appends to an active JSONL log file and rotates it when its size would exceed MaxSizeBytes.
func NewRotatingWriter ¶
func NewRotatingWriter(cfg RotatingWriterConfig) (*RotatingWriter, error)
NewRotatingWriter creates and opens a RotatingWriter configured by cfg.
func (*RotatingWriter) Close ¶
func (w *RotatingWriter) Close() error
Close closes the active log file.
func (*RotatingWriter) Path ¶
func (w *RotatingWriter) Path() string
Path returns the filesystem path of the active log file (<Dir>/<Stream>.jsonl).
func (*RotatingWriter) Sync ¶
func (w *RotatingWriter) Sync() error
Sync flushes the active log file's in-memory state to disk.
type RotatingWriterConfig ¶
type RotatingWriterConfig struct {
// Dir is the directory where active and rotated log files are stored.
// If empty, it is resolved via xdg.LogsDir().
Dir string
// Stream is the base stream name (e.g. "daemon" or "client").
// Defaults to "tasksmith", producing "<Dir>/<Stream>.jsonl".
Stream string
// MaxSizeBytes is the maximum size in bytes of the active log file before it is rotated.
// Defaults to 10 MB (10 * 1024 * 1024) when <= 0.
MaxSizeBytes int64
// MaxBackups is the maximum number of rotated backup files to retain for the stream.
// Defaults to 5 when <= 0.
MaxBackups int
// MaxAge is the maximum age of rotated backup files before they are pruned.
// Defaults to 7 days when <= 0.
MaxAge time.Duration
}
RotatingWriterConfig configures a size-based RotatingWriter.