Overview
RotatingFileWriter implements io.Writer with automatic log file rotation based on file size. When a log file exceeds the configured size, it’s rotated and backups are created with incrementing suffixes (.1, .2, .3, etc.).
This is a zero-dependency implementation using only the Go standard library, providing buffered writes for high performance.
Type Definition
Constructor Function
NewRotatingFileWriter
Creates a new RotatingFileWriter with the specified parameters.filename- Path to the log file (e.g., “app.log” or “/var/log/app.log”)maxSizeMB- Maximum size in megabytes before rotation (must be > 0)maxBackups- Maximum number of backup files to keep (must be >= 0)
*RotatingFileWriter- Configured writererror- Error if file cannot be created/opened or parameters are invalid
- Resumes appending to existing file if it exists
- Creates new file with permissions 0600 (owner read/write only)
- Creates parent directories if they don’t exist (with permissions 0755)
Methods
Write
Implements io.Writer interface. Writes bytes to the log file, rotating if necessary.p- Byte slice to write
n- Number of bytes writtenerr- Error if write or rotation failed
- Thread-safe (uses mutex)
- Checks if rotation needed before writing
- Rotates if current size + new data > max size
- Updates current size after successful write
- Returns error if writer is closed
Rotate
Performs a manual rotation of the log file.error- Error if rotation failed
- Flush and close current log file
- Rename backups: .3 → .4, .2 → .3, .1 → .2
- Rename current file: app.log → app.log.1
- Create new empty app.log
- Delete backup beyond maxBackups limit
- Force rotation (e.g., on SIGHUP signal)
- Manual rotation independent of size
- Integration with log rotation systems
Sync
Flushes buffered data to disk.error- Error if flush or sync failed
- Flushes buffered writer
- Calls file.Sync() to ensure data is on disk
- Safe to call on closed writer (returns nil)
- Before application exit
- After critical log messages
- Periodic flushing for durability
Close
Flushes buffered data and closes the log file.error- Error if flush or close failed
- Idempotent (safe to call multiple times)
- Flushes buffered data before closing
- Marks writer as closed (subsequent writes fail)
GetMaxSize
Returns the maximum file size in bytes before rotation.int64- Maximum size in bytes
- Monitoring and diagnostics
- Testing
GetMaxBackups
Returns the maximum number of backup files to keep.int- Maximum backup count
- Monitoring and diagnostics
- Testing
Rotation Behavior
File Naming
Rotation creates backup files with numeric suffixes:Rotation Process
Before rotation:Automatic Rotation Trigger
Rotation occurs when:Usage Examples
Basic File Rotation
Production Setup
With SIGHUP Rotation
Combined with MultiWriter
Small Files for Testing
Custom Rotation Path
Configuration via Environment
Best Practices
Performance
RotatingFileWriter is optimized for high throughput: Benchmark results:- ~16 million messages per second
- Zero allocations per write
- Buffered I/O reduces syscalls
Thread Safety
All methods are thread-safe:- No interleaved writes
- Safe rotation
- Consistent state
Related
- MultiWriter - Write to multiple destinations
- SamplingWriter - Rate-limited writing
- Logger Options - WithOutput option