Overview
The BE Monorepo uses OpenTelemetry for structured logging, providing correlation with traces and automatic export to Grafana Loki.Logger Implementation
The logger is implemented insrc/core/utils/logger.ts using OpenTelemetry’s Logs API.
Logger Class
Default Logger Instance
A default logger instance is exported for use throughout the application:Log Levels
The logger supports multiple severity levels:| Level | Severity Number | Use Case |
|---|---|---|
| TRACE | 1-4 | Fine-grained debugging |
| DEBUG | 5-8 | Development debugging |
| INFO | 9-12 | General informational messages |
| WARN | 13-16 | Warning conditions |
| ERROR | 17-20 | Error conditions |
| FATAL | 21-24 | Critical failures |
Structured Logging
Adding Attributes
Logs can include structured attributes for better filtering and analysis:Error Logging
When logging errors, include relevant context:Console Output with Colors
The logger provides colorized console output for local development:Time Formatting
Usage in Error Handling
The logger is integrated with Hono’s error handling:src/app.ts:72
Creating Custom Loggers
You can create loggers with specific contexts:Best Practices
1. Use Structured Attributes
Always use structured attributes instead of string interpolation:2. Include Request Context
Always include request ID for correlation:3. Log at Appropriate Levels
- Use
log()for normal operations - Use
warn()for concerning but non-critical issues - Use
error()for failures that need attention
4. Avoid Logging Sensitive Data
Never log passwords, tokens, or PII:Querying Logs in Grafana
Logs are automatically exported to Loki and can be queried in Grafana:Configuration
The logger uses environment variables for configuration:Next Steps
Tracing
Learn how to correlate logs with traces
Grafana Setup
Query and visualize logs in Grafana
