signal module provides signal handling for log rotation, enabling seamless integration with external rotation tools like logrotate. This is essential for production systems where log rotation is managed at the system level.
Features
- SIGHUP handler for log rotation
- Configurable signals - Listen to custom signals
- Thread-safe operation
- Graceful shutdown
- Integration with
RotatingFileWriter
Installation
Basic Usage
How It Works
When your application receives a SIGHUP signal (usually fromlogrotate), the handler:
- Receives the signal
- Calls the
Rotate()method on the writer - Closes the current log file
- Opens a new log file
- Continues logging to the new file
Integration with logrotate
Application Setup
logrotate Configuration
Create/etc/logrotate.d/myapp:
daily- Rotate logs dailyrotate 14- Keep 14 days of logscompress- Compress old logs with gzipdelaycompress- Don’t compress the most recent rotated filecreate 0640 myapp myapp- Create new log file with permissionspostrotate- Send SIGHUP after rotation
Testing logrotate
API Reference
Rotator Interface
NewSIGHUPHandler
WrapRotator
Handler Methods
Custom Rotator
Implement your own rotation logic:Manual Rotation Trigger
From Command Line
From Go Code
Docker/Kubernetes
Docker
Kubernetes with Sidecar
Advanced Example with Logging
Best Practices
Use with RotatingFileWriter
Graceful Shutdown
Error Handling
Troubleshooting
SIGHUP Not Working
Permissions Issues
logrotate Not Sending Signal
See Also
- Rotating Writer - Internal size-based rotation
- Configuration - Logger setup
- Production Setup - Deployment best practices