From 947f04eb7f369d5f856a4d886dd07ddade605a06 Mon Sep 17 00:00:00 2001 From: MarcusStill <69536339+MarcusStill@users.noreply.github.com> Date: Mon, 7 Sep 2026 03:28:46 +0000 Subject: [PATCH] fix(logger): resolve delayed and lost loguru logs on exit - Added `buffering=1` to `logger.add()` to force line-buffered logs so log messages are flushed to disk in real-time instead of blocked. - Fixed lost error/crash logs by placing `logger.complete()` in global exit hooks, uncaught exception hooks, and connection failures. - Added support for reading and parsing the dynamic `log_level` parameter from the `config.ini` [OTHER] section to replace hardcoded log levels. - Replaced the split standard logging setup in `modules/config.py` with the central application logger. --- main.py | 8 ++++++-- modules/config.py | 5 +++-- modules/system.py | 2 ++ 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/main.py b/main.py index c7f54d0..321e438 100644 --- a/main.py +++ b/main.py @@ -55,7 +55,7 @@ system = System() config_data = system.config -logger.add(system.log_file, rotation="1 MB", enqueue=True) +logger.add(system.log_file, rotation="1 MB", enqueue=True, buffering=1, level=system.log_level) def handle_uncaught_exception(exc_type, exc_value, exc_traceback): @@ -66,6 +66,7 @@ def handle_uncaught_exception(exc_type, exc_value, exc_traceback): logger.opt(exception=(exc_type, exc_value, exc_traceback)).error( "Неперехваченное исключение" ) + logger.complete() sys.excepthook = handle_uncaught_exception @@ -103,6 +104,7 @@ def starting_the_main_form(self) -> None: "Не удалось подключиться к серверу базы данных.\n Приложение будет закрыто.", "" ) + logger.complete() sys.exit(1) login: str = self.ui.lineEdit.text() password: str = self.ui.lineEdit_2.text() @@ -3559,4 +3561,6 @@ class Payment: auth = AuthForm() auth.show() auth.ui.label_9.setText(system.database) - sys.exit(app.exec()) + ret = app.exec() + logger.complete() + sys.exit(ret) diff --git a/modules/config.py b/modules/config.py index 3cb0a17..ca1c246 100644 --- a/modules/config.py +++ b/modules/config.py @@ -1,4 +1,3 @@ -import logging import os from configparser import ( ConfigParser, @@ -8,7 +7,7 @@ ) from typing import List, Optional -logger = logging.getLogger(__name__) +from modules.logger import logger class Config: @@ -122,6 +121,8 @@ def load_config(self): ) config_data[key] = config.get(section, key) + if config.has_option("OTHER", "log_level"): + config_data["log_level"] = config.get("OTHER", "log_level") return config_data except (NoSectionError, NoOptionError) as e: diff --git a/modules/system.py b/modules/system.py index 34c8fdf..5372387 100644 --- a/modules/system.py +++ b/modules/system.py @@ -38,6 +38,8 @@ def __init__(self): self.user = self.config.get("user") self.software_version = self.config.get("version") self.log_file = self.config.get("log_file") + log_level_conf = self.config.get("log_level") + self.log_level = log_level_conf.upper() if log_level_conf else "DEBUG" # Информация о РМ self.kol_pc = int(self.config.get("kol")) self.pcs = self.config.pcs # список рабочих станций