Overview
go_logs provides two rotating file writers with zero external dependencies:- RotatingFileWriter - Simple size-based rotation (
~/workspace/source/rotating_writer.go) - EnhancedRotatingFileWriter - Advanced rotation with time-based rotation and compression (
~/workspace/source/rotating_writer_enhanced.go)
Simple Rotation (Size-Based)
Creating a Rotating Writer
From~/workspace/source/rotating_writer.go:60:
Parameters
- filename: Path to log file (e.g.,
"app.log"or"/var/log/app.log") - maxSizeMB: Maximum size in megabytes before rotation (e.g.,
100) - maxBackups: Maximum number of backup files to keep (e.g.,
5)
How It Works
From~/workspace/source/rotating_writer.go:103-107:
- Logs are written to
app.log - When file exceeds
maxSizeMB, rotation occurs:app.log.3→app.log.4(deleted if exceeds maxBackups)app.log.2→app.log.3app.log.1→app.log.2app.log→app.log.1- New empty
app.logis created
- Process continues writing to new
app.log
Manual Rotation
From~/workspace/source/rotating_writer.go:134:
Syncing to Disk
From~/workspace/source/rotating_writer.go:222:
Enhanced Rotation (Time-Based + Compression)
Basic Time-Based Rotation
From~/workspace/source/rotating_writer_enhanced.go:84:
Rotation Types
From~/workspace/source/rotating_writer_enhanced.go:14-24:
Full Configuration
From~/workspace/source/rotating_writer_enhanced.go:41-62 and ~/workspace/source/rotating_writer_enhanced.go:95:
Configuration Options
| Option | Type | Description |
|---|---|---|
Filename | string | Path to log file |
MaxSizeMB | int | Maximum size in MB before rotation |
MaxBackups | int | Maximum number of backup files to keep (0 = unlimited) |
RotationType | RotationType | When to rotate: RotateSize, RotateDaily, RotateHourly |
Compress | bool | Enable gzip compression of rotated files |
MaxAge | int | Maximum days to keep old log files (0 = unlimited) |
LocalTime | bool | Use local time for rotation timestamps (false = UTC) |
Using WithRotatingFileEnhanced
From~/workspace/source/options.go:250:
File Naming
Size-Based Rotation
Time-Based Rotation
From~/workspace/source/rotating_writer_enhanced.go:203-211:
With Compression
Performance Features
Buffered Writes
From~/workspace/source/rotating_writer.go:43 and ~/workspace/source/rotating_writer.go:202:
- All writes use
bufio.Writerfor buffering - Reduces system calls and improves throughput
- Automatically flushed on rotation and close
Thread-Safety
From~/workspace/source/rotating_writer.go:46 and ~/workspace/source/rotating_writer.go:91-92:
Automatic Directory Creation
From~/workspace/source/rotating_writer.go:186-192:
0755 permissions.
File Permissions
From~/workspace/source/rotating_writer.go:196:
- Log files are created with
0600permissions (owner read/write only) - Directories are created with
0755permissions - Ensures logs are not world-readable for security
Compression
From~/workspace/source/rotating_writer_enhanced.go:278-315:
- Compression uses standard library
compress/gzip - 32KB buffer for efficient compression
- Original file removed after successful compression
- Errors during compression fall back to uncompressed rotation
Cleanup Policies
By Backup Count
From~/workspace/source/rotating_writer_enhanced.go:407-411:
- Keeps only
MaxBackupsmost recent files - Older files are deleted automatically
- Applied after each rotation
By Age
From~/workspace/source/rotating_writer_enhanced.go:373-380:
- Files older than
MaxAgedays are deleted - Calculated from file modification time
- Applied before backup count cleanup
Integration Examples
Daily Logs with 30-Day Retention
Hourly Logs for High-Volume Services
Size-Based with Simple Config
From~/workspace/source/options.go:274:
Multi-Output with Rotation
Best Practices
Always Close Writers
Sync Before Exit
Handle Rotation Errors
Monitor Disk Space
- Set
MaxBackupsandMaxAgeto prevent unlimited disk usage - Monitor log volume and adjust
MaxSizeMBas needed - Use compression for long-term storage
Troubleshooting
Rotation Not Happening
Check:- Current file size with
GetCurrentSize() - Max size setting with
GetMaxSize() - Next rotation time with
GetNextRotationTime()(enhanced only)
Permission Denied
Ensure:- Process has write permissions to log directory
- Log file is not open in another process
- No file locks on rotated files
Disk Space Issues
Configure:MaxBackupsto limit file countMaxAgeto automatically delete old filesCompressto reduce storage usage by 70-90%
See Also
- Configuration - Environment variable setup
- Production Deployment - Best practices for production