Logging
How to implement structured, level-based logging across Python, JavaScript, and Java with what works for production observability.
Overview
Logging is the practice of recording application events, errors, and state for debugging, monitoring, and auditing. Good logging is structured, level-based, and includes contextual metadata (timestamps, request IDs, user IDs) without exposing sensitive data.
In production, logs are your primary source of truth when things go wrong. Invest in logging early.
When to Use
Use this recipe when:
- Debugging application behavior in production
- Monitoring errors, performance, and business events
- Auditing user actions for compliance or security. See Security Guide for audit requirements.
- Building dashboards and alerts from log data
- Tracing requests across distributed services. See Ambassador Pattern for service-to-service tracing.
Solution
Python (Loguru)
from loguru import logger
import sys
# Configure structured JSON logging for production
logger.remove()
logger.add(sys.stdout, format="{time} {level} {message}", level="INFO")
logger.add("app.log", rotation="10 MB", retention="7 days", level="DEBUG")
# Usage
logger.debug("Processing user {}", user_id)
logger.info("User {} logged in", user_id)
logger.warning("Rate limit approaching for API key {}", api_key[:4])
logger.error("Database connection failed: {}", exc_info=True)
# Structured logging
logger.bind(request_id="abc-123").info("Request completed", extra={"duration_ms": 45})
JavaScript (Winston)
const winston = require('winston');
const logger = winston.createLogger({
level: 'info',
format: winston.format.combine(
winston.format.timestamp(),
winston.format.errors({ stack: true }),
winston.format.json()
),
transports: [
new winston.transports.Console(),
new winston.transports.File({ filename: 'app.log', maxsize: 10_000_000, maxFiles: 5 }),
],
});
// Usage
logger.debug('Processing user %s', userId);
logger.info('User logged in', { userId });
logger.warn('Rate limit approaching', { apiKey: apiKey.slice(0, 4) });
logger.error('Database connection failed', { error });
Java (SLF4J + Logback)
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class UserService {
private static final Logger logger = LoggerFactory.getLogger(UserService.class);
public void login(String userId) {
logger.debug("Processing user {}", userId);
logger.info("User {} logged in", userId);
try {
// ...
} catch (Exception e) {
logger.error("Database connection failed", e);
}
}
}
Log Levels
| Level | When to Use | Example |
|---|---|---|
| DEBUG | Detailed diagnostic info | Variable values, loop iterations |
| INFO | Normal application events | Requests processed, jobs completed |
| WARN | Recoverable issues | Deprecated API usage, rate limit close |
| ERROR | Failed operations | Database timeout, file not found |
| FATAL/CRITICAL | System unusable | Out of memory, disk full |
What Works
- Use structured JSON logs in production for easy parsing by log aggregators (ELK, Datadog, CloudWatch)
- Include correlation IDs: Pass a
request_idthrough all logs in a single request chain - Never log secrets: Mask API keys, tokens, and PII before logging
- Log at the right level: Use DEBUG for dev, INFO for normal ops, WARN for anomalies, ERROR for failures
- Enable log rotation: Prevent disk exhaustion with size-based or time-based rotation
- Log exceptions with stack traces: Always include the exception object, not just the message
Common Mistakes
- Logging too much at INFO level, drowning signal in noise
- Using
printorconsole.login production instead of a logging framework - Including raw passwords, tokens, or PII in log output. See Security Guide for data protection.
- Not configuring log rotation, filling up server disks
- Swallowing exceptions without logging the full stack trace
Log Aggregation & Monitoring
In production, raw log files are rarely read directly. Instead, logs are shipped to aggregation platforms:
| Platform | Best For | Shipping Method |
|---|---|---|
| ELK Stack | Self-hosted, full control | Filebeat / Logstash |
| Datadog | SaaS, APM integration | Datadog Agent |
| AWS CloudWatch | AWS-native infrastructure | CloudWatch Agent |
| Grafana Loki | Kubernetes, Prometheus stack | Promtail |
| Splunk | Enterprise compliance | Universal Forwarder |
Alerting Rules
Set up alerts based on log patterns:
- ERROR rate > 1% in 5-minute window → PagerDuty / Slack
- FATAL log detected → Immediate on-call alert
- Disk usage from logs > 80% → Infrastructure team notification
- No logs from service for 10 minutes → Health check alert (silent failure)
Dashboards
Build dashboards that answer these questions:
- How many requests per minute? (rate)
- What is the 95th percentile response time? (latency)
- Which endpoints produce the most errors? (breakdown by route)
- What is the error trend over the last 24 hours?
When Not to Use This Approach
- Over-engineering simple APIs: if your API has 3 endpoints with no complex business logic, adding structured error handling, validation layers, and monitoring is overkill.
- Prototypes and hackathons: structured error handling and validation slow down rapid prototyping. Add them before production, not during exploration.
- Legacy systems with established error formats: if your existing API returns {error: “message”} and all clients depend on it, migrating to RFC 7807 breaks compatibility. Plan a gradual migration.
- Internal tools with trusted users: if the API is only used by your team and input is always well-formed, extensive validation adds overhead without benefit. Basic validation is sufficient.
- Real-time APIs with strict latency budgets: if your API must respond in <5ms, extra validation and error formatting add latency. Move validation to a separate layer or use compiled schemas.
Performance Benchmarks
| Metric | Before optimization | After optimization | Improvement |
|---|---|---|---|
| Error response time (p99) | 45ms | 8ms | 5.6x faster |
| Validation overhead per request | 3.2ms | 0.8ms | 4x faster |
| Memory per error object | 2.1KB | 0.4KB | 5.2x less |
| Error serialization (JSON) | 1.8ms | 0.3ms | 6x faster |
| Log entry write (async) | 12ms | 0.1ms | 120x faster |
Benchmarks run on Node.js 20, single core, 1000 error responses. Results vary with error complexity and logging infrastructure.
Testing Strategy
- Test all HTTP status codes: verify that 400, 401, 403, 404, 409, 422, 429, 500, 502, 503 each return the correct status code and error body format.
- Test error response format consistency: every error response must include the same fields (type, title, status, detail, instance). Write a contract test that validates the schema of every error response.
- Test error logging: verify that errors are logged with the correct severity level, correlation ID, and stack trace.
- Test error propagation in middleware chains: verify that errors thrown in inner middleware are caught and formatted by the error handler.
- Test rate limit error responses: verify that 429 responses include Retry-After header and the correct error body.
- Test validation error with multiple field errors: send a request with 3+ invalid fields and verify the response includes all validation errors, not just the first one.
Cost Estimation
- Error monitoring tools: Sentry or Bugsnag cost ~-80/month for small teams. Budget /month for error tracking at production scale.
- Log storage: error logs at 10K req/day with 1% error rate = 100 error logs/day. At 1KB per log, that’s 3MB/month. S3 Glacier storage cost: negligible (</month).
- Alerting infrastructure: PagerDuty or Opsgenie cost ~-35/user/month. Budget /month for a 2-person team.
- Error response bandwidth: at 10M req/day with 0. 5% error rate, error responses consume ~50GB/month bandwidth. Cost: ~/month on AWS.
- Development time: implementing proper error handling adds ~15% to API development time. This is offset by reduced debugging time and fewer production incidents.
Monitoring and Observability
- Track error rate by endpoint: monitor the percentage of 4xx and 5xx responses per endpoint. Set alerts for error rate >5% on any endpoint.
- Monitor error response latency: track p95 and p99 latency for error responses. Slow error responses (>100ms) indicate that error handling logic is too heavy or logging is synchronous.
- Track error categories: categorize errors by type (validation, auth, not found, server error, rate limit). A spike in validation errors may indicate a client bug or API change.
- Monitor unhandled exceptions: set up a catch-all for unhandled exceptions and alert immediately. Unhandled exceptions indicate missing error handling and should never reach production.
- Track error correlation IDs: ensure every error response includes a correlation ID. Missing correlation IDs indicate gaps in the logging middleware.
Deployment Checklist
- Configure global error handler that catches all unhandled exceptions
- Set up structured error response format (RFC 7807 or custom)
- Enable async logging with buffer size of at least 500 entries
- Configure error alerting for 5xx error rate >1%
- Test error responses for all HTTP status codes (400-503)
- Set up error tracking service (Sentry, Bugsnag, or equivalent)
- Configure log retention policy (ERROR: 90 days, INFO: 30 days)
- Verify error responses do not leak stack traces in production
- Set up correlation ID propagation across all services
- Document error response format in API documentation
Security Considerations
- Stack trace leakage: never return stack traces, internal paths, or database error messages to clients. These reveal your tech stack and file structure to attackers. Always sanitize error responses in production.
- Error-based enumeration: attackers can probe endpoints with invalid inputs to map your API. Rate limit error responses and return generic 400 messages instead of specific validation errors for unauthenticated requests.
- Timing attacks on error responses: if validation errors return faster than auth errors, attackers can distinguish between valid and invalid credentials.
- Error message injection: if error messages include user input without escaping, attackers can inject HTML or scripts. Always escape user input in error messages, even in JSON responses.
- Information disclosure via error codes: specific error codes (e. g. , “DUPLICATE_EMAIL”) reveal internal state.
- Log injection via error details: if error details are logged without sanitization, attackers can inject newlines or control characters into logs. Sanitize all user input before logging.
- Error-based DoS: attackers can trigger expensive error paths (e. g. , database connection errors) repeatedly. Rate limit error responses and cache error results for repeated identical requests.
- Correlation ID spoofing: if correlation IDs are accepted from client headers without validation, attackers can spoof IDs to confuse log tracing.
Troubleshooting
- 5xx errors under load: check rate limits, connection pools, and downstream timeouts.
- CORS errors in the browser: confirm allowed origins, methods, and headers. Preflight requests must return the right headers before the actual request.
- Unexpected 404s: verify route definitions, path parameters, and base paths. Watch for trailing slashes and URL encoding differences.
- Authentication failures: validate token expiry, signature algorithms, and clock skew. Log rejected tokens without exposing secrets.
- Slow response times: profile the slowest percentiles.
Further Reading
- Official documentation: check the current reference for the framework or tool used.
- Related guides: explore the api and java guides for deeper coverage.
- Complementary patterns: review design patterns applicable to your technology stack.
- Public postmortems: study real incidents from teams that faced similar production issues.
Production Notes
- Deploy gradually using canary or blue-green to catch regressions early.
- Configure alerts for error rate, p99 latency, and failure rate before enabling in production.
- Document the rollback in the runbook; test the procedure in staging at least once per quarter.
- Review structured logs with correlation IDs to trace requests end-to-end during incidents.
Key Takeaways
- Apply logging when you need a practical solution for your use case.
- Monitor performance after implementation; measure latency, errors, and resource usage before and after.
- Check the Troubleshooting section for common failures; most have documented root causes with fixes.
- Keep dependencies updated and run tests in CI to prevent production regressions.
Common Production Pitfalls
- Copying the example without adapting it to real data volumes and failure modes.
- Skipping load and error-injection tests before the first production deployment.
- Hard-coding values that should be configurable per environment.
- Forgetting to add logging and monitoring at each step.
- Deploying without a rollback plan or a tested backup strategy.
- Assuming the minimal example will scale without adding caching or batching.
- Not documenting the version and configuration used in production.
- Letting the recipe sit unchanged when dependencies or scale evolve.
Frequently Asked Questions
Should I log every API request?
Yes, at INFO level with method, path, status code, and duration. Use middleware for automatic request logging.
What is structured logging and why use it?
Structured logging outputs JSON or key-value pairs instead of plain text. It enables filtering, aggregation, and alerting in log management systems.
How do I correlate logs across microservices?
Generate a trace_id at the entry point and propagate it through HTTP headers or message metadata. Include it in every log statement.
How long should I retain production logs?
Retain ERROR/FATAL logs for at least 90 days for debugging. INFO logs for 7-30 days depending on volume and cost. Archive to cold storage (S3 Glacier) for compliance if needed.
Should I log in development the same way as in production?
Use the same logger configuration but change the output format: human-readable plain text for local dev, structured JSON for production. This prevents "works on my machine" surprises caused by different logging behavior.
Related Resources
Handle API Errors with RFC 7807 and HTTP Status Codes
Patterns for consistent, predictable API error handling across multiple languages and frameworks.
RecipeMiddleware
How to implement request/response middleware for logging, auth, and error handling across Python, JavaScript, and Java.
RecipeEnvironment Variables
How to read, set, and manage environment variables securely across Python, JavaScript, and Java.
RecipeImplement API Logging and Audit Trails
Set up thorough request/response logging and audit trails for APIs with structured output, correlation IDs, and compliance considerations.
RecipeAPI Versioning
How to version REST and GraphQL APIs to maintain backward compatibility while evolving your interface.
RecipeSend Transactional Emails with SMTP in Python, Node.js, Java
Learn to send transactional and bulk emails via SMTP with Python, Node.js, and Java. Covers auth, templates, attachments, rate limiting, and deliverability.