[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
This commit is contained in:
@@ -452,7 +452,7 @@ class ContextFormatter(logging.Formatter):
|
|||||||
# to an empty string so we don't throw an exception if
|
# to an empty string so we don't throw an exception if
|
||||||
# they get used
|
# they get used
|
||||||
for key in ('instance', 'color', 'user_identity', 'resource',
|
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__:
|
if key not in record.__dict__:
|
||||||
record.__dict__[key] = ''
|
record.__dict__[key] = ''
|
||||||
|
|
||||||
|
|||||||
@@ -964,6 +964,18 @@ class ContextFormatterTestCase(LogTestBase):
|
|||||||
str(message)))
|
str(message)))
|
||||||
self.assertEqual(expected, self.stream.getvalue())
|
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):
|
def test_user_identity_logging_set_format(self):
|
||||||
self.config(logging_context_format_string="HAS CONTEXT "
|
self.config(logging_context_format_string="HAS CONTEXT "
|
||||||
"[%(request_id)s "
|
"[%(request_id)s "
|
||||||
|
|||||||
@@ -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.
|
||||||
Reference in New Issue
Block a user