From c1c4215b7bccf6bfe956f96e125ae601e2d40992 Mon Sep 17 00:00:00 2001 From: collhoun <2904yr@mail.ru> Date: Wed, 22 Apr 2026 22:55:19 +0300 Subject: [PATCH 1/2] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B=20=D0=BA=D0=BE?= =?UTF-8?q?=D0=BD=D1=84=D0=B8=D0=B3=D0=B0=20=D0=BB=D0=BE=D0=B3=D0=B8=D1=80?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- configs/logging_dev.yaml | 18 ++++++++++++++++++ configs/logging_prod.yaml | 18 ++++++++++++++++++ configs/logging_test.yaml | 18 ++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 configs/logging_dev.yaml create mode 100644 configs/logging_prod.yaml create mode 100644 configs/logging_test.yaml diff --git a/configs/logging_dev.yaml b/configs/logging_dev.yaml new file mode 100644 index 0000000..9f15197 --- /dev/null +++ b/configs/logging_dev.yaml @@ -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] diff --git a/configs/logging_prod.yaml b/configs/logging_prod.yaml new file mode 100644 index 0000000..56713e0 --- /dev/null +++ b/configs/logging_prod.yaml @@ -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] \ No newline at end of file diff --git a/configs/logging_test.yaml b/configs/logging_test.yaml new file mode 100644 index 0000000..564c98d --- /dev/null +++ b/configs/logging_test.yaml @@ -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] \ No newline at end of file From 9e7d5d9add9c103059c39d76d1c15c355ffde88a Mon Sep 17 00:00:00 2001 From: collhoun <2904yr@mail.ru> Date: Wed, 22 Apr 2026 22:56:11 +0300 Subject: [PATCH 2/2] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=B1=D0=B0=D0=B7=D0=BE=D0=B2=D0=B0=D1=8F=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=BD=D1=84=D0=B8=D0=B3=D1=83=D1=80=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D1=8E=20=D0=BB=D0=BE=D0=B3=D0=B5=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/__init__.py | 0 src/core/logger.py | 47 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 src/core/__init__.py create mode 100644 src/core/logger.py diff --git a/src/core/__init__.py b/src/core/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/core/logger.py b/src/core/logger.py new file mode 100644 index 0000000..21a6af9 --- /dev/null +++ b/src/core/logger.py @@ -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) \ No newline at end of file