From e26ba45a2e6e02d35794e54aa36d64ba7a045394 Mon Sep 17 00:00:00 2001 From: imbeacon Date: Wed, 6 Dec 2023 15:41:36 +0200 Subject: [PATCH] Fix for log handlers --- for_build/DEBIAN/postinst | 4 +--- .../gateway/tb_gateway_service.py | 4 +++- .../grpc_connectors/gw_grpc_connector.py | 5 +++-- .../tb_gateway_remote_configurator.py | 22 ++++++++++++++----- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/for_build/DEBIAN/postinst b/for_build/DEBIAN/postinst index 42864436..587e4a9b 100755 --- a/for_build/DEBIAN/postinst +++ b/for_build/DEBIAN/postinst @@ -35,12 +35,10 @@ sudo cp -a -r /etc/thingsboard-gateway/extensions /var/lib/thingsboard_gateway/ sudo rm -r /etc/thingsboard-gateway/extensions sudo usermod -a -G dialout $CURRENT_USER sudo usermod -a -G thingsboard_gateway $CURRENT_USER -sudo touch /tmp/gateway -sudo chown thingsboard_gateway:thingsboard_gateway /tmp/gateway sudo chown thingsboard_gateway:thingsboard_gateway /var/log/thingsboard-gateway/ -R sudo chown thingsboard_gateway:thingsboard_gateway /var/lib/thingsboard_gateway/ -R sudo chown thingsboard_gateway:thingsboard_gateway /etc/thingsboard-gateway/ -R -sudo sed -i 's/\.\/logs/\/var\/log\/thingsboard-gateway/g' /etc/thingsboard-gateway/config/logs.conf >> /etc/thingsboard-gateway/config/logs.conf +sudo sed -i 's/\.\/logs/\/var\/log\/thingsboard-gateway/g' /etc/thingsboard-gateway/config/logs.json >> /etc/thingsboard-gateway/config/logs.json echo "Installation completed" echo "Enabling daemon..." sudo pidof systemd && sudo systemctl enable thingsboard-gateway || echo "Systemctl not found, cannot enable the daemon!" diff --git a/thingsboard_gateway/gateway/tb_gateway_service.py b/thingsboard_gateway/gateway/tb_gateway_service.py index b1c73472..98b1b75e 100644 --- a/thingsboard_gateway/gateway/tb_gateway_service.py +++ b/thingsboard_gateway/gateway/tb_gateway_service.py @@ -59,7 +59,7 @@ try: except ImportError: print("Cannot load GRPC connector!") -log = None +log:TbLogger = None main_handler = logging.handlers.MemoryHandler(-1) DEFAULT_CONNECTORS = { @@ -515,6 +515,8 @@ class TBGatewayService: if self.__grpc_manager is not None: self.__grpc_manager.stop() + if os.path.exists("/tmp/gateway"): + os.remove("/tmp/gateway") self.__close_connectors() self._event_storage.stop() log.info("The gateway has been stopped.") diff --git a/thingsboard_gateway/grpc_connectors/gw_grpc_connector.py b/thingsboard_gateway/grpc_connectors/gw_grpc_connector.py index 1a88b408..f9e0fe0b 100644 --- a/thingsboard_gateway/grpc_connectors/gw_grpc_connector.py +++ b/thingsboard_gateway/grpc_connectors/gw_grpc_connector.py @@ -20,9 +20,10 @@ from typing import Union import simplejson from simplejson import dumps + +from thingsboard_gateway.gateway.proto.messages_pb2 import * from thingsboard_gateway.grpc_connectors.gw_grpc_client import GrpcClient from thingsboard_gateway.grpc_connectors.gw_grpc_msg_creator import GrpcMsgCreator, Status -from thingsboard_gateway.gateway.proto.messages_pb2 import * log = getLogger('connector') @@ -30,7 +31,7 @@ log = getLogger('connector') class GwGrpcConnector(Thread): def __init__(self, connector_config: str, config_dir_path: str): super().__init__() - fileConfig(config_dir_path + 'logs.conf') + fileConfig(config_dir_path + 'logs.json') global log log = getLogger('connector') self.stopped = False diff --git a/thingsboard_gateway/tb_utility/tb_gateway_remote_configurator.py b/thingsboard_gateway/tb_utility/tb_gateway_remote_configurator.py index f652a633..852d2120 100644 --- a/thingsboard_gateway/tb_utility/tb_gateway_remote_configurator.py +++ b/thingsboard_gateway/tb_utility/tb_gateway_remote_configurator.py @@ -14,12 +14,12 @@ import os.path from logging import getLogger -from time import sleep, time from logging.config import dictConfig +from time import sleep, time +from packaging import version from regex import fullmatch from simplejson import dumps, load -from packaging import version from thingsboard_gateway.gateway.tb_client import TBClient from thingsboard_gateway.tb_utility.tb_handler import TBLoggerHandler @@ -231,8 +231,8 @@ class RemoteConfigurator: try: with open(self._gateway.get_config_path() + 'logs.json', 'r') as logs: return load(logs) - except Exception as e: - LOG.exception(e) + except Exception: + LOG.warning("Cannot open logs configuration file. Using default logs configuration.") return {} def process_config_request(self, config): @@ -383,7 +383,17 @@ class RemoteConfigurator: try: LOG = getLogger('service') logs_conf_file_path = self._gateway.get_config_path() + 'logs.json' - + target_handlers = {} + for handler in config['handlers']: + filename = config['handlers'][handler].get('filename') + if "consoleHandler" == handler or (filename is not None and os.path.exists(filename)): + target_handlers[handler] = config['handlers'][handler] + else: + LOG.warning('Handler %s not found. Removing from configuration...', handler) + for logger in config['loggers']: + if handler in config['loggers'][logger]['handlers']: + config['loggers'][logger]['handlers'].remove(handler) + config['handlers'] = target_handlers dictConfig(config) LOG = getLogger('service') self._gateway.remote_handler = TBLoggerHandler(self._gateway) @@ -397,7 +407,7 @@ class RemoteConfigurator: LOG.debug("Logs configuration has been updated.") self._gateway.tb_client.client.send_attributes({'logs_configuration': config}) except Exception as e: - LOG.error("Remote logging configuration is wrong!") + LOG.error("Remote logging configuration is wrong, cannot apply it!") LOG.exception(e) def _handle_active_connectors_update(self, config):