diff options
author | Joshua Harlow <harlowja@gmail.com> | 2015-02-22 17:22:20 -0800 |
---|---|---|
committer | Joshua Harlow <harlowja@yahoo-inc.com> | 2015-02-24 12:58:18 -0800 |
commit | 2e6517151e2c85e89e8ba3055f7ab9428f495704 (patch) | |
tree | 7ab32016f3d1cb9dca8a48c828885e6438d74d46 | |
parent | db5a0c6284f419e0a025476653e19b8fcc9bcd6c (diff) | |
download | oslo-utils-2e6517151e2c85e89e8ba3055f7ab9428f495704.tar.gz |
Allow providing a logger to save_and_reraise_exception
Instead of always assuming the best logger to use when
a exception is being dropped is the root logger (which
it may not always be) allow for providing a custom
logger when using this functionality + class so that
users can direct it to a logger of there choosing.
Change-Id: Ic56892be9d007229f5c302a2c6da19f00e91752e
-rw-r--r-- | oslo_utils/excutils.py | 13 | ||||
-rw-r--r-- | oslo_utils/tests/test_excutils.py | 61 | ||||
-rw-r--r-- | tests/test_excutils.py | 49 |
3 files changed, 70 insertions, 53 deletions
diff --git a/oslo_utils/excutils.py b/oslo_utils/excutils.py index c5839d6..4e4c669 100644 --- a/oslo_utils/excutils.py +++ b/oslo_utils/excutils.py @@ -63,8 +63,11 @@ class save_and_reraise_exception(object): # Not raising a new exception, so reraise ctxt.reraise = True """ - def __init__(self, reraise=True): + def __init__(self, reraise=True, logger=None): self.reraise = reraise + if logger is None: + logger = logging.getLogger() + self.logger = logger def __enter__(self): self.type_, self.value, self.tb, = sys.exc_info() @@ -73,10 +76,10 @@ class save_and_reraise_exception(object): def __exit__(self, exc_type, exc_val, exc_tb): if exc_type is not None: if self.reraise: - logging.error(_LE('Original exception being dropped: %s'), - traceback.format_exception(self.type_, - self.value, - self.tb)) + self.logger.error(_LE('Original exception being dropped: %s'), + traceback.format_exception(self.type_, + self.value, + self.tb)) return False if self.reraise: six.reraise(self.type_, self.value, self.tb) diff --git a/oslo_utils/tests/test_excutils.py b/oslo_utils/tests/test_excutils.py index 92bf39c..c2626a4 100644 --- a/oslo_utils/tests/test_excutils.py +++ b/oslo_utils/tests/test_excutils.py @@ -41,21 +41,21 @@ class SaveAndReraiseTest(test_base.BaseTestCase): self.assertEqual(str(e), msg) - def test_save_and_reraise_exception_dropped(self): + @mock.patch('logging.getLogger') + def test_save_and_reraise_exception_dropped(self, get_logger_mock): + logger = get_logger_mock() e = None msg = 'second exception' - with mock.patch('logging.error') as log: + try: try: - try: - raise Exception('dropped') - except Exception: - with excutils.save_and_reraise_exception(): - raise Exception(msg) - except Exception as _e: - e = _e - - self.assertEqual(str(e), msg) - self.assertTrue(log.called) + raise Exception('dropped') + except Exception: + with excutils.save_and_reraise_exception(): + raise Exception(msg) + except Exception as _e: + e = _e + self.assertEqual(str(e), msg) + self.assertTrue(logger.error.called) def test_save_and_reraise_exception_no_reraise(self): """Test that suppressing the reraise works.""" @@ -65,21 +65,34 @@ class SaveAndReraiseTest(test_base.BaseTestCase): with excutils.save_and_reraise_exception() as ctxt: ctxt.reraise = False - def test_save_and_reraise_exception_dropped_no_reraise(self): + @mock.patch('logging.getLogger') + def test_save_and_reraise_exception_dropped_no_reraise(self, + get_logger_mock): + logger = get_logger_mock() e = None msg = 'second exception' - with mock.patch('logging.error') as log: + try: try: - try: - raise Exception('dropped') - except Exception: - with excutils.save_and_reraise_exception(reraise=False): - raise Exception(msg) - except Exception as _e: - e = _e - - self.assertEqual(str(e), msg) - self.assertFalse(log.called) + raise Exception('dropped') + except Exception: + with excutils.save_and_reraise_exception(reraise=False): + raise Exception(msg) + except Exception as _e: + e = _e + self.assertEqual(str(e), msg) + self.assertFalse(logger.error.called) + + def test_save_and_reraise_exception_provided_logger(self): + fake_logger = mock.MagicMock() + try: + try: + raise Exception('foo') + except Exception: + with excutils.save_and_reraise_exception(logger=fake_logger): + raise Exception('second exception') + except Exception: + pass + self.assertTrue(fake_logger.error.called) class ForeverRetryUncaughtExceptionsTest(test_base.BaseTestCase): diff --git a/tests/test_excutils.py b/tests/test_excutils.py index 2ccac55..3f35e79 100644 --- a/tests/test_excutils.py +++ b/tests/test_excutils.py @@ -41,21 +41,21 @@ class SaveAndReraiseTest(test_base.BaseTestCase): self.assertEqual(str(e), msg) - def test_save_and_reraise_exception_dropped(self): + @mock.patch('logging.getLogger') + def test_save_and_reraise_exception_dropped(self, get_logger_mock): + logger = get_logger_mock() e = None msg = 'second exception' - with mock.patch('logging.error') as log: + try: try: - try: - raise Exception('dropped') - except Exception: - with excutils.save_and_reraise_exception(): - raise Exception(msg) - except Exception as _e: - e = _e - - self.assertEqual(str(e), msg) - self.assertTrue(log.called) + raise Exception('dropped') + except Exception: + with excutils.save_and_reraise_exception(): + raise Exception(msg) + except Exception as _e: + e = _e + self.assertEqual(str(e), msg) + self.assertTrue(logger.error.called) def test_save_and_reraise_exception_no_reraise(self): """Test that suppressing the reraise works.""" @@ -65,21 +65,22 @@ class SaveAndReraiseTest(test_base.BaseTestCase): with excutils.save_and_reraise_exception() as ctxt: ctxt.reraise = False - def test_save_and_reraise_exception_dropped_no_reraise(self): + @mock.patch('logging.getLogger') + def test_save_and_reraise_exception_dropped_no_reraise(self, + get_logger_mock): + logger = get_logger_mock() e = None msg = 'second exception' - with mock.patch('logging.error') as log: + try: try: - try: - raise Exception('dropped') - except Exception: - with excutils.save_and_reraise_exception(reraise=False): - raise Exception(msg) - except Exception as _e: - e = _e - - self.assertEqual(str(e), msg) - self.assertFalse(log.called) + raise Exception('dropped') + except Exception: + with excutils.save_and_reraise_exception(reraise=False): + raise Exception(msg) + except Exception as _e: + e = _e + self.assertEqual(str(e), msg) + self.assertFalse(logger.error.called) class ForeverRetryUncaughtExceptionsTest(test_base.BaseTestCase): |