logging.py
This file configures the application's logging system, setting up file and console handlers. It ensures log directories exist and applies formatting, enabling structured output for operational monitoring and debugging.
Scope
-
Configures application-wide logging settings.
-
Ensures log file directories are properly created.
-
Establishes both file-based and console logging handlers.
-
Initializes a global logger instance for application use.
Imports
-
loggingProvides core logging functionality, enabling structured message recording and output management. It manages log levels, formats, and handlers effectively for application events and debugging.
-
osOffers operating system interaction, specifically for directory creation to store log files. It ensures the necessary file system structure is in place before logging commences.
-
settings from app.core.configSupplies application configuration parameters, including log file path and level. This centralizes logging settings, allowing easy modification without altering core logic.
Variables
loggerThe primary logger instance, configured for application-wide use, facilitating consistent message recording across modules. It acts as the central point for all log emissions.
Global Code
Creates the directory for log files if it doesn't already exist, ensuring the logging system can write its output. This prevents runtime errors during log file creation. Sets up the root logger with specified level, format, and handlers, directing log messages to both a file and the console. This initializes the logging system.
FAQs
Why is os.makedirs called directly?
os.makedirsis called directly to ensure the log file's parent directory exists beforelogging.FileHandlerattempts to write. This prevents potentialFileNotFoundErrorexceptions during application startup or logging operations.
Why use both FileHandler and StreamHandler?
Using both handlers ensures log messages are simultaneously written to a persistent file for archival and debugging, and displayed in the console for immediate operational feedback during development and deployment.
How is the logging level determined?
The logging level is dynamically retrieved from
settings.LOG_LEVEL, allowing administrators to easily adjust the verbosity of logs (e.g., DEBUG, INFO, WARNING) without requiring any code changes or redeployment.
Insights
| Metric | Score | Level |
|---|---|---|
| Complexity | 0.20 | Simple |
| Security | 0.80 | Secure |
| Performance | 0.80 | Optimised |
Complexity
Low - Structured Logging Format
Adopt a structured logging format (e.g., JSON) for easier parsing, filtering, and analysis by external log management systems and tools, enhancing observability.
Security
Medium - Sensitive Data Masking in Logs
Implement mechanisms to automatically mask or redact sensitive information like PII or credentials before logging, preventing accidental exposure and data breaches.
Performance
Low - Asynchronous Logging Implementation
Consider implementing asynchronous logging to prevent I/O operations from blocking the main application thread, improving overall responsiveness and system throughput under load.