Skip to content

Commit

Permalink
Added separated loggers for connector and connector, fixed adding fil…
Browse files Browse the repository at this point in the history
…e handler for loggers
  • Loading branch information
samson0v committed Dec 13, 2024
1 parent e214f6b commit b5f5e24
Show file tree
Hide file tree
Showing 19 changed files with 200 additions and 83 deletions.
14 changes: 7 additions & 7 deletions thingsboard_gateway/config/logs.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
"stream": "ext://sys.stdout"
},
"databaseHandler": {
"class": "thingsboard_gateway.tb_utility.tb_handler.TimedRotatingFileHandler",
"class": "thingsboard_gateway.tb_utility.tb_rotating_file_handler.TimedRotatingFileHandler",
"formatter": "LogFormatter",
"filename": "./logs/database.log",
"backupCount": 1,
"encoding": "utf-8"
},
"serviceHandler": {
"class": "thingsboard_gateway.tb_utility.tb_handler.TimedRotatingFileHandler",
"class": "thingsboard_gateway.tb_utility.tb_rotating_file_handler.TimedRotatingFileHandler",
"formatter": "LogFormatter",
"filename": "./logs/service.log",
"backupCount": 7,
Expand All @@ -32,7 +32,7 @@
"encoding": "utf-8"
},
"connectorHandler": {
"class": "thingsboard_gateway.tb_utility.tb_handler.TimedRotatingFileHandler",
"class": "thingsboard_gateway.tb_utility.tb_rotating_file_handler.TimedRotatingFileHandler",
"formatter": "LogFormatter",
"filename": "./logs/connector.log",
"backupCount": 7,
Expand All @@ -41,7 +41,7 @@
"encoding": "utf-8"
},
"converterHandler": {
"class": "thingsboard_gateway.tb_utility.tb_handler.TimedRotatingFileHandler",
"class": "thingsboard_gateway.tb_utility.tb_rotating_file_handler.TimedRotatingFileHandler",
"formatter": "LogFormatter",
"filename": "./logs/converter.log",
"backupCount": 7,
Expand All @@ -50,7 +50,7 @@
"encoding": "utf-8"
},
"tb_connectionHandler": {
"class": "thingsboard_gateway.tb_utility.tb_handler.TimedRotatingFileHandler",
"class": "thingsboard_gateway.tb_utility.tb_rotating_file_handler.TimedRotatingFileHandler",
"formatter": "LogFormatter",
"filename": "./logs/tb_connection.log",
"backupCount": 7,
Expand All @@ -59,7 +59,7 @@
"encoding": "utf-8"
},
"storageHandler": {
"class": "thingsboard_gateway.tb_utility.tb_handler.TimedRotatingFileHandler",
"class": "thingsboard_gateway.tb_utility.tb_rotating_file_handler.TimedRotatingFileHandler",
"formatter": "LogFormatter",
"filename": "./logs/storage.log",
"backupCount": 7,
Expand All @@ -68,7 +68,7 @@
"encoding": "utf-8"
},
"extensionHandler": {
"class": "thingsboard_gateway.tb_utility.tb_handler.TimedRotatingFileHandler",
"class": "thingsboard_gateway.tb_utility.tb_rotating_file_handler.TimedRotatingFileHandler",
"formatter": "LogFormatter",
"filename": "./logs/extension.log",
"backupCount": 7,
Expand Down
6 changes: 3 additions & 3 deletions thingsboard_gateway/connectors/bacnet/bacnet_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ def __init__(self, gateway, config, connector_type):

self.__log = init_logger(self.__gateway, self.name, log_level,
enable_remote_logging=remote_logging,
is_connector_logger=True, connector_name=self.name)
self.__converter_log = init_logger(self.__gateway, self.name, log_level,
is_connector_logger=True)
self.__converter_log = init_logger(self.__gateway, self.name + '_converter', log_level,
enable_remote_logging=remote_logging,
is_converter_logger=True, connector_name=self.name)
is_converter_logger=True, attr_name=self.name)
self.__log.info('Starting BACnet connector...')

