Skip to content

Commit

Permalink
Merge pull request #1597 from samson0v/issues/iotgw-376
Browse files Browse the repository at this point in the history
Fixed logging
  • Loading branch information
samson0v authored Nov 13, 2024
2 parents c35927e + 18d7300 commit 86dd2ac
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 109 deletions.
21 changes: 18 additions & 3 deletions thingsboard_gateway/gateway/tb_gateway_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class TBGRPCServerManager:
log: TbLogger = None # type: ignore
logging.setLoggerClass(TbLogger)
main_handler = logging.handlers.MemoryHandler(-1)
main_handler.setLevel(0)

DEFAULT_CONNECTORS = {
"mqtt": "MqttConnector",
Expand Down Expand Up @@ -169,7 +170,8 @@ def __init__(self, config_file=None):

global log
log = logging.getLogger('service')
log.setLevel('INFO')
if logging_error is not None:
log.setLevel('INFO')
global main_handler
self.main_handler = main_handler
log.addHandler(self.main_handler)
Expand Down Expand Up @@ -217,7 +219,8 @@ def __init__(self, config_file=None):
self.send_telemetry({"ts": time() * 1000, "values": {
"LOGS": "Logging loading exception, logs.json is wrong: %s" % (str(logging_error),)}})
TBLoggerHandler.set_default_handler()
self.remote_handler = TBLoggerHandler(self)
if not hasattr(self, "remote_handler"):
self.remote_handler = TBLoggerHandler(self)
log.addHandler(self.remote_handler)
self.__debug_log_enabled = log.isEnabledFor(10)
self.update_loggers()
Expand Down Expand Up @@ -682,7 +685,6 @@ def __process_remote_logging_update(self, remote_logging_level):
log.info('Remote logging has being deactivated.')
elif remote_logging_level is not None:
if self.remote_handler.current_log_level != remote_logging_level or not self.remote_handler.activated:
self.main_handler.setLevel(remote_logging_level)
self.remote_handler.activate(remote_logging_level)
log.info('Remote logging has being updated. Current logging level is: %s ',
remote_logging_level)
Expand Down Expand Up @@ -2033,11 +2035,24 @@ def get_status(self):
return {'connected': self.tb_client.is_connected()}

def update_loggers(self):
self.__update_base_loggers()

global log
log = logging.getLogger('service')
self._event_storage.update_logger()
self.tb_client.update_logger()

def __update_base_loggers(self):
if self.remote_handler.activated:
for logger_name in TBLoggerHandler.LOGGER_NAME_TO_ATTRIBUTE_NAME:
logger = logging.getLogger(logger_name)

if self.remote_handler not in logger.handlers:
if logger.name == 'tb_connection' and logger.level <= 10:
continue

logger.addHandler(self.remote_handler)

def is_latency_metrics_enabled(self):
return self.__latency_debug_mode

Expand Down
155 changes: 76 additions & 79 deletions thingsboard_gateway/tb_utility/tb_gateway_remote_configurator.py

Large diffs are not rendered by default.

20 changes: 6 additions & 14 deletions thingsboard_gateway/tb_utility/tb_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ def get_logger(self, name):

def add_logger(self, name):
log = logging.getLogger(name)
log.addHandler(self.__gateway.main_handler)
log.debug("Added remote handler to log %s", name)
if hasattr(self.__gateway, 'main_handler') and self.__gateway.main_handler not in log.handlers:
log.addHandler(self.__gateway.main_handler)
log.debug("Added main handler to log %s", name)
self.loggers[name] = log

def _send_logs(self):
Expand Down Expand Up @@ -103,19 +104,10 @@ def _send_logs(self):
sleep(1)

def activate(self, log_level=None):
try:
for logger in self.loggers:
if log_level is not None and logging.getLevelName(log_level) is not None:
if logger == 'tb_connection' and log_level == 'DEBUG':
log = init_logger(self.__gateway, logger, log_level)
self.loggers[logger] = log
else:
log = init_logger(self.__gateway, logger, log_level)
self.loggers[logger] = log
except Exception as e:
log = TbLogger('service')
log.exception("Failed to activate logs sending", exc_info=e)
self.setLevel(logging.getLevelName(log_level or 'INFO'))
self.current_log_level = log_level
self.activated = True

self._send_logs_thread = threading.Thread(target=self._send_logs, name='Logs Sending Thread', daemon=True)
self._send_logs_thread.start()

Expand Down
5 changes: 4 additions & 1 deletion thingsboard_gateway/tb_utility/tb_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@

from importlib.util import module_from_spec, spec_from_file_location
from inspect import getmembers, isclass
from logging import getLogger
from logging import getLogger, setLoggerClass
from os import listdir, path

from thingsboard_gateway.tb_utility.tb_logger import TbLogger

setLoggerClass(TbLogger)
log = getLogger("service")

EXTENSIONS_FOLDER = '/extensions'.replace('/', path.sep)
Expand Down
21 changes: 11 additions & 10 deletions thingsboard_gateway/tb_utility/tb_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,18 @@ def init_logger(gateway, name, level, enable_remote_logging=False, is_connector_
if connector_name:
log.connector_name = connector_name

if hasattr(gateway, 'main_handler') and gateway.main_handler not in log.handlers:
log.addHandler(gateway.main_handler)

if enable_remote_logging:
from thingsboard_gateway.tb_utility.tb_handler import TBLoggerHandler
remote_handler = TBLoggerHandler(gateway)
log.addHandler(remote_handler)
log.setLevel(gateway.main_handler.level)
remote_handler.add_logger(name)
remote_handler.activate()

if hasattr(gateway, 'main_handler'):
log.addHandler(gateway.main_handler)
log.setLevel(gateway.main_handler.level)
if not hasattr(gateway, 'remote_handler'):
gateway.remote_handler = TBLoggerHandler(gateway)
if gateway.remote_handler not in log.handlers:
log.addHandler(gateway.remote_handler)
gateway.remote_handler.add_logger(name)
if not gateway.remote_handler.activated:
gateway.remote_handler.activate()

log_level_conf = level
if log_level_conf:
Expand All @@ -55,7 +56,7 @@ def init_logger(gateway, name, level, enable_remote_logging=False, is_connector_
try:
log.setLevel(log_level)
except ValueError:
log.setLevel(logging.NOTSET)
log.setLevel(100)

return log

Expand Down
4 changes: 3 additions & 1 deletion thingsboard_gateway/tb_utility/tb_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from logging import getLogger
from logging import getLogger, setLoggerClass
from platform import platform, release, system
from time import time
from uuid import uuid1
Expand All @@ -23,8 +23,10 @@
from simplejson import loads

from thingsboard_gateway import version
from thingsboard_gateway.tb_utility.tb_logger import TbLogger
from thingsboard_gateway.tb_utility.tb_utility import TBUtility

setLoggerClass(TbLogger)
log = getLogger("service")

UPDATE_SERVICE_BASE_URL = "https://updates.thingsboard.io"
Expand Down
4 changes: 3 additions & 1 deletion thingsboard_gateway/tb_utility/tb_utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.
import datetime
from getpass import getuser
from logging import getLogger
from logging import getLogger, setLoggerClass
from os import environ
from platform import system as platform_system
from re import search, findall
Expand All @@ -31,7 +31,9 @@
from thingsboard_gateway.gateway.constants import SECURITY_VAR, REPORT_STRATEGY_PARAMETER
from thingsboard_gateway.gateway.entities.datapoint_key import DatapointKey
from thingsboard_gateway.gateway.entities.report_strategy_config import ReportStrategyConfig
from thingsboard_gateway.tb_utility.tb_logger import TbLogger

setLoggerClass(TbLogger)
log = getLogger("service")


Expand Down

0 comments on commit 86dd2ac

Please sign in to comment.