Structured Logging System¶
This document provides an overview of the centralized structured logging system implemented for the PRS Backend project.
Overview¶
The logging system consists of:
- Logger Service: A centralized service for structured logging
- Log Formatters: Utilities for consistent log formatting
- Log Middleware: Middleware for request and database logging
- Log Transport: Configuration for log destinations and rotation
Logger Service¶
The LoggerService provides a consistent interface for logging throughout the application:
Log Levels¶
The logger supports the following log levels (in order of increasing severity):
- TRACE: Detailed information for debugging
- DEBUG: Debugging information
- INFO: General information about application operation
- WARN: Warning conditions
- ERROR: Error conditions
- FATAL: Severe error conditions that may cause the application to terminate
Log Categories¶
Logs are categorized to make filtering and analysis easier:
- API: API requests and responses
- DATABASE: Database operations
- SECURITY: Authentication and authorization
- PERFORMANCE: Performance metrics
- BUSINESS: Business events and operations
- INTEGRATION: External service interactions
- SYSTEM: System-level events
Log Structure¶
All logs follow a consistent structure:
Common Fields¶
- timestamp: ISO 8601 timestamp
- level: Log level
- message: Log message
- service: Service name
- environment: Environment (development, production, etc.)
- requestId: Request ID for correlation
- userId: User ID (if available)
- username: Username (if available)
- category: Log category
Request Logging¶
All HTTP requests and responses are automatically logged with the following information:
Request Logs¶
| JSON | |
|---|---|
Response Logs¶
| JSON | |
|---|---|
Database Logging¶
Database operations are automatically logged with the following information:
| JSON | |
|---|---|
Slow queries (> 100ms) are logged at the INFO level with additional performance information:
| JSON | |
|---|---|
Error Logging¶
Errors are logged with detailed information:
| JSON | |
|---|---|
Log Configuration¶
The logging system is configured based on the environment:
Production¶
- Logs are written to rotating files
- Log level is set to INFO
- Files are rotated based on size (50MB) and time (daily)
- Old logs are compressed and retained for 14 days
Development¶
- Logs are written to both console and files
- Console logs are pretty-printed with colors
- File logs include all levels (DEBUG and above)
- Files are rotated based on size (10MB) and time (daily)
Local¶
- Logs are written to console only
- Console logs are pretty-printed with colors
- Log level is set to DEBUG
Best Practices¶
- Use Appropriate Log Levels: Use the appropriate log level for each message
- Include Context: Always include relevant context in logs
- Structured Data: Use structured data instead of string concatenation
- Sensitive Information: Never log sensitive information (passwords, tokens, etc.)
- Correlation IDs: Use request IDs for correlating logs across services
- Business Events: Log important business events for auditing
- Performance Metrics: Log performance metrics for monitoring
Integration with Error Handling¶
The logging system integrates with the error handling system:
| JavaScript | |
|---|---|
Extending the Logging System¶
To add new log categories or formatters:
- Add new categories to
LOG_CATEGORIESinloggerService.js - Add new formatters to
logFormatter.js - Add new methods to
LoggerServiceclass as needed