Enhance logging system documentation and default logger configuration
- Updated the logging documentation to reflect changes in the unified log message format, including the addition of pathname, line number, and function name for better traceability. - Modified the `get_logger` function to set a default value for `component_name`, improving usability for users who may not specify a component name. - Ensured consistency in the documentation regarding the parameters and their descriptions. These updates improve the clarity and ease of use of the logging system, making it more accessible for developers.
This commit is contained in:
parent
e147aa1873
commit
848119e2cb
@ -8,8 +8,8 @@ The TCP Dashboard project uses a unified logging system built on Python's standa
|
||||
- **Standardized & Simple**: Relies on standard Python `logging` handlers, making it robust and easy to maintain.
|
||||
- **Date-based Rotation**: Log files are automatically rotated daily at midnight by `TimedRotatingFileHandler`.
|
||||
- **Automatic Cleanup**: Log file retention is managed automatically based on the number of backup files to keep (`backupCount`), preventing excessive disk usage.
|
||||
- **Unified Format**: All log messages follow a consistent format: `[YYYY-MM-DD HH:MM:SS - LEVEL - message]`.
|
||||
- **Configurable Console Output**: Optional verbose console output for real-time monitoring, configurable via function arguments or environment variables.
|
||||
- **Unified Format**: All log messages follow a detailed, consistent format: `[YYYY-MM-DD HH:MM:SS - LEVEL - pathname:lineno - funcName] - message]`.
|
||||
- **Configurable Console Output**: Optional console output for real-time monitoring, configurable via function arguments or environment variables.
|
||||
|
||||
## Usage
|
||||
|
||||
@ -36,7 +36,7 @@ The `get_logger` function accepts the following parameters:
|
||||
|
||||
| Parameter | Type | Default | Description |
|
||||
|-------------------|---------------------|---------|-----------------------------------------------------------------------------|
|
||||
| `component_name` | `str` | - | Name of the component (e.g., `bot_manager`). Used for the logger name and directory. |
|
||||
| `component_name` | `str` | `default_logger` | Name of the component (e.g., `bot_manager`). Used for the logger name and directory. |
|
||||
| `log_level` | `str` | `INFO` | The minimum logging level to be processed (DEBUG, INFO, WARNING, ERROR, CRITICAL). |
|
||||
| `verbose` | `Optional[bool]` | `None` | If `True`, enables console logging. If `None`, uses `VERBOSE_LOGGING` or `LOG_TO_CONSOLE` from environment variables. |
|
||||
| `max_log_files` | `int` | `30` | The maximum number of backup log files to keep. The core of the log cleanup mechanism. |
|
||||
@ -58,6 +58,8 @@ logs/
|
||||
├── data_collector/
|
||||
│ ├── data_collector.log
|
||||
│ └── data_collector.log.2023-11-15
|
||||
└── default_logger/
|
||||
└── default_logger.log
|
||||
└── test_component/
|
||||
└── test_component.log
|
||||
```
|
||||
|
||||
@ -30,7 +30,7 @@ import threading
|
||||
# Lock for thread-safe logger configuration
|
||||
_lock = threading.Lock()
|
||||
|
||||
def get_logger(component_name: str, log_level: str = "INFO",
|
||||
def get_logger(component_name: str = "default_logger", log_level: str = "INFO",
|
||||
verbose: Optional[bool] = None, clean_old_logs: bool = True,
|
||||
max_log_files: int = 30) -> logging.Logger:
|
||||
"""
|
||||
@ -39,7 +39,8 @@ def get_logger(component_name: str, log_level: str = "INFO",
|
||||
This function is thread-safe and ensures that handlers are not duplicated.
|
||||
|
||||
Args:
|
||||
component_name: Name of the component (e.g., 'bot_manager', 'data_collector')
|
||||
component_name: Name of the component (e.g., 'bot_manager', 'data_collector').
|
||||
Defaults to 'default_logger' if not provided.
|
||||
log_level: Logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
|
||||
verbose: Enable console logging. If None, uses VERBOSE_LOGGING from .env
|
||||
clean_old_logs: (Deprecated) This is now handled by max_log_files.
|
||||
@ -74,7 +75,7 @@ def get_logger(component_name: str, log_level: str = "INFO",
|
||||
|
||||
# Unified formatter
|
||||
formatter = logging.Formatter(
|
||||
'[%(asctime)s - %(levelname)s - %(message)s]',
|
||||
'[%(asctime)s - %(levelname)s - %(pathname)s:%(lineno)d - %(funcName)s] - %(message)s',
|
||||
datefmt='%Y-%m-%d %H:%M:%S'
|
||||
)
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user