Compare commits

...

2 commits

5 changed files with 101 additions and 0 deletions

18
configs/logging_dev.yaml Normal file
View file

@ -0,0 +1,18 @@
version: 1
disable_existing_loggers: false
formatters:
dev_formatter:
format: "%(asctime)s | %(levelname)-7s | %(name)s | %(message)s"
datefmt: "%H:%M:%S"
handlers:
console:
class: logging.StreamHandler
level: TRACE
formatter: dev_formatter
stream: ext://sys.stdout
root:
level: TRACE
handlers: [console]

18
configs/logging_prod.yaml Normal file
View file

@ -0,0 +1,18 @@
version: 1
disable_existing_loggers: false
formatters:
prod_formatter:
format: "%(asctime)s | %(levelname)-7s | %(name)s | %(message)s"
datefmt: "%Y-%m-%d %H:%M:%S"
handlers:
console:
class: logging.StreamHandler
level: INFO
formatter: prod_formatter
stream: ext://sys.stdout
root:
level: INFO
handlers: [file]

18
configs/logging_test.yaml Normal file
View file

@ -0,0 +1,18 @@
version: 1
disable_existing_loggers: false
formatters:
test_formatter:
format: "%(asctime)s | %(levelname)-7s | %(name)s | %(message)s"
datefmt: "%Y-%m-%d %H:%M:%S"
handlers:
console:
class: logging.StreamHandler
level: DEBUG
formatter: test_formatter
stream: ext://sys.stdout
root:
level: DEBUG
handlers: [file]

0
src/core/__init__.py Normal file
View file

47
src/core/logger.py Normal file
View 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)