From 0f169693933bb48a93cd97f6ec4b5d91e1e6e7fc Mon Sep 17 00:00:00 2001 From: samson0v Date: Mon, 16 Dec 2024 10:47:28 +0200 Subject: [PATCH] Moved file handler updating to TbLogger class, added file handlers class name checking --- .../gateway/tb_gateway_service.py | 22 +------------------ .../tb_gateway_remote_configurator.py | 7 ++++++ thingsboard_gateway/tb_utility/tb_logger.py | 20 +++++++++++++++++ 3 files changed, 28 insertions(+), 21 deletions(-) diff --git a/thingsboard_gateway/gateway/tb_gateway_service.py b/thingsboard_gateway/gateway/tb_gateway_service.py index 61fa4b24..ad62a513 100644 --- a/thingsboard_gateway/gateway/tb_gateway_service.py +++ b/thingsboard_gateway/gateway/tb_gateway_service.py @@ -59,7 +59,6 @@ from thingsboard_gateway.tb_utility.tb_loader import TBModuleLoader from thingsboard_gateway.tb_utility.tb_logger import TbLogger from thingsboard_gateway.tb_utility.tb_remote_shell import RemoteShell -from thingsboard_gateway.tb_utility.tb_rotating_file_handler import TimedRotatingFileHandler from thingsboard_gateway.tb_utility.tb_updater import TBUpdater from thingsboard_gateway.tb_utility.tb_utility import TBUtility @@ -2066,7 +2065,7 @@ def get_status(self): def update_loggers(self): self.__update_base_loggers() - self.__update_connectors_and_converters_loggers() + TbLogger.update_file_handlers() global log log = logging.getLogger('service') @@ -2083,25 +2082,6 @@ def __update_base_loggers(self): logger.addHandler(self.remote_handler) - def __update_connectors_and_converters_loggers(self): - for logger in logging.Logger.manager.loggerDict.values(): - if hasattr(logger, 'is_connector_logger') or hasattr(logger, 'is_converter_logger'): - file_handler_filter = list(filter(lambda handler: isinstance(handler, TimedRotatingFileHandler), - logger.handlers)) - if len(file_handler_filter): - old_file_handler = file_handler_filter[0] - - new_file_handler = None - if logger.is_connector_logger: - new_file_handler = TimedRotatingFileHandler.get_connector_file_handler(old_file_handler.baseFilename.split('/')[-1]) # noqa - - if logger.is_converter_logger: - new_file_handler = TimedRotatingFileHandler.get_converter_file_handler(old_file_handler.baseFilename.split('/')[-1]) # noqa - - if new_file_handler: - logger.addHandler(new_file_handler) - logger.removeHandler(old_file_handler) - def is_latency_metrics_enabled(self): return self.__latency_debug_mode diff --git a/thingsboard_gateway/tb_utility/tb_gateway_remote_configurator.py b/thingsboard_gateway/tb_utility/tb_gateway_remote_configurator.py index 219faf50..49ea448d 100644 --- a/thingsboard_gateway/tb_utility/tb_gateway_remote_configurator.py +++ b/thingsboard_gateway/tb_utility/tb_gateway_remote_configurator.py @@ -421,6 +421,7 @@ def _handle_grpc_configuration_update(self, config): def _handle_logs_configuration_update(self, config): self.__log.debug('Processing logs configuration update...') try: + self.__check_file_handlers_class_name(config) self.__log = getLogger('service') logs_conf_file_path = self._gateway.get_config_path() + 'logs.json' target_handlers = {} @@ -994,3 +995,9 @@ def __is_username_password_match(old_security, new_security): def __is_ca_cert_match(old_security, new_security): return new_security.get('accessToken') == old_security.get('accessToken') and \ new_security.get('caCert') == old_security.get('caCert') + + @staticmethod + def __check_file_handlers_class_name(config): + for handler_config in config.get('handlers', {}).values(): + if handler_config.get('class', '') == 'thingsboard_gateway.tb_utility.tb_handler.TimedRotatingFileHandler': + handler_config['class'] = 'thingsboard_gateway.tb_utility.tb_rotating_file_handler.TimedRotatingFileHandler' diff --git a/thingsboard_gateway/tb_utility/tb_logger.py b/thingsboard_gateway/tb_utility/tb_logger.py index 53ed45a7..07a0ffe3 100644 --- a/thingsboard_gateway/tb_utility/tb_logger.py +++ b/thingsboard_gateway/tb_utility/tb_logger.py @@ -236,5 +236,25 @@ def send_errors_if_needed(cls, gateway): def is_main_module_logger(name, attr_name, is_converter_logger): return name == attr_name or attr_name is None or (name != attr_name and is_converter_logger) + @staticmethod + def update_file_handlers(): + for logger in logging.Logger.manager.loggerDict.values(): + if hasattr(logger, 'is_connector_logger') or hasattr(logger, 'is_converter_logger'): + file_handler_filter = list(filter(lambda handler: isinstance(handler, TimedRotatingFileHandler), + logger.handlers)) + if len(file_handler_filter): + old_file_handler = file_handler_filter[0] + + new_file_handler = None + if logger.is_connector_logger: + new_file_handler = TimedRotatingFileHandler.get_connector_file_handler(old_file_handler.baseFilename.split('/')[-1]) # noqa + + if logger.is_converter_logger: + new_file_handler = TimedRotatingFileHandler.get_converter_file_handler(old_file_handler.baseFilename.split('/')[-1]) # noqa + + if new_file_handler: + logger.addHandler(new_file_handler) + logger.removeHandler(old_file_handler) + logging.setLoggerClass(TbLogger)