Use project when logging the user identity

The logging_user_identity_format option's default value is changed
to reference the project instead of the tenant. This is necessary
because I2fded6f3476df1fb8c4e042ef28ed8ccb7ab0737 removed the tenant
argument from request contexts.

Related-Bug: #1505827
Change-Id: Id6d4b6eaa2b96ba5bd53e8b2dae5d9eea13b25d9
This commit is contained in:
Alan Bishop 2022-02-05 12:47:09 -08:00
parent 27d159b4d6
commit 650ce7d313
3 changed files with 22 additions and 27 deletions

View File

@ -167,7 +167,7 @@ log_opts = [
help='Prefix each line of exception output with this format. ' help='Prefix each line of exception output with this format. '
'Used by oslo_log.formatters.ContextFormatter'), 'Used by oslo_log.formatters.ContextFormatter'),
cfg.StrOpt('logging_user_identity_format', cfg.StrOpt('logging_user_identity_format',
default='%(user)s %(tenant)s ' default='%(user)s %(project)s '
'%(domain)s %(user_domain)s %(project_domain)s', '%(domain)s %(user_domain)s %(project_domain)s',
help='Defines the format string for %(user_identity)s that ' help='Defines the format string for %(user_identity)s that '
'is used in logging_context_format_string. ' 'is used in logging_context_format_string. '

View File

@ -69,26 +69,14 @@ handlers=
def _fake_context(): def _fake_context():
ctxt = context.RequestContext(1, 1, overwrite=True) ctxt = context.RequestContext(user_id="myuser",
ctxt.user = 'myuser' user_name="myuser",
ctxt.tenant = 'mytenant' domain="mydomain",
ctxt.domain = 'mydomain' project_id="mytenant",
ctxt.project_domain = 'myprojectdomain' project_name="mytenant",
ctxt.user_domain = 'myuserdomain' project_domain_id="mydomain",
user_domain_id="myuserdomain",
return ctxt overwrite=True)
def _fake_new_context():
# New style contexts have a user_name / project_name, this is done
# distinctly from the above context to not have to rewrite all the
# other tests.
ctxt = context.RequestContext(1, 1, overwrite=True)
ctxt.user_name = 'myuser'
ctxt.project_name = 'mytenant'
ctxt.domain = 'mydomain'
ctxt.project_domain = 'myprojectdomain'
ctxt.user_domain = 'myuserdomain'
return ctxt return ctxt
@ -108,7 +96,7 @@ class CommonLoggerTestsMixIn(object):
log.register_options(self.config_fixture.conf) log.register_options(self.config_fixture.conf)
self.config(logging_context_format_string='%(asctime)s %(levelname)s ' self.config(logging_context_format_string='%(asctime)s %(levelname)s '
'%(name)s [%(request_id)s ' '%(name)s [%(request_id)s '
'%(user)s %(tenant)s] ' '%(user)s %(project)s] '
'%(message)s') '%(message)s')
self.log = None self.log = None
log._setup_logging_from_conf(self.config_fixture.conf, 'test', 'test') log._setup_logging_from_conf(self.config_fixture.conf, 'test', 'test')
@ -405,7 +393,7 @@ class OSJournalHandlerTestCase(BaseTestCase):
def test_emit(self): def test_emit(self):
logger = log.getLogger('nova-test.foo') logger = log.getLogger('nova-test.foo')
local_context = _fake_new_context() local_context = _fake_context()
logger.info("Foo", context=local_context) logger.info("Foo", context=local_context)
self.assertEqual( self.assertEqual(
mock.call(mock.ANY, CODE_FILE=mock.ANY, CODE_FUNC='test_emit', mock.call(mock.ANY, CODE_FILE=mock.ANY, CODE_FUNC='test_emit',
@ -414,6 +402,7 @@ class OSJournalHandlerTestCase(BaseTestCase):
SYSLOG_FACILITY=syslog.LOG_USER, SYSLOG_FACILITY=syslog.LOG_USER,
SYSLOG_IDENTIFIER=mock.ANY, SYSLOG_IDENTIFIER=mock.ANY,
REQUEST_ID=mock.ANY, REQUEST_ID=mock.ANY,
PROJECT_ID='mytenant',
PROJECT_NAME='mytenant', PROJECT_NAME='mytenant',
PROCESS_NAME='MainProcess', PROCESS_NAME='MainProcess',
THREAD_NAME='MainThread', THREAD_NAME='MainThread',
@ -432,7 +421,7 @@ class OSJournalHandlerTestCase(BaseTestCase):
def test_emit_exception(self): def test_emit_exception(self):
logger = log.getLogger('nova-exception.foo') logger = log.getLogger('nova-exception.foo')
local_context = _fake_new_context() local_context = _fake_context()
try: try:
raise Exception("Some exception") raise Exception("Some exception")
except Exception: except Exception:
@ -447,6 +436,7 @@ class OSJournalHandlerTestCase(BaseTestCase):
REQUEST_ID=mock.ANY, REQUEST_ID=mock.ANY,
EXCEPTION_INFO=mock.ANY, EXCEPTION_INFO=mock.ANY,
EXCEPTION_TEXT=mock.ANY, EXCEPTION_TEXT=mock.ANY,
PROJECT_ID='mytenant',
PROJECT_NAME='mytenant', PROJECT_NAME='mytenant',
PROCESS_NAME='MainProcess', PROCESS_NAME='MainProcess',
THREAD_NAME='MainThread', THREAD_NAME='MainThread',
@ -968,7 +958,7 @@ class ContextFormatterTestCase(LogTestBase):
message = 'test' message = 'test'
self.log.info(message, context=ctxt) self.log.info(message, context=ctxt)
expected = ("HAS CONTEXT [%s %s %s %s %s %s]: %s\n" % expected = ("HAS CONTEXT [%s %s %s %s %s %s]: %s\n" %
(ctxt.request_id, ctxt.user, ctxt.tenant, ctxt.domain, (ctxt.request_id, ctxt.user, ctxt.project_id, ctxt.domain,
ctxt.user_domain, ctxt.project_domain, ctxt.user_domain, ctxt.project_domain,
str(message))) str(message)))
self.assertEqual(expected, self.stream.getvalue()) self.assertEqual(expected, self.stream.getvalue())
@ -979,13 +969,13 @@ class ContextFormatterTestCase(LogTestBase):
"%(user_identity)s]: " "%(user_identity)s]: "
"%(message)s", "%(message)s",
logging_user_identity_format="%(user)s " logging_user_identity_format="%(user)s "
"%(tenant)s") "%(project)s")
ctxt = _fake_context() ctxt = _fake_context()
ctxt.request_id = '99' ctxt.request_id = '99'
message = 'test' message = 'test'
self.log.info(message, context=ctxt) self.log.info(message, context=ctxt)
expected = ("HAS CONTEXT [%s %s %s]: %s\n" % expected = ("HAS CONTEXT [%s %s %s]: %s\n" %
(ctxt.request_id, ctxt.user, ctxt.tenant, (ctxt.request_id, ctxt.user, ctxt.project_id,
str(message))) str(message)))
self.assertEqual(expected, self.stream.getvalue()) self.assertEqual(expected, self.stream.getvalue())

View File

@ -0,0 +1,5 @@
---
upgrade:
- |
The ``logging_user_identity_format`` option's default value is updated to
replace the obsolete ``tenant`` argument with ``project``.