Comprehensive guide for logging best practices
Detailed diagnostic information for development and troubleshooting
DEBUG: Processing user request for ID 12345
DEBUG: Cache miss for key 'user:session:abc123'
DEBUG: Executing SQL: SELECT * FROM users WHERE id = ?
DEBUG: Function calculateTotal() returned 156.78
General informational messages about system operation
INFO: Application started on port 8080
INFO: User 'john@example.com' logged in successfully
INFO: Payment processed for order #ORD-12345
INFO: Daily backup completed successfully
INFO: Connected to database: prod-db-01
Potentially harmful situations that don't prevent operation
WARN: API endpoint /old-api is deprecated, use /api/v2
WARN: Retrying failed request to payment service (attempt 2/3)
WARN: Connection pool at 80% capacity
WARN: Cache server unavailable, using local cache
WARN: Response time exceeded 500ms: 1,245ms
Error events that still allow the application to continue
ERROR: Failed to process payment for order #ORD-12345: TimeoutException
ERROR: Database connection lost: Could not connect to prod-db-01
ERROR: Unable to send email notification: SMTP server unreachable
ERROR: Invalid data in request: User ID cannot be null
ERROR: Failed to write to file /var/log/app.log: Disk full
Severe errors that cause application shutdown or inability to function
FATAL: Cannot connect to database, application shutting down
FATAL: Out of memory, cannot allocate heap space
FATAL: Configuration file missing: /etc/app/config.yaml
FATAL: SSL certificate expired, cannot start HTTPS server
FATAL: Critical dependency 'payment-service' unreachable
| Environment | Recommended Level | Reasoning |
|---|---|---|
| Development | DEBUG | Maximum visibility for debugging |
| Testing/Staging | INFO | Similar to production |
| Production | INFO or WARN | Balance visibility and performance |
| High-traffic Prod | WARN | Reduce volume and costs |
{
"timestamp": "2024-01-15T14:30:45.123Z",
"level": "ERROR",
"service": "payment-api",
"requestId": "req-abc-123",
"userId": "user-456",
"message": "Payment processing failed",
"error": {
"type": "TimeoutException",
"message": "Connection timeout after 5000ms",
"stack": "..."
},
"metadata": {
"orderId": "ORD-789",
"amount": 99.99,
"currency": "USD",
"gateway": "stripe"
}
}
| DEBUG | Diagnostic info |
| INFO | General events |
| WARN | Potential issues |
| ERROR | Operation failed |
| FATAL | App shutting down |
Will the app crash?
Yes → FATAL
Did an operation fail?
Yes → ERROR
Is this unexpected but handled?
Yes → WARN
Is this a significant event?
Yes → INFO
Is this for debugging?
Yes → DEBUG