feat(logging): add configurable logging with file output support
Introduce --log-level and --log-file CLI arguments. Add execution time tracking and detailed logs across all modules.
This commit is contained in:
28
src/uipath_explainator/logging_utils.py
Normal file
28
src/uipath_explainator/logging_utils.py
Normal file
@@ -0,0 +1,28 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
import logging
|
||||
|
||||
|
||||
LOG_FORMAT = "%(asctime)s %(levelname)s [%(name)s] %(message)s"
|
||||
|
||||
|
||||
def configure_logging(level_name: str = "INFO", log_file: Path | None = None) -> None:
|
||||
level = getattr(logging, level_name.upper(), logging.INFO)
|
||||
formatter = logging.Formatter(LOG_FORMAT)
|
||||
|
||||
handlers: list[logging.Handler] = [logging.StreamHandler()]
|
||||
if log_file is not None:
|
||||
log_file.parent.mkdir(parents=True, exist_ok=True)
|
||||
handlers.append(logging.FileHandler(log_file, encoding="utf-8"))
|
||||
|
||||
root_logger = logging.getLogger()
|
||||
root_logger.handlers.clear()
|
||||
root_logger.setLevel(level)
|
||||
|
||||
for handler in handlers:
|
||||
handler.setLevel(level)
|
||||
handler.setFormatter(formatter)
|
||||
root_logger.addHandler(handler)
|
||||
|
||||
logging.captureWarnings(True)
|
||||
Reference in New Issue
Block a user