Log
StableBuffered, rotating file logging for Go
An API revision is planned.
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
- Bounded channel1024 taken under a short read lock; a full queue drops the record and counts it
Processor
- Format txt, json or raw, through a sanitizer policy
- Follow the path at most every 10 ms: an edited file is appended to, a replaced or moved one opened again
- Rotate before a write would pass the size limit, never splitting a record
- Write flushed every 100 ms, synced on a timer and at shutdown
- On an adaptive timer, 100 ms to 60 s: the total size, free disk and retention, oldest files first
Outputs
app.logarchives beside it: app_YYMMDD_HHMMSS.log- Console stdout, stderr, or split by level; or the host’s writer
- Levels
- Trace, Debug, Info, Warn and Error, each with a
*Traceform that adds the calling frames.LogStructuredwrites a message with fields,Writebytes as they are. - Formats
txtlines with time and level,jsonobjects, orraw. A sanitizer policy, raw, txt, json or shell, escapes what a value could otherwise smuggle into the line.- Rotation
- At
max_size_kbthe file is renamed with its time and a new one opened.max_total_size_kb,min_disk_free_kbandretention_period_hrsdelete the oldest archives, never the file being written. - Lifecycle
ApplyConfig, thenStart.Stopdrains the records already accepted and can be followed byStart;Shutdownalso 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’scopytruncatedo: the next record lands at the file’s new end. - Replaced, removed or moved, as vim,
sed -i,rm,mvor logrotate’screatedo: 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.