diff options
author | Jenkins <jenkins@review.openstack.org> | 2016-02-18 11:35:41 +0000 |
---|---|---|
committer | Gerrit Code Review <review@openstack.org> | 2016-02-18 11:35:41 +0000 |
commit | 9e9a892e3acc0dd2abe97f5d15a2c37d9b5941b8 (patch) | |
tree | c1dc51b93fdad81695668a571da5383d867adec0 | |
parent | a04ab9b173b5d0e6ff2cabcd22e9c0ae9a958aae (diff) | |
parent | 01aaeae060ce5fbc58672ed36f3346128623fcf1 (diff) | |
download | oslo-context-9e9a892e3acc0dd2abe97f5d15a2c37d9b5941b8.tar.gz |
Merge "Add common oslo.log format parameters"
-rw-r--r-- | oslo_context/context.py | 10 | ||||
-rw-r--r-- | oslo_context/tests/test_context.py | 21 |
2 files changed, 30 insertions, 1 deletions
diff --git a/oslo_context/context.py b/oslo_context/context.py index 19bc1a3..0458174 100644 --- a/oslo_context/context.py +++ b/oslo_context/context.py @@ -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 diff --git a/oslo_context/tests/test_context.py b/oslo_context/tests/test_context.py index 1a91023..406e480 100644 --- a/oslo_context/tests/test_context.py +++ b/oslo_context/tests/test_context.py @@ -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() |