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
This commit is contained in:
parent
db5a0c6284
commit
2e6517151e
@ -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)
|
||||
|
@ -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
|
||||
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)
|
||||
|
||||
self.assertEqual(str(e), msg)
|
||||
self.assertFalse(log.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):
|
||||
|
@ -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):
|
||||
|
Loading…
Reference in New Issue
Block a user