summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHervé Beraud <hberaud@redhat.com>2022-05-11 10:52:26 +0200
committerHervé Beraud <hberaud@redhat.com>2022-05-11 11:19:32 +0200
commit66f9be975ca1266343f0adbfab86da4420aceab5 (patch)
tree4c6dc1d3b95f1f328a07b56c916dd9c037ce04fa
parentebdee7f39920ad5b4268ee296952432b0d41a375 (diff)
downloadoslo-log-66f9be975ca1266343f0adbfab86da4420aceab5.tar.gz
[Fix] init global_request_id if not in context
Initialize by default the `global_request_id` if not passed in the given context to avoid error with logging. Change-Id: I6bd63c1d6a1331a4e47b923ccf6df50c930c8162 Closes-Bug: #1972974
-rw-r--r--oslo_log/formatters.py2
-rw-r--r--oslo_log/tests/unit/test_log.py12
-rw-r--r--releasenotes/notes/init-global-request-id-eb2031bc221e5fb7.yaml5
3 files changed, 18 insertions, 1 deletions
diff --git a/oslo_log/formatters.py b/oslo_log/formatters.py
index 39f49e2..bf8455f 100644
--- a/oslo_log/formatters.py
+++ b/oslo_log/formatters.py
@@ -452,7 +452,7 @@ class ContextFormatter(logging.Formatter):
# to an empty string so we don't throw an exception if
# they get used
for key in ('instance', 'color', 'user_identity', 'resource',
- 'user_name', 'project_name'):
+ 'user_name', 'project_name', 'global_request_id'):
if key not in record.__dict__:
record.__dict__[key] = ''
diff --git a/oslo_log/tests/unit/test_log.py b/oslo_log/tests/unit/test_log.py
index 041d5cf..4c34d4c 100644
--- a/oslo_log/tests/unit/test_log.py
+++ b/oslo_log/tests/unit/test_log.py
@@ -964,6 +964,18 @@ class ContextFormatterTestCase(LogTestBase):
str(message)))
self.assertEqual(expected, self.stream.getvalue())
+ def test_global_request_id_logging(self):
+ fmt_str = "HAS CONTEXT [%(request_id)s %(global_request_id)s]: " \
+ "%(message)s"
+ self.config(logging_context_format_string=fmt_str)
+ ctxt = _fake_context()
+ ctxt.request_id = '99'
+ message = 'test'
+ self.log.info(message, context=ctxt)
+ expected = ("HAS CONTEXT [%s %s]: %s\n" %
+ (ctxt.request_id, ctxt.global_request_id, str(message)))
+ self.assertEqual(expected, self.stream.getvalue())
+
def test_user_identity_logging_set_format(self):
self.config(logging_context_format_string="HAS CONTEXT "
"[%(request_id)s "
diff --git a/releasenotes/notes/init-global-request-id-eb2031bc221e5fb7.yaml b/releasenotes/notes/init-global-request-id-eb2031bc221e5fb7.yaml
new file mode 100644
index 0000000..ff229ec
--- /dev/null
+++ b/releasenotes/notes/init-global-request-id-eb2031bc221e5fb7.yaml
@@ -0,0 +1,5 @@
+---
+fixes:
+ - |
+ Initialize the ``global_request_id`` context variable with a default
+ value if the key is not passed in the context.