From efbe367527203a88e0fec9f81ccb2f1e9252b1bf Mon Sep 17 00:00:00 2001 From: Bob Bui Date: Thu, 18 Nov 2021 15:36:53 -0800 Subject: [PATCH] refactor: minor correction --- json_logging/__init__.py | 8 ++++---- json_logging/dto.py | 3 +-- json_logging/formatters.py | 9 +++++---- json_logging/framework/flask/__init__.py | 1 + json_logging/util.py | 8 ++++---- 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/json_logging/__init__.py b/json_logging/__init__.py index ff4b5ba..ce4571d 100644 --- a/json_logging/__init__.py +++ b/json_logging/__init__.py @@ -126,10 +126,10 @@ def __init(framework_name=None, custom_formatter=None, enable_json=False): if ENABLE_JSON_LOGGING: logging._defaultFormatter = _default_formatter() - # go to all the initialized logger and update it to use JSON formatter - ENABLE_JSON_LOGGING_DEBUG and _logger.debug("Update all existing logger to using JSONLogFormatter") - existing_loggers = list(map(logging.getLogger, logging.Logger.manager.loggerDict)) - util.update_formatter_for_loggers(existing_loggers, _default_formatter) + _logger.debug("Update all existing logger to using JSONLogFormatter") + + existing_loggers = list(map(logging.getLogger, logging.Logger.manager.loggerDict)) + util.update_formatter_for_loggers(existing_loggers, _default_formatter) def init_request_instrument(app=None, custom_formatter=None, exclude_url_patterns=[], diff --git a/json_logging/dto.py b/json_logging/dto.py index 9877f25..4cf39a8 100644 --- a/json_logging/dto.py +++ b/json_logging/dto.py @@ -5,8 +5,7 @@ class RequestResponseDTOBase(dict): """ - Data transfer object (DTO) for HTTP request & response information for each request instrumentation logging - Any key that is stored in this dict will be appended to final JSON log object + Data transfer object (DTO) for request instrumentation logging Served as base class for any actual RequestResponseDTO implementation """ diff --git a/json_logging/formatters.py b/json_logging/formatters.py index a5358d5..2dad38e 100644 --- a/json_logging/formatters.py +++ b/json_logging/formatters.py @@ -7,7 +7,7 @@ # The list contains all the attributes listed in that will not be overwritten by custom extra props # http://docs.python.org/library/logging.html#logrecord-attributes -RECORD_ATTR_SKIP_LIST = [ +LOG_RECORD_BUILT_IN_ATTRS = [ 'asctime', 'created', 'exc_info', 'exc_text', 'filename', 'args', 'funcName', 'id', 'levelname', 'levelno', 'lineno', 'module', 'msg', 'msecs', 'msecs', 'message', 'name', 'pathname', 'process', @@ -25,7 +25,7 @@ if sys.version_info < (3, 0): EASY_SERIALIZABLE_TYPES = (basestring, bool, dict, float, int, list, type(None)) else: - RECORD_ATTR_SKIP_LIST.append('stack_info') + LOG_RECORD_BUILT_IN_ATTRS.append('stack_info') EASY_SERIALIZABLE_TYPES = (str, bool, dict, float, int, list, type(None)) @@ -86,7 +86,7 @@ def _get_extra_fields(self, record): fields['msg'] = record.msg for key, value in record.__dict__.items(): - if key not in RECORD_ATTR_SKIP_LIST: + if key not in LOG_RECORD_BUILT_IN_ATTRS: if isinstance(value, EASY_SERIALIZABLE_TYPES): fields[key] = value else: @@ -150,6 +150,7 @@ def _format_log_object(self, record, request_util): json_log_object.update({ "correlation_id": request_util.get_correlation_id(within_formatter=True), }) + return json_log_object @@ -162,7 +163,7 @@ def _format_log_object(self, record, request_util): json_log_object = super(JSONRequestLogFormatter, self)._format_log_object(record, request_util) request_adapter = request_util.request_adapter - response_adapter = json_logging._request_util.response_adapter + response_adapter = request_util.response_adapter request = record.request_response_data._request response = record.request_response_data._response diff --git a/json_logging/framework/flask/__init__.py b/json_logging/framework/flask/__init__.py index 5c3d61e..03857ea 100644 --- a/json_logging/framework/flask/__init__.py +++ b/json_logging/framework/flask/__init__.py @@ -58,6 +58,7 @@ def after_request(response): request_response_data = g.request_response_data request_response_data.on_request_complete(response) self.request_logger.info("", extra={'request_response_data': request_response_data}) + return response diff --git a/json_logging/util.py b/json_logging/util.py index 595461d..c69d98a 100644 --- a/json_logging/util.py +++ b/json_logging/util.py @@ -102,6 +102,7 @@ class RequestUtil(object): """ def __new__(cls, *args, **kw): + # make this request util a singleton object if not hasattr(cls, '_instance'): request_info_extractor_class = kw['request_info_extractor_class'] response_info_extractor_class = kw['response_info_extractor_class'] @@ -128,8 +129,7 @@ def get_correlation_id(self, request=None, within_formatter=False): :param request: request object :return: correlation id string """ - # _logger.debug("Getting correlation", extra={'correlation_id': '-'}) - # + if request is None: if self.is_support_global_request_object: request = self.request_info_extractor_class.get_current_request() @@ -139,13 +139,12 @@ def get_correlation_id(self, request=None, within_formatter=False): if request is None: return json_logging.EMPTY_VALUE - # _logger.debug("Attempt to get correlation from request context", extra={'correlation_id': '-'}) correlation_id = self.request_adapter.get_correlation_id_in_request_context(request) if correlation_id is not None: return correlation_id correlation_id = self._get_correlation_id_in_request_header(self.request_adapter, request) - # exists = json_logging.CREATE_CORRELATION_ID_IF_NOT_EXISTS + if correlation_id is None and self.create_correlation_id_if_not_exists: correlation_id = str(json_logging.CORRELATION_ID_GENERATOR()) self.request_adapter.set_correlation_id(request, correlation_id) @@ -203,6 +202,7 @@ def _get_correlation_id_in_request_header(request_adapter, request): value = request_adapter.get_http_header(request, header) if value is not None: return value + return None