if BackwardCompatibilityAdapter.is_old_config(config):
Expand Down
12 changes: 9 additions & 3 deletions thingsboard_gateway/connectors/ble/ble_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,14 @@ def __init__(self, gateway, config, connector_type):
self.__config = config
self.__id = self.__config.get('id')
self.name = self.__config.get("name", 'BLE Connector ' + ''.join(choice(ascii_lowercase) for _ in range(5)))
self.__log = init_logger(self.__gateway, self.name, self.__config.get('logLevel', 'INFO'),
enable_remote_logging=self.__config.get('enableRemoteLogging', False))
self.__log = init_logger(self.__gateway, self.name,
self.__config.get('logLevel', 'INFO'),
enable_remote_logging=self.__config.get('enableRemoteLogging', False),
is_connector_logger=True)
self.__converter_log = init_logger(self.__gateway, self.name + '_converter',
self.__config.get('logLevel', 'INFO'),
enable_remote_logging=self.__config.get('enableRemoteLogging', False),
is_converter_logger=True, attr_name=self.name)

self.daemon = True

Expand Down Expand Up @@ -138,7 +144,7 @@ def __process_data(self):
StatisticsService.count_connector_bytes(self.name, data, stat_parameter_name='connectorBytesReceived')

try:
converter = converter(device_config, self.__log)
converter = converter(device_config, self.__converter_log)
converted_data: ConvertedData = converter.convert(config, data)
self.statistics['MessagesReceived'] = self.statistics['MessagesReceived'] + 1
self.__log.debug(converted_data)
Expand Down
11 changes: 8 additions & 3 deletions thingsboard_gateway/connectors/can/can_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,12 @@ def __init__(self, gateway, config, connector_type):
self.__config = config
self.__id = self.__config.get('id')
self._log = init_logger(self.__gateway, self.name, self.__config.get('logLevel', 'INFO'),
enable_remote_logging=self.__config.get('enableRemoteLogging', False))
enable_remote_logging=self.__config.get('enableRemoteLogging', False),
is_connector_logger=True)
self._converter_log = init_logger(self.__gateway, self.name + '_converter',
self.__config.get('logLevel', 'INFO'),
enable_remote_logging=self.__config.get('enableRemoteLogging', False),
is_converter_logger=True, attr_name=self.name)
self.__bus_conf = {}
self.__bus = None
self.__reconnect_count = 0
Expand Down Expand Up @@ -591,11 +596,11 @@ def __get_converter(self, config, need_uplink):
else:
if need_uplink:
uplink = config.get("uplink")
return BytesCanUplinkConverter(self._log) if uplink is None \
return BytesCanUplinkConverter(self._converter_log) if uplink is None \
else TBModuleLoader.import_module(self._connector_type, uplink)
else:
downlink = config.get("downlink")
return BytesCanDownlinkConverter(self._log) if downlink is None \
return BytesCanDownlinkConverter(self._converter_log) if downlink is None \
else TBModuleLoader.import_module(self._connector_type, downlink)

def get_config(self):
Expand Down
9 changes: 7 additions & 2 deletions thingsboard_gateway/connectors/ftp/ftp_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ def __init__(self, gateway, config, connector_type):
self.__tls_support = self.__config.get("TLSSupport", False)
self.name = self.__config.get("name", "".join(choice(ascii_lowercase) for _ in range(5)))
self.__log = init_logger(self.__gateway, self.name, self.__config.get('logLevel', 'INFO'),
enable_remote_logging=self.__config.get('enableRemoteLogging', False))
enable_remote_logging=self.__config.get('enableRemoteLogging', False),
is_connector_logger=True)
self.__converter_log = init_logger(self.__gateway, self.name + '_converter',
self.__config.get('logLevel', 'INFO'),
enable_remote_logging=self.__config.get('enableRemoteLogging', False),
is_converter_logger=True, attr_name=self.name)
self.daemon = True
self.__stopped = False
self.__requests_in_progress = []
Expand Down Expand Up @@ -135,7 +140,7 @@ def __process_paths(self, ftp):
time_point = timer()
if time_point - path.last_polled_time >= path.poll_period or path.last_polled_time == 0:
configuration = path.config
converter = FTPUplinkConverter(configuration, self.__log)
converter = FTPUplinkConverter(configuration, self.__converter_log)
path.last_polled_time = time_point

