log

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 11 Imported by: 0 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

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

View Source
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
)
View Source
const ComponentKey = "component"

ComponentKey is the attribute key used by ForComponent.

View Source
const ErrorKey = "error"

ErrorKey is the attribute key used by Err.

Variables

This section is empty.

Functions

func Debug

func Debug(msg string, attrs ...Attr)

Debug logs a message at LevelDebug using the default logger.

func Error

func Error(msg string, attrs ...Attr)

Error logs a message at LevelError using the default logger.

func Info

func Info(msg string, attrs ...Attr)

Info logs a message at LevelInfo using the default logger.

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.

func Warn

func Warn(msg string, attrs ...Attr)

Warn logs a message at LevelWarn using the default logger.

Types

type Attr

type Attr struct {
	Key   string
	Value any
	// contains filtered or unexported fields
}

Attr represents a structured key-value pair attached to a log record.

func Any

func Any(k string, v any) Attr

Any constructs an Attr with an arbitrary value.

func Bool

func Bool(k string, v bool) Attr

Bool constructs an Attr with a bool value.

func Duration

func Duration(k string, v time.Duration) Attr

Duration constructs an Attr with a time.Duration value.

func Err

func Err(err error) Attr

Err constructs an Attr under the "error" key for the given error. If err is nil, the attribute value is nil.

func Float64

func Float64(k string, v float64) Attr

Float64 constructs an Attr with a float64 value.

func Int

func Int(k string, v int) Attr

Int constructs an Attr with an int value.

func Int64

func Int64(k string, v int64) Attr

Int64 constructs an Attr with an int64 value.

func String

func String(k, v string) Attr

String constructs an Attr with a string value.

func Time

func Time(k string, v time.Time) Attr

Time constructs an Attr with a time.Time value.

func Uint64

func Uint64(k string, v uint64) Attr

Uint64 constructs an Attr with a uint64 value.

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

func ForComponent(component string) Interface

ForComponent returns a component-scoped Logger derived from the default logger.

func With

func With(attrs ...Attr) Interface

With returns an attribute-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

func ParseLevel(s string) Level

ParseLevel parses a case-insensitive level string ("debug", "info", "warn", "warning", "error") into a Level. Unrecognized or empty strings default to LevelInfo.

func (Level) String

func (l Level) String() string

String returns the canonical uppercase name of the log level.

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

Logger is a structured JSON Lines (JSONL) logger backed by log/slog.

func New

func New(w io.Writer, level Level) *Logger

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

func (l *Logger) Debug(msg string, attrs ...Attr)

Debug logs a message at LevelDebug with optional structured attributes.

func (*Logger) Error

func (l *Logger) Error(msg string, attrs ...Attr)

Error logs a message at LevelError with optional structured attributes.

func (*Logger) ForComponent

func (l *Logger) ForComponent(component string) Interface

ForComponent returns a child Logger scoped to the given component name under the "component" attribute key.

func (*Logger) Info

func (l *Logger) Info(msg string, attrs ...Attr)

Info logs a message at LevelInfo with optional structured attributes.

func (*Logger) Warn

func (l *Logger) Warn(msg string, attrs ...Attr)

Warn logs a message at LevelWarn with optional structured attributes.

func (*Logger) With

func (l *Logger) With(attrs ...Attr) Interface

With returns a child Logger with the given attributes pre-bound to all subsequent entries.

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.

func (*RotatingWriter) Write

func (w *RotatingWriter) Write(p []byte) (int, error)

Write writes p to the active log file, rotating it first if the write would cause the file size to exceed MaxSizeBytes and the file is currently non-empty.

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.

Jump to

Keyboard shortcuts

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