log package provides a simple, consistent logging interface with support for multiple backend implementations including Logrus and a no-op logger for testing.
Features
- Simple Interface: Consistent API across different implementations
- Structured Logging: Key-value pairs for better log parsing
- Multiple Backends: Logrus, no-op, and extensible for custom loggers
- Log Levels: Debug, Info, Warn, Error, and Fatal
- Configurable: Control log level, output format, and destination
Installation
Logger Interface
Logrus Implementation
Creating a Logrus Logger
Logrus Options
LogrusWithLevel
debug, info, warn, error, fatal, panic.
Example:
LogrusWithWriter
LogrusWithFormatter
Log Levels
Debug
Detailed information for debugging:Info
General informational messages:Warn
Warning messages for potentially harmful situations:Error
Error messages for serious problems:Fatal
Critical errors that cause application exit:No-op Logger
For testing or when logging is not needed:Structured Logging
The logger uses key-value pairs for structured logging:Complete Example
Integration with Config
HTTP Middleware Example
Error Logging with Context
Best Practices
Use Appropriate Log Levels
Use Appropriate Log Levels
Choose the right level for each message:
Always Use Key-Value Pairs
Always Use Key-Value Pairs
Provide context with structured data:
Don't Log Sensitive Data
Don't Log Sensitive Data
Never log passwords, tokens, or PII:
Use JSON Format in Production
Use JSON Format in Production
JSON logs are easier to parse and analyze: