diff options
| -rw-r--r-- | openstack/common/log.py | 14 | ||||
| -rw-r--r-- | tests/unit/test_log.py | 27 |
2 files changed, 35 insertions, 6 deletions
diff --git a/openstack/common/log.py b/openstack/common/log.py index 6d1d9f9..76962c2 100644 --- a/openstack/common/log.py +++ b/openstack/common/log.py @@ -257,16 +257,18 @@ class PublishErrorsHandler(logging.Handler): dict(error=record.msg)) -def handle_exception(type, value, tb): - extra = {} - if CONF.verbose: - extra['exc_info'] = (type, value, tb) - getLogger().critical(str(value), **extra) +def _create_logging_excepthook(product_name): + def logging_excepthook(type, value, tb): + extra = {} + if CONF.verbose: + extra['exc_info'] = (type, value, tb) + getLogger(product_name).critical(str(value), **extra) + return logging_excepthook def setup(product_name): """Setup logging.""" - sys.excepthook = handle_exception + sys.excepthook = _create_logging_excepthook(product_name) if CONF.log_config: try: diff --git a/tests/unit/test_log.py b/tests/unit/test_log.py index c4ce75b..35ff72d 100644 --- a/tests/unit/test_log.py +++ b/tests/unit/test_log.py @@ -1,7 +1,9 @@ import cStringIO import exceptions import logging +import subprocess import sys +import textwrap from openstack.common import context from openstack.common import cfg @@ -219,6 +221,31 @@ class LegacyFormatterTestCase(test_utils.BaseTestCase): self.assertEqual("NOCTXT: baz --DBG\n", self.stream.getvalue()) +class ExceptionLoggingTestCase(test_utils.BaseTestCase): + """Test that Exceptions are logged""" + + def test_excepthook_logs_exception(self): + code = textwrap.dedent(""" + import sys + from openstack.common import log as logging + + logging.setup('somename') + raise Exception('Some error happened') + """) + + child = subprocess.Popen([ + sys.executable, "-"], + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + + (out, err) = child.communicate(input=code) + + self.assertTrue( + "CRITICAL somename [-] Some error happened", + msg="Exception is not logged") + + class FancyRecordTestCase(test_utils.BaseTestCase): """Test how we handle fancy record keys that are not in the base python logging""" |
