Skip to content

Commit efbe367

Browse files
author
Bob Bui
committed
refactor: minor correction
1 parent 1866b12 commit efbe367

File tree

5 files changed

+15
-14
lines changed

5 files changed

+15
-14
lines changed

json_logging/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,10 @@ def __init(framework_name=None, custom_formatter=None, enable_json=False):
126126
if ENABLE_JSON_LOGGING:
127127
logging._defaultFormatter = _default_formatter()
128128

129-
# go to all the initialized logger and update it to use JSON formatter
130-
ENABLE_JSON_LOGGING_DEBUG and _logger.debug("Update all existing logger to using JSONLogFormatter")
131-
existing_loggers = list(map(logging.getLogger, logging.Logger.manager.loggerDict))
132-
util.update_formatter_for_loggers(existing_loggers, _default_formatter)
129+
_logger.debug("Update all existing logger to using JSONLogFormatter")
130+
131+
existing_loggers = list(map(logging.getLogger, logging.Logger.manager.loggerDict))
132+
util.update_formatter_for_loggers(existing_loggers, _default_formatter)
133133

134134

135135
def init_request_instrument(app=None, custom_formatter=None, exclude_url_patterns=[],

json_logging/dto.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55

66
class RequestResponseDTOBase(dict):
77
"""
8-
Data transfer object (DTO) for HTTP request & response information for each request instrumentation logging
9-
Any key that is stored in this dict will be appended to final JSON log object
8+
Data transfer object (DTO) for request instrumentation logging
109
Served as base class for any actual RequestResponseDTO implementation
1110
"""
1211

json_logging/formatters.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
# The list contains all the attributes listed in that will not be overwritten by custom extra props
99
# http://docs.python.org/library/logging.html#logrecord-attributes
10-
RECORD_ATTR_SKIP_LIST = [
10+
LOG_RECORD_BUILT_IN_ATTRS = [
1111
'asctime', 'created', 'exc_info', 'exc_text', 'filename', 'args',
1212
'funcName', 'id', 'levelname', 'levelno', 'lineno', 'module', 'msg',
1313
'msecs', 'msecs', 'message', 'name', 'pathname', 'process',
@@ -25,7 +25,7 @@
2525
if sys.version_info < (3, 0):
2626
EASY_SERIALIZABLE_TYPES = (basestring, bool, dict, float, int, list, type(None))
2727
else:
28-
RECORD_ATTR_SKIP_LIST.append('stack_info')
28+
LOG_RECORD_BUILT_IN_ATTRS.append('stack_info')
2929
EASY_SERIALIZABLE_TYPES = (str, bool, dict, float, int, list, type(None))
3030

3131

@@ -86,7 +86,7 @@ def _get_extra_fields(self, record):
8686
fields['msg'] = record.msg
8787

8888
for key, value in record.__dict__.items():
89-
if key not in RECORD_ATTR_SKIP_LIST:
89+
if key not in LOG_RECORD_BUILT_IN_ATTRS:
9090
if isinstance(value, EASY_SERIALIZABLE_TYPES):
9191
fields[key] = value
9292
else:
@@ -150,6 +150,7 @@ def _format_log_object(self, record, request_util):
150150
json_log_object.update({
151151
"correlation_id": request_util.get_correlation_id(within_formatter=True),
152152
})
153+
153154
return json_log_object
154155

155156

@@ -162,7 +163,7 @@ def _format_log_object(self, record, request_util):
162163
json_log_object = super(JSONRequestLogFormatter, self)._format_log_object(record, request_util)
163164

164165
request_adapter = request_util.request_adapter
165-
response_adapter = json_logging._request_util.response_adapter
166+
response_adapter = request_util.response_adapter
166167

167168
request = record.request_response_data._request
168169
response = record.request_response_data._response

json_logging/framework/flask/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def after_request(response):
5858
request_response_data = g.request_response_data
5959
request_response_data.on_request_complete(response)
6060
self.request_logger.info("", extra={'request_response_data': request_response_data})
61+
6162
return response
6263

6364

json_logging/util.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ class RequestUtil(object):
102102
"""
103103

104104
def __new__(cls, *args, **kw):
105+
# make this request util a singleton object
105106
if not hasattr(cls, '_instance'):
106107
request_info_extractor_class = kw['request_info_extractor_class']
107108
response_info_extractor_class = kw['response_info_extractor_class']
@@ -128,8 +129,7 @@ def get_correlation_id(self, request=None, within_formatter=False):
128129
:param request: request object
129130
:return: correlation id string
130131
"""
131-
# _logger.debug("Getting correlation", extra={'correlation_id': '-'})
132-
#
132+
133133
if request is None:
134134
if self.is_support_global_request_object:
135135
request = self.request_info_extractor_class.get_current_request()
@@ -139,13 +139,12 @@ def get_correlation_id(self, request=None, within_formatter=False):
139139
if request is None:
140140
return json_logging.EMPTY_VALUE
141141

142-
# _logger.debug("Attempt to get correlation from request context", extra={'correlation_id': '-'})
143142
correlation_id = self.request_adapter.get_correlation_id_in_request_context(request)
144143
if correlation_id is not None:
145144
return correlation_id
146145

147146
correlation_id = self._get_correlation_id_in_request_header(self.request_adapter, request)
148-
# exists = json_logging.CREATE_CORRELATION_ID_IF_NOT_EXISTS
147+
149148
if correlation_id is None and self.create_correlation_id_if_not_exists:
150149
correlation_id = str(json_logging.CORRELATION_ID_GENERATOR())
151150
self.request_adapter.set_correlation_id(request, correlation_id)
@@ -203,6 +202,7 @@ def _get_correlation_id_in_request_header(request_adapter, request):
203202
value = request_adapter.get_http_header(request, header)
204203
if value is not None:
205204
return value
205+
206206
return None
207207

208208

0 commit comments

Comments
 (0)