Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Make Messages unicode before hitting logging
Browse files Browse the repository at this point in the history
Some of the log handlers in the python standard library
are hitting issues with Messages since they check for
basestring types only, and attempt to coerce anything else
to string. This leads to Messages being coerced to bad encodings,
resulting in unhandled exceptions.

This change adds a check that looks for non-basestring
objects and coerces them to unicode before they hit the problematic
logging areas.

bug 1225099

Change-Id: I0bff6b52205e3c88db38876b8c6de1bd820f460a
Co-authored-by: Luis A. Garcia <luis@linux.vnet.ibm.com>
  • Loading branch information
mrodden and Luis A. Garcia committed Oct 1, 2013
1 parent eaf670b commit 572cfec
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions openstack/common/log.py
Expand Up @@ -249,6 +249,13 @@ def deprecated(self, msg, *args, **kwargs):
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, basestring):
msg = unicode(msg)

if 'extra' not in kwargs:
kwargs['extra'] = {}
extra = kwargs['extra']
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/test_log.py
Expand Up @@ -25,6 +25,7 @@
from openstack.common import context
from openstack.common.fixture import config
from openstack.common.fixture import moxstubout
from openstack.common import gettextutils
from openstack.common import jsonutils
from openstack.common import log
from openstack.common import log_handler
Expand Down Expand Up @@ -263,6 +264,19 @@ def test_debugging_log(self):
self.log.debug("baz")
self.assertEqual("NOCTXT: baz --DBG\n", self.stream.getvalue())

def test_message_logging(self):
# NOTE(luisg): Logging message objects with unicode objects
# may cause trouble by the logging mechanism trying to coerce
# the Message object, with a wrong encoding. This test case
# tests that problem does not occur.
ctxt = _fake_context()
ctxt.request_id = unicode('99')
message = gettextutils.Message('test ' + unichr(128), 'test')
self.log.info(message, context=ctxt)
expected = "HAS CONTEXT [%s]: %s\n" % (ctxt.request_id,
unicode(message))
self.assertEqual(expected, self.stream.getvalue())


class ExceptionLoggingTestCase(test.BaseTestCase):
"""Test that Exceptions are logged."""
Expand Down

0 comments on commit 572cfec

Please sign in to comment.