Skip to content

Commit

Permalink
Merge pull request #100 from charles-dyfis-net/custom_correlation_id_…
Browse files Browse the repository at this point in the history
…fieldname

Allow output field used for correlation_id to be customized
  • Loading branch information
bobbui authored Feb 15, 2024
2 parents 403d822 + a5b3b60 commit 6297219
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ Name | Description | Default value
--- | --- | ---
ENABLE_JSON_LOGGING | **
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
CORRELATION_ID_FIELD | Name of field in generated log messages containing the correlation-id value | 'correlation_id'
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']
EMPTY_VALUE | Default value when a logging record property is None | '-'
CORRELATION_ID_GENERATOR | function to generate unique correlation-id | uuid.uuid1
Expand Down
1 change: 1 addition & 0 deletions json_logging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
BaseFrameworkConfigurator
from json_logging.util import get_library_logger, is_env_var_toggle

CORRELATION_ID_FIELD = 'correlation_id'
CORRELATION_ID_GENERATOR = uuid.uuid1
ENABLE_JSON_LOGGING = False
if is_env_var_toggle("ENABLE_JSON_LOGGING"):
Expand Down
6 changes: 3 additions & 3 deletions json_logging/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ class JSONLogWebFormatter(JSONLogFormatter):
def _format_log_object(self, record, request_util):
json_log_object = super(JSONLogWebFormatter, self)._format_log_object(record, request_util)

if "correlation_id" not in json_log_object:
if json_logging.CORRELATION_ID_FIELD not in json_log_object:
json_log_object.update({
"correlation_id": request_util.get_correlation_id(within_formatter=True),
json_logging.CORRELATION_ID_FIELD: request_util.get_correlation_id(within_formatter=True),
})

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

json_log_object.update({
"type": "request",
"correlation_id": request_util.get_correlation_id(request),
json_logging.CORRELATION_ID_FIELD: request_util.get_correlation_id(request),
"remote_user": request_adapter.get_remote_user(request),
"request": request_adapter.get_path(request),
"referer": request_adapter.get_http_header(request, 'referer', json_logging.EMPTY_VALUE),
Expand Down

0 comments on commit 6297219

Please sign in to comment.