log package implements simple logging. It provides basic logging functions and customizable loggers.
Basic Logging
Custom Logger
Log Flags
File Logging
Multiple Loggers
Practical Examples
HTTP Request Logger
Structured Logging
log/slog (Go 1.21+)
New structured logging package.Best Practices
- Use appropriate levels - Info for general messages, Error for errors
- Include context - Add relevant information to log messages
- Don’t log sensitive data - Avoid passwords, tokens, etc.
- Configure output - Set appropriate flags and prefix
- Use structured logging - For production applications
- Rotate log files - Prevent unbounded growth
- Handle errors - Check errors when opening log files