Skip to content

Commit

Permalink
Moved file handler updating to TbLogger class, added file handlers cl…
Browse files Browse the repository at this point in the history
…ass name checking
  • Loading branch information
samson0v committed Dec 16, 2024
1 parent 4604d8f commit 0f16969
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
22 changes: 1 addition & 21 deletions thingsboard_gateway/gateway/tb_gateway_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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')
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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'
20 changes: 20 additions & 0 deletions thingsboard_gateway/tb_utility/tb_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 0f16969

Please sign in to comment.