Skip to content

Commit

Permalink
refactor: minor correction
Browse files Browse the repository at this point in the history
  • Loading branch information
Bob Bui committed Nov 18, 2021
1 parent 1866b12 commit efbe367
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 14 deletions.
8 changes: 4 additions & 4 deletions json_logging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=[],
Expand Down
3 changes: 1 addition & 2 deletions json_logging/dto.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""

Expand Down
9 changes: 5 additions & 4 deletions json_logging/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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))


Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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


Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions json_logging/framework/flask/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
8 changes: 4 additions & 4 deletions json_logging/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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


Expand Down

0 comments on commit efbe367

Please sign in to comment.