if '*' in path.path:
Expand Down
7 changes: 6 additions & 1 deletion thingsboard_gateway/connectors/modbus/modbus_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,12 @@ def __init__(self, gateway, config, connector_type):
self.name = self.__config.get("name", 'Modbus Connector ' + ''.join(choice(ascii_lowercase) for _ in range(5)))
self.__log = init_logger(self.__gateway, config.get('name', self.name),
config.get('logLevel', 'INFO'),
enable_remote_logging=config.get('enableRemoteLogging', False))
enable_remote_logging=config.get('enableRemoteLogging', False),
is_connector_logger=True)
self.__converter_log = init_logger(self.__gateway, self.name + '_converter',
config.get('logLevel', 'INFO'),
enable_remote_logging=config.get('enableRemoteLogging', False),
is_converter_logger=True, attr_name=self.name)
self.__log.info('Starting Modbus Connector...')
self.__id = self.__config.get('id')
self.__connected = False
Expand Down
4 changes: 2 additions & 2 deletions thingsboard_gateway/connectors/modbus/slave.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ def __load_converter(self, config, converter_type, converter_config: Union[Dict,
converter = config[converter_type + CONVERTER_PARAMETER]
else:
if converter_type == DOWNLINK_PREFIX:
converter = BytesModbusDownlinkConverter(converter_config, self._log)
converter = BytesModbusDownlinkConverter(converter_config, self.connector.__converter_log)
else:
converter = BytesModbusUplinkConverter(converter_config, self._log)
converter = BytesModbusUplinkConverter(converter_config, self.connector.__converter_log)

return converter
except Exception as e:
Expand Down
6 changes: 5 additions & 1 deletion thingsboard_gateway/connectors/mqtt/mqtt_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ def __init__(self, gateway, config, connector_type):
config.get('logLevel', 'INFO'),
enable_remote_logging=config.get('enableRemoteLogging', False),
is_connector_logger=True)
self.__converter_log = init_logger(self.__gateway, config['name'] + '_converter',
config.get('logLevel', 'INFO'),
enable_remote_logging=config.get('enableRemoteLogging', False),
is_converter_logger=True, attr_name=config['name'])

# check if the configuration is in the old format
using_old_config_format_detected = BackwardCompatibilityAdapter.is_old_config_format(config)
Expand Down Expand Up @@ -454,7 +458,7 @@ def _on_connect(self, client, userdata, flags, result_code, *extra_params):
if module:
self.__log.debug('Converter %s for topic %s - found!', converter_class_name,
mapping["topicFilter"])
converter = module(mapping, self.__log)
converter = module(mapping, self.__converter_log)
if sharing_id:
self.__shared_custom_converters[sharing_id] = converter
else:
Expand Down
9 changes: 7 additions & 2 deletions thingsboard_gateway/connectors/ocpp/ocpp_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ def __init__(self, gateway, config, connector_type):
self._gateway = gateway
self.name = self._config.get("name", 'OCPP Connector ' + ''.join(choice(ascii_lowercase) for _ in range(5)))
self._log = init_logger(self._gateway, self.name, self._config.get('logLevel', 'INFO'),
enable_remote_logging=self._config.get('enableRemoteLogging', False))
enable_remote_logging=self._config.get('enableRemoteLogging', False),
is_connector_logger=True)
self._converter_log = init_logger(self._gateway, self.name + '_converter',
self._config.get('logLevel', 'INFO'),
enable_remote_logging=self._config.get('enableRemoteLogging', False),
is_converter_logger=True, attr_name=self.name)

self._default_converters = {'uplink': 'OcppUplinkConverter'}
self._server = None
Expand Down Expand Up @@ -184,7 +189,7 @@ async def on_connect(self, websocket, path):
if is_valid:
uplink_converter_name = cp_config.get('extension', self._default_converters['uplink'])
cp = ChargePoint(charge_point_id, websocket, {**cp_config, 'uplink_converter_name': uplink_converter_name},
OcppConnector._callback, self._log)
OcppConnector._callback, self._converter_log)
cp.authorized = True

self._log.info('Connected Charge Point with id: %s', charge_point_id)
Expand Down
9 changes: 7 additions & 2 deletions thingsboard_gateway/connectors/odbc/odbc_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ def __init__(self, gateway, config, connector_type):
self.__config = config
self.__id = self.__config.get('id')
self._log = init_logger(self.__gateway, self.name, self.__config.get('logLevel', 'INFO'),
enable_remote_logging=self.__config.get('enableRemoteLogging', False))
enable_remote_logging=self.__config.get('enableRemoteLogging', False),
is_connector_logger=True)
self._converter_log = init_logger(self.__gateway, self.name + '_converter',
self.__config.get('logLevel', 'INFO'),
enable_remote_logging=self.__config.get('enableRemoteLogging', False),
is_converter_logger=True, attr_name=self.name)
self._connector_type = connector_type
self.__stopped = False

Expand All @@ -81,7 +86,7 @@ def __init__(self, gateway, config, connector_type):
self.__attribute_columns = []
self.__timeseries_columns = []

self.__converter = OdbcUplinkConverter(self._log) if not self.__config.get("converter", "") else \
self.__converter = OdbcUplinkConverter(self._converter_log) if not self.__config.get("converter", "") else \
TBModuleLoader.import_module(self._connector_type, self.__config["converter"])

self.__configure_pyodbc()
Expand Down
12 changes: 8 additions & 4 deletions thingsboard_gateway/connectors/opcua/opcua_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ def __init__(self, gateway: 'TBGatewayService', config, connector_type):
self.__config.get('server', {}).get('mapping', []))))
self.__enable_remote_logging = self.__config.get('enableRemoteLogging', False)
self.__log = init_logger(self.__gateway, self.name, self.__config.get('logLevel', 'INFO'),
enable_remote_logging=self.__enable_remote_logging)
enable_remote_logging=self.__enable_remote_logging, is_connector_logger=True)
self.__converter_log = init_logger(self.__gateway, self.name + '_converter',
self.__config.get('logLevel', 'INFO'),
enable_remote_logging=self.__enable_remote_logging,
is_converter_logger=True, attr_name=self.name)
self.__replace_loggers()
report_strategy = self.__config.get('reportStrategy')
self.__connector_report_strategy_config = gateway.get_report_strategy_service().get_main_report_strategy()
Expand Down Expand Up @@ -150,7 +154,7 @@ def __replace_loggers(self):
'ERROR',
enable_remote_logging=self.__enable_remote_logging,
is_connector_logger=True,
connector_name=self.name)
attr_name=self.name)

def open(self):
self.__stopped = False
Expand Down Expand Up @@ -638,9 +642,9 @@ async def _create_new_devices(self):
device_config = {**device_config, 'device_name': device_name, 'device_type': device_type}
self.__device_nodes.append(
Device(path=node, name=device_name, config=device_config,
converter=converter(device_config, self.__log),
converter=converter(device_config, self.__converter_log),
converter_for_sub=converter(device_config,
self.__log) if self.__enable_subscriptions else None,
self.__converter_log) if self.__enable_subscriptions else None,
logger=self.__log))
self.__log.info('Added device node: %s', device_name)
self.__log.debug('Device nodes: %s', self.__device_nodes)
Expand Down
Loading

0 comments on commit b5f5e24

Please sign in to comment.