Log

Stable

Buffered, rotating file logging for Go

An API revision is planned.

Used by ViF and LogWisp

A buffered, rotating file logger for Go with disk-space management, operational heartbeats and hot reconfiguration.

  • Lock-free asynchronous writes, minimal impact on the caller
  • Rotation and disk-space limits built in
  • Heartbeat records for production monitoring
  • Reconfigure at runtime without losing records

How a record is written

A caller puts a record on a bounded channel and returns. A single processor goroutine takes records off it in order, formats them and writes them, and it alone rotates the file and deletes old ones. Nothing a caller does waits on the disk, and when the queue is full the record is dropped and counted rather than blocking the program.

Callers

  • Debug … Error a message and key-value pairs, from any goroutine
  • *Trace the same, with the calling frames, up to ten deep
  • Heartbeattimer process, disk and system counts, past the level filter

Queue

  1. Bounded channel1024 taken under a short read lock; a full queue drops the record and counts it

Processor

  1. Format txt, json or raw, through a sanitizer policy
  2. Follow the path at most every 10 ms: an edited file is appended to, a replaced or moved one opened again
  3. Rotate before a write would pass the size limit, never splitting a record
  4. Write flushed every 100 ms, synced on a timer and at shutdown
  5. On an adaptive timer, 100 ms to 60 s: the total size, free disk and retention, oldest files first

Outputs

  • app.log archives beside it: app_YYMMDD_HHMMSS.log
  • Console stdout, stderr, or split by level; or the host’s writer
No caller waits on the disk. One goroutine owns the file, so formatting, rotation and cleanup never race a write.
Levels
Trace, Debug, Info, Warn and Error, each with a *Trace form that adds the calling frames. LogStructured writes a message with fields, Write bytes as they are.
Formats
txt lines with time and level, json objects, or raw. A sanitizer policy, raw, txt, json or shell, escapes what a value could otherwise smuggle into the line.
Rotation
At max_size_kb the file is renamed with its time and a new one opened. max_total_size_kb, min_disk_free_kb and retention_period_hrs delete the oldest archives, never the file being written.
Lifecycle
ApplyConfig, then Start. Stop drains the records already accepted and can be followed by Start; Shutdown also closes the file. A new configuration applies while running.

A file someone else touches

The logger writes with O_APPEND to whatever file its path names, and checks that path at most once per 10 ms of records, at every Flush, at Stop and before a rotation.

  • Edited in place, as truncate -s 0, nano or logrotate’s copytruncate do: the next record lands at the file’s new end.
  • Replaced, removed or moved, as vim, sed -i, rm, mv or logrotate’s create do: the path is opened again, created if missing, with no signal or reopen call. On Unix the reopen never follows a symlink and keeps only a regular file; a refusal is reported once and the old handle stays in use.
  • Disk heartbeats count each reopen, so a monitor sees it happen.

Heartbeats

At heartbeat_level 1 to 3 the logger writes its own records every heartbeat_interval_s: processed and dropped counts, then file and disk use, then the process’s memory, collections and goroutines. They pass the level filter, so a service logging only errors still says it is alive.

2024-01-15T10:30:00Z PROC type=proc sequence=1 uptime_hours="24.50" processed_logs=1847293 total_dropped_logs=0

Beyond the logger

The formatter and sanitizer packages work on their own: buffered calls for one goroutine, and Append* calls into a caller’s buffer, safe from any. compat adapts the logger to gnet v2, fasthttp and Fiber v2. A host that owns the terminal, as a TUI does, routes console output through SetConsoleWriter and the logger’s own errors through SetErrorHandler.