Correct coercion of logged message to unicode

Changed to check if logged message is an instance of six.text_type
and coerce it to unicode if it isn't.

Change-Id: I26b0d3490f731f6fdb9a429aa9ef59360ddc88ba
Closes-bug: #1348244
This commit is contained in:
James Carey 2014-07-30 20:05:50 +00:00
parent e7d26a6f51
commit 661441359d
2 changed files with 15 additions and 5 deletions

View File

@ -300,11 +300,10 @@ class ContextAdapter(BaseLoggerAdapter):
self.warn(stdmsg, *args, **kwargs)
def process(self, msg, kwargs):
# NOTE(mrodden): catch any Message/other object and
# coerce to unicode before they can get
# to the python logging and possibly
# cause string encoding trouble
if not isinstance(msg, six.string_types):
# NOTE(jecarey): If msg is not unicode, coerce it into unicode
# before it can get to the python logging and
# possibly cause string encoding trouble
if not isinstance(msg, six.text_type):
msg = six.text_type(msg)
if 'extra' not in kwargs:

View File

@ -408,6 +408,17 @@ class ContextFormatterTestCase(LogTestBase):
six.text_type(message))
self.assertEqual(expected, self.stream.getvalue())
def test_unicode_conversion(self):
ctxt = _fake_context()
ctxt.request_id = six.text_type('99')
message = "Exception is (%s)"
ex = Exception(gettextutils.Message('test' + six.unichr(128)))
self.log.debug(message, ex, context=ctxt)
message = six.text_type(message) % ex
expected = "HAS CONTEXT [%s]: %s --DBG\n" % (ctxt.request_id,
message)
self.assertEqual(expected, self.stream.getvalue())
class ExceptionLoggingTestCase(LogTestBase):
"""Test that Exceptions are logged."""