Merge "The user_identity format flexibility"
This commit is contained in:
commit
ca3cbe8970
|
@ -153,6 +153,11 @@ log_opts = [
|
|||
default='[instance: %(uuid)s] ',
|
||||
help='The format for an instance UUID that is passed with the '
|
||||
'log message.'),
|
||||
cfg.StrOpt('logging_user_identity_format',
|
||||
default='%(user)s %(tenant)s '
|
||||
'%(domain)s %(user_domain)s %(project_domain)s',
|
||||
help='Format string for user_identity field of '
|
||||
'the logging_context_format_string'),
|
||||
]
|
||||
|
||||
|
||||
|
|
|
@ -62,6 +62,11 @@ def _update_record_with_context(record):
|
|||
return context
|
||||
|
||||
|
||||
class _ReplaceFalseValue(dict):
|
||||
def __getitem__(self, key):
|
||||
return dict.get(self, key, None) or '-'
|
||||
|
||||
|
||||
class JSONFormatter(logging.Formatter):
|
||||
def __init__(self, fmt=None, datefmt=None):
|
||||
# NOTE(jkoelker) we ignore the fmt argument, but its still there
|
||||
|
@ -222,6 +227,15 @@ class ContextFormatter(logging.Formatter):
|
|||
if key not in record.__dict__:
|
||||
record.__dict__[key] = ''
|
||||
|
||||
# Set the "user_identity" value of "logging_context_format_string"
|
||||
# by using "logging_user_identity_format" and
|
||||
# "to_dict()" of oslo.context.
|
||||
if context:
|
||||
record.user_identity = (
|
||||
self.conf.logging_user_identity_format %
|
||||
_ReplaceFalseValue(context.__dict__)
|
||||
)
|
||||
|
||||
if record.__dict__.get('request_id'):
|
||||
fmt = self.conf.logging_context_format_string
|
||||
else:
|
||||
|
|
|
@ -43,7 +43,14 @@ from oslo_log import log
|
|||
|
||||
|
||||
def _fake_context():
|
||||
return context.RequestContext(1, 1, overwrite=True)
|
||||
ctxt = context.RequestContext(1, 1, overwrite=True)
|
||||
ctxt.user = 'myuser'
|
||||
ctxt.tenant = 'mytenant'
|
||||
ctxt.domain = 'mydomain'
|
||||
ctxt.project_domain = 'myprojectdomain'
|
||||
ctxt.user_domain = 'myuserdomain'
|
||||
|
||||
return ctxt
|
||||
|
||||
|
||||
class CommonLoggerTestsMixIn(object):
|
||||
|
@ -425,6 +432,37 @@ class ContextFormatterTestCase(LogTestBase):
|
|||
message)
|
||||
self.assertEqual(expected, self.stream.getvalue())
|
||||
|
||||
def test_user_identity_logging(self):
|
||||
self.config(logging_context_format_string="HAS CONTEXT "
|
||||
"[%(request_id)s "
|
||||
"%(user_identity)s]: "
|
||||
"%(message)s")
|
||||
ctxt = _fake_context()
|
||||
ctxt.request_id = u'99'
|
||||
message = 'test'
|
||||
self.log.info(message, context=ctxt)
|
||||
expected = ("HAS CONTEXT [%s %s %s %s %s %s]: %s\n" %
|
||||
(ctxt.request_id, ctxt.user, ctxt.tenant, ctxt.domain,
|
||||
ctxt.user_domain, ctxt.project_domain,
|
||||
six.text_type(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 "
|
||||
"%(user_identity)s]: "
|
||||
"%(message)s",
|
||||
logging_user_identity_format="%(user)s "
|
||||
"%(tenant)s")
|
||||
ctxt = _fake_context()
|
||||
ctxt.request_id = u'99'
|
||||
message = 'test'
|
||||
self.log.info(message, context=ctxt)
|
||||
expected = ("HAS CONTEXT [%s %s %s]: %s\n" %
|
||||
(ctxt.request_id, ctxt.user, ctxt.tenant,
|
||||
six.text_type(message)))
|
||||
self.assertEqual(expected, self.stream.getvalue())
|
||||
|
||||
|
||||
class ExceptionLoggingTestCase(LogTestBase):
|
||||
"""Test that Exceptions are logged."""
|
||||
|
@ -595,9 +633,6 @@ class DomainTestCase(LogTestBase):
|
|||
|
||||
def test_domain_in_log_msg(self):
|
||||
ctxt = _fake_context()
|
||||
ctxt.domain = 'mydomain'
|
||||
ctxt.project_domain = 'myprojectdomain'
|
||||
ctxt.user_domain = 'myuserdomain'
|
||||
user_identity = ctxt.to_dict()['user_identity']
|
||||
self.assertTrue(ctxt.domain in user_identity)
|
||||
self.assertTrue(ctxt.project_domain in user_identity)
|
||||
|
|
Loading…
Reference in New Issue