Overview
go_logs supports flexible output configuration through theio.Writer interface. You can log to:
- Standard output (console)
- Standard error
- Files with automatic rotation
- Multiple destinations simultaneously
- Custom writers
Standard Output
Console Output (stdout)
Log to standard output for development and containerized environments.Standard Error (stderr)
Log to standard error to separate logs from application output.Discard Output
Disable all output (useful for testing).File Output
Simple File Output
Write logs to a file without rotation.Rotating File Writer
Automatically rotate log files when they reach a size limit.filename: Path to log file (e.g.,/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)
Advanced File Rotation
Time-Based Rotation
Rotate logs based on time instead of size.RotateSize: Rotate when file exceedsMaxSizeMB(default)RotateDaily: Rotate at midnight (00:00)RotateHourly: Rotate at the start of each hour
Daily Rotation
Rotate logs once per day at midnight.Hourly Rotation
Rotate logs every hour.Size-Based with Compression
Rotate by size and compress old files to save disk space.Multiple Output Destinations
Multi-Writer
Write logs to multiple destinations simultaneously (e.g., file + console).WithMultiOutput Option
Convenience option for multiple outputs.Different Formatters per Output
Use different formats for different outputs (e.g., JSON for files, text for console).Dynamic Writers
Add or remove writers at runtime.Error Handling for Multi-Writer
Handle errors from individual writers.Environment-Based Configuration
Load output configuration from environment variables.Production Configuration Examples
Containerized Application (Docker/Kubernetes)
Traditional Server
Development
High-Volume Application
Custom Writers
Implement custom output destinations by implementingio.Writer.
Flushing and Cleanup
Always flush logs before application shutdown.Performance Considerations
Buffering
Rotating file writers use buffered I/O for performance:- Default buffer: 4KB
- Automatic flush on rotation
- Manual flush via
Sync()
Rotation Overhead
- Size-based rotation: ~1-5ms per rotation
- Time-based rotation: Checked on each write, minimal overhead
- Compression: ~10-50ms depending on file size
Best Practices
- Use JSON format for production (easier to parse)
- Enable compression to save disk space
- Set MaxAge to prevent disk full errors
- Flush on shutdown with
defer logger.Sync() - Use MultiWriter sparingly (adds overhead)
Troubleshooting
Permission Errors
Disk Full
Lost Logs on Crash
Next Steps
- Configuration Options - All available options
- Environment Variables - Environment-based config
- Formatters - Text and JSON formatting