Logger - Flexible Logging System
The Logger module provides a flexible logging system that works seamlessly in both local development and cloud environments.
Overview
The Logger extends Python's standard logging.Logger with support for:
- Local development - Colored, formatted console output
- Google Cloud - Structured JSON logging compatible with Google Cloud Logging
- Easy switching - Change modes with a single parameter
- Standard interface - Uses familiar logging methods (
.info(),.error(), etc.)
Basic Usage
Local Development
For local development, use LogType.LOCAL to get colored, human-readable output:
1 2 3 4 5 6 7 | |
Output:
1 2 3 4 | |
Google Cloud Logging
For production deployment on Google Cloud, use LogType.GOOGLE_CLOUD:
1 2 3 4 5 6 7 8 9 10 | |
This outputs structured JSON logs that integrate with Google Cloud Logging.
Environment-Based Configuration
Use the Settings module to automatically select the appropriate log type:
1 2 3 4 5 6 7 8 9 10 | |
Set IS_LOCAL=true in your .env.local file for local development.
Advanced Usage
Custom Logger Name
Use __name__ for automatic module naming, or provide a custom name:
1 2 3 4 5 6 | |
Log Levels
All standard Python logging levels are supported:
1 2 3 4 5 | |
Logging with Variables
Use f-strings or format strings for dynamic content:
1 2 3 4 5 6 7 8 | |
Logging Exceptions
Log exceptions with stack traces:
1 2 3 4 5 | |
Complete Example
Here's a complete example showing Logger in a FastAPI application:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |
Log Formatters
LocalFormatter
The LocalFormatter provides colored, human-readable output for local development:
- Timestamps in readable format
- Color-coded log levels
- Module and line number information
- Clean formatting for console output
GoogleCloudFormatter
The GoogleCloudFormatter produces structured JSON logs compatible with Google Cloud Logging:
- Structured JSON format
- Severity levels mapped to Google Cloud standards
- Automatic metadata inclusion
- Stack trace formatting for errors
Best Practices
1. Use Module Names
Always use __name__ for automatic module identification:
1 2 | |
2. Appropriate Log Levels
Choose the right level for your messages:
DEBUG- Detailed diagnostic information useful during developmentINFO- General informational messages about application flowWARNING- Something unexpected but the application continuesERROR- Error occurred but application can recoverCRITICAL- Serious error, application might not continue
3. Structured Logging
Include context in your log messages:
1 2 3 4 5 | |
4. Don't Log Sensitive Data
Never log passwords, tokens, or other sensitive information:
1 2 3 4 5 | |
5. Use Exception Logging
Use logger.exception() in exception handlers:
1 2 3 4 | |
Configuration
The Logger module uses the following configuration:
- LogType.LOCAL: Colored console output via
LocalFormatter - LogType.GOOGLE_CLOUD: Structured JSON via
GoogleCloudFormatter
Customizing Formatters
You can create custom formatters by extending the base formatters:
1 2 3 4 5 6 7 8 | |
Testing
Test your logging by checking log output:
def test_logger(caplog): logger = Logger("test")
1 2 3 4 5 6 7 8 9 | |
```
Related Documentation
- Configuration Guide - Use Settings to manage log types
- Tracer Guide - Combine with Timer for performance logging
- FastAPI Use Case - Logger in web applications