From 27309f53d9fa0ba9a2ca07a65feed96bf38f470c Mon Sep 17 00:00:00 2001 From: Fringg Date: Tue, 17 Feb 2026 05:15:03 +0300 Subject: [PATCH] feat: add LOG_COLORS env setting to toggle console ANSI colors --- .env.example | 2 ++ app/config.py | 1 + app/logging_config.py | 31 +++++++++++++++++++------------ 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/.env.example b/.env.example index 37366091..f1d6c807 100644 --- a/.env.example +++ b/.env.example @@ -845,6 +845,8 @@ VERSION_CHECK_INTERVAL_HOURS=1 # ===== ЛОГИРОВАНИЕ ===== LOG_LEVEL=INFO LOG_FILE=logs/bot.log +# ANSI-цвета в консоли (true — цветной вывод с Rich, false — plain-text) +LOG_COLORS=true # === Ротация логов === # Включить новую систему ротации (по умолчанию старое поведение) diff --git a/app/config.py b/app/config.py index 31dc929c..826e005d 100644 --- a/app/config.py +++ b/app/config.py @@ -549,6 +549,7 @@ class Settings(BaseSettings): LOG_LEVEL: str = 'INFO' LOG_FILE: str = 'logs/bot.log' + LOG_COLORS: bool = True # ANSI-цвета в консоли (false для plain-text вывода) # === Log Rotation Settings === LOG_ROTATION_ENABLED: bool = False # По умолчанию старое поведение diff --git a/app/logging_config.py b/app/logging_config.py index b1308c98..3f189749 100644 --- a/app/logging_config.py +++ b/app/logging_config.py @@ -121,24 +121,31 @@ def setup_logging() -> tuple[logging.Formatter, logging.Formatter, Any]: ], ) - # Console formatter: colors enabled by default on non-Windows. + # Console formatter: colors controlled by LOG_COLORS env var (default: true). # Rich tracebacks with conservative limits to avoid 5000-line dumps. + use_colors = settings.LOG_COLORS + console_renderer_kwargs: dict[str, Any] = { + 'colors': use_colors, + 'pad_event_to': 0, + 'pad_level': False, + } + if use_colors: + console_renderer_kwargs['exception_formatter'] = structlog.dev.RichTracebackFormatter( + show_locals=False, + max_frames=20, + extra_lines=1, + width=120, + suppress=['aiogram', 'aiohttp'], + ) + else: + console_renderer_kwargs['exception_formatter'] = structlog.dev.plain_traceback + console_formatter = structlog.stdlib.ProcessorFormatter( foreign_pre_chain=shared_processors, processors=[ structlog.stdlib.ProcessorFormatter.remove_processors_meta, _prefix_logger_name, - structlog.dev.ConsoleRenderer( - pad_event_to=0, - pad_level=False, - exception_formatter=structlog.dev.RichTracebackFormatter( - show_locals=False, - max_frames=20, - extra_lines=1, - width=120, - suppress=['aiogram', 'aiohttp'], - ), - ), + structlog.dev.ConsoleRenderer(**console_renderer_kwargs), ], )