Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite log.py #238

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/cnaas_nms/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, \
Expand All @@ -34,7 +34,7 @@
InvalidTokenError
from flask_jwt_extended.exceptions import InvalidHeaderError


init_logger()
logger = get_logger()


Expand Down
56 changes: 29 additions & 27 deletions src/cnaas_nms/tools/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)