Добавлена базовая конфигурацию логера
This commit is contained in:
parent
c1c4215b7b
commit
9e7d5d9add
2 changed files with 47 additions and 0 deletions
0
src/core/__init__.py
Normal file
0
src/core/__init__.py
Normal file
47
src/core/logger.py
Normal file
47
src/core/logger.py
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
import logging
|
||||
import logging.config
|
||||
import os
|
||||
import yaml
|
||||
from pathlib import Path
|
||||
|
||||
TRACE_LEVEL_NUM = 5
|
||||
logging.addLevelName(TRACE_LEVEL_NUM, "TRACE")
|
||||
|
||||
def trace(self, message, *args, **kws):
|
||||
"""
|
||||
Trace для логирования
|
||||
Args:
|
||||
message (_type_): сообщение
|
||||
"""
|
||||
if self.isEnabledFor(TRACE_LEVEL_NUM):
|
||||
self._log(TRACE_LEVEL_NUM, message, args, **kws)
|
||||
|
||||
logging.Logger.trace = trace
|
||||
|
||||
def setup_logging():
|
||||
"""
|
||||
Сетапит логирование по конфигу
|
||||
Папки конфига захардкодены внутри проекта
|
||||
"""
|
||||
env = os.getenv("ENVIRONMENT", "dev").lower()
|
||||
root_dir = Path(__file__).resolve().parent.parent.parent # .parent.parent.parent -> AGENT (корень проекта)
|
||||
config_path = root_dir / "configs" / f"logging_{env}.yaml"
|
||||
|
||||
if config_path.exists():
|
||||
with open(config_path, "rt", encoding="utf-8") as f:
|
||||
try:
|
||||
config = yaml.safe_load(f)
|
||||
logging.config.dictConfig(config)
|
||||
logging.getLogger(__name__).info(f"Логирование настроено из: {config_path}")
|
||||
except Exception as e:
|
||||
print(f"Ошибка при парсинге {config_path}: {e}")
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
else:
|
||||
print(f"ВНИМАНИЕ: Конфиг {config_path} не найден. Базовая настройка (INFO).")
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
|
||||
setup_logging()
|
||||
|
||||
def get_logger(name: str) -> logging.Logger:
|
||||
return logging.getLogger(name)
|
||||
Loading…
Add table
Add a link
Reference in a new issue