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:
parent
110676d92c
commit
9c8da6cbc9
@ -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:
|
||||
|
@ -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."""
|
||||
|
Loading…
x
Reference in New Issue
Block a user