Add common oslo.log format parameters
Add to get_logging_values() attributes that used in format string parameters and that can cause an exception when the are not defined. This is being refactored from the oslo.log formatter functionality. Change-Id: I7108a51bea01db6ccd9b6b24afc7b8d92b8eeda7 Implements: blueprint app-agnostic-logging-parameters
This commit is contained in:
parent
22ad2c2c59
commit
01aaeae060
|
@ -102,7 +102,15 @@ class RequestContext(object):
|
|||
|
||||
def get_logging_values(self):
|
||||
"""Return a dictionary of logging specific context attributes."""
|
||||
values = self.to_dict()
|
||||
# Define these attributes so that oslo.log does not throw an exception
|
||||
# if used in any formatting strings
|
||||
values = {'instance': '',
|
||||
'resource': '',
|
||||
'user_name': '',
|
||||
'project_name': '',
|
||||
'color': ''}
|
||||
values.update(self.to_dict())
|
||||
|
||||
return values
|
||||
|
||||
@classmethod
|
||||
|
|
|
@ -261,6 +261,11 @@ class ContextTest(test_base.BaseTestCase):
|
|||
self.assertIn('request_id', d)
|
||||
self.assertIn('resource_uuid', d)
|
||||
self.assertIn('user_identity', d)
|
||||
self.assertIn('instance', d)
|
||||
self.assertIn('resource', d)
|
||||
self.assertIn('user_name', d)
|
||||
self.assertIn('project_name', d)
|
||||
self.assertIn('color', d)
|
||||
|
||||
self.assertEqual(auth_token, d['auth_token'])
|
||||
self.assertEqual(tenant, d['tenant'])
|
||||
|
@ -275,6 +280,22 @@ class ContextTest(test_base.BaseTestCase):
|
|||
user_identity = "%s %s %s %s %s" % (user, tenant, domain,
|
||||
user_domain, project_domain)
|
||||
self.assertEqual(user_identity, d['user_identity'])
|
||||
self.assertEqual("", d['instance'])
|
||||
self.assertEqual("", d['resource'])
|
||||
self.assertEqual("", d['user_name'])
|
||||
self.assertEqual("", d['project_name'])
|
||||
self.assertEqual("", d['color'])
|
||||
|
||||
def test_get_logging_values_extra_attributes(self):
|
||||
ctx = context.RequestContext()
|
||||
d = ctx.get_logging_values()
|
||||
self.assertEqual("", d['color'])
|
||||
color = "red"
|
||||
setattr(ctx, "color", color)
|
||||
d = ctx.get_logging_values()
|
||||
# Regardless of setting attribute, this is not affected as this
|
||||
# is not included in to_dict().
|
||||
self.assertEqual("", d['color'])
|
||||
|
||||
def test_dict_empty_user_identity(self):
|
||||
ctx = context.RequestContext()
|
||||
|
|
Loading…
Reference in New Issue