From 661441359d59cd255042a844cb4921dfc95a34d3 Mon Sep 17 00:00:00 2001 From: James Carey Date: Wed, 30 Jul 2014 20:05:50 +0000 Subject: [PATCH] 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 --- openstack/common/log.py | 9 ++++----- tests/unit/test_log.py | 11 +++++++++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/openstack/common/log.py b/openstack/common/log.py index b3c2936fc..8480b4850 100644 --- a/openstack/common/log.py +++ b/openstack/common/log.py @@ -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: diff --git a/tests/unit/test_log.py b/tests/unit/test_log.py index 08f226833..facd24331 100644 --- a/tests/unit/test_log.py +++ b/tests/unit/test_log.py @@ -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."""