summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZuul <zuul@review.opendev.org>2022-05-13 11:03:59 +0000
committerGerrit Code Review <review@openstack.org>2022-05-13 11:03:59 +0000
commit6401da71b3b1b8d0fc213f0a58dbf69031acba87 (patch)
tree2e80052b8ea4fb19b0aa869f3da793fc66c58a5c
parentcdbf6e7a915059df7820b41a989adce09969834c (diff)
parent66f9be975ca1266343f0adbfab86da4420aceab5 (diff)
downloadoslo-log-6401da71b3b1b8d0fc213f0a58dbf69031acba87.tar.gz
Merge "[Fix] init global_request_id if not in context"5.0.0
-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.