Skip to content

Commit 6297219

Browse files
authored
Merge pull request bobbui#100 from charles-dyfis-net/custom_correlation_id_fieldname
Allow output field used for correlation_id to be customized
2 parents 403d822 + a5b3b60 commit 6297219

File tree

3 files changed

+5
-3
lines changed

3 files changed

+5
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ Name | Description | Default value
280280
--- | --- | ---
281281
ENABLE_JSON_LOGGING | **
282282
DEPRECATED** Whether to enable JSON logging mode.Can be set as an environment variable, enable when set to to either one in following list (case-insensitive) **['true', '1', 'y', 'yes']** , this have no effect on request logger | false
283+
CORRELATION_ID_FIELD | Name of field in generated log messages containing the correlation-id value | 'correlation_id'
283284
CORRELATION_ID_HEADERS | List of HTTP headers that will be used to look for correlation-id value. HTTP headers will be searched one by one according to list order| ['X-Correlation-ID','X-Request-ID']
284285
EMPTY_VALUE | Default value when a logging record property is None | '-'
285286
CORRELATION_ID_GENERATOR | function to generate unique correlation-id | uuid.uuid1

json_logging/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
BaseFrameworkConfigurator
1313
from json_logging.util import get_library_logger, is_env_var_toggle
1414

15+
CORRELATION_ID_FIELD = 'correlation_id'
1516
CORRELATION_ID_GENERATOR = uuid.uuid1
1617
ENABLE_JSON_LOGGING = False
1718
if is_env_var_toggle("ENABLE_JSON_LOGGING"):

json_logging/formatters.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ class JSONLogWebFormatter(JSONLogFormatter):
146146
def _format_log_object(self, record, request_util):
147147
json_log_object = super(JSONLogWebFormatter, self)._format_log_object(record, request_util)
148148

149-
if "correlation_id" not in json_log_object:
149+
if json_logging.CORRELATION_ID_FIELD not in json_log_object:
150150
json_log_object.update({
151-
"correlation_id": request_util.get_correlation_id(within_formatter=True),
151+
json_logging.CORRELATION_ID_FIELD: request_util.get_correlation_id(within_formatter=True),
152152
})
153153

154154
return json_log_object
@@ -172,7 +172,7 @@ def _format_log_object(self, record, request_util):
172172

173173
json_log_object.update({
174174
"type": "request",
175-
"correlation_id": request_util.get_correlation_id(request),
175+
json_logging.CORRELATION_ID_FIELD: request_util.get_correlation_id(request),
176176
"remote_user": request_adapter.get_remote_user(request),
177177
"request": request_adapter.get_path(request),
178178
"referer": request_adapter.get_http_header(request, 'referer', json_logging.EMPTY_VALUE),

0 commit comments

Comments
 (0)