From 0079e887c00259de4a6c7919b1670d8f0b26fc3a Mon Sep 17 00:00:00 2001 From: Simon Oliver Tveit Date: Mon, 29 Nov 2021 16:52:12 +0100 Subject: [PATCH 1/2] Rewrite log.py move logging init into its own function. Change how get_logger works. Logging now uses one logger (in additon to flask logger) --- src/cnaas_nms/tools/log.py | 56 ++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/src/cnaas_nms/tools/log.py b/src/cnaas_nms/tools/log.py index 1dca5372..4c83bcf5 100644 --- a/src/cnaas_nms/tools/log.py +++ b/src/cnaas_nms/tools/log.py @@ -14,34 +14,36 @@ def emit(self, record): msg = self.format(record) add_event(msg, level=record.levelname) +class CustomFormatter(logging.Formatter): + + main_format = '[%(asctime)s] %(levelname)s in %(module)s: %(message)s' + job_format = '[%(asctime)s] %(levelname)s in %(module)s job #%(jobid)s: %(message)s' + + def __init__(self): + super().__init__() + + def format(self, record): + # include jobid in log line if it it exists + if hasattr(record, 'jobid'): + self._style._fmt = CustomFormatter.job_format + else: + self._style._fmt = CustomFormatter.main_format + result = logging.Formatter.format(self, record) + return result + +def init_logger(): + logger = logging.getLogger('cnaas-nms') + logger.setLevel(logging.DEBUG) + formatter = CustomFormatter() + for handler in [logging.StreamHandler(), WebsocketHandler()]: + handler.setFormatter(formatter) + logger.addHandler(handler) def get_logger(): + extra = {} if hasattr(thread_data, 'job_id') and type(thread_data.job_id) == int: - logger = logging.getLogger('cnaas-nms-{}'.format(thread_data.job_id)) - if not logger.handlers: - formatter = logging.Formatter('[%(asctime)s] %(levelname)s in %(module)s job #{}: %(message)s'. - format(thread_data.job_id)) - # stdout logging - handler = logging.StreamHandler() - handler.setFormatter(formatter) - logger.addHandler(handler) - # websocket logging - handler = WebsocketHandler() - handler.setFormatter(formatter) - logger.addHandler(handler) + extra['jobid'] = thread_data.job_id elif current_app: - logger = current_app.logger - else: - logger = logging.getLogger('cnaas-nms') - if not logger.handlers: - formatter = logging.Formatter('[%(asctime)s] %(levelname)s in %(module)s: %(message)s') - # stdout logging - handler = logging.StreamHandler() - handler.setFormatter(formatter) - logger.addHandler(handler) - # websocket logging - handler = WebsocketHandler() - handler.setFormatter(formatter) - logger.addHandler(handler) - logger.setLevel(logging.DEBUG) #TODO: get from /etc config ? - return logger + return current_app.logger + logger = logging.getLogger('cnaas-nms') + return logging.LoggerAdapter(logger, extra) From bfef395de059756addf69da0a867e21b2c9d8375 Mon Sep 17 00:00:00 2001 From: Simon Oliver Tveit Date: Wed, 21 Sep 2022 19:36:55 +0200 Subject: [PATCH 2/2] Init logging --- src/cnaas_nms/api/app.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cnaas_nms/api/app.py b/src/cnaas_nms/api/app.py index 2c4163c1..b5c14ca8 100644 --- a/src/cnaas_nms/api/app.py +++ b/src/cnaas_nms/api/app.py @@ -13,7 +13,7 @@ from flask_cors import CORS from cnaas_nms.version import __api_version__ -from cnaas_nms.tools.log import get_logger +from cnaas_nms.tools.log import init_logger, get_logger from cnaas_nms.api.device import device_api, devices_api, device_init_api, \ device_initcheck_api, device_syncto_api, device_discover_api, \ @@ -34,7 +34,7 @@ InvalidTokenError from flask_jwt_extended.exceptions import InvalidHeaderError - +init_logger() logger = get_logger()