1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
"""
Diagnostic utilities
"""
import functools
import sys
import traceback
def create_log_exception_decorator(logger):
"""Create a decorator that logs and reraises any exceptions that escape
the decorated function
:param logging.Logger logger:
:returns: the decorator
:rtype: callable
Usage example
import logging
from pika.diagnostics_utils import create_log_exception_decorator
_log_exception = create_log_exception_decorator(logging.getLogger(__name__))
@_log_exception
def my_func_or_method():
raise Exception('Oops!')
"""
def log_exception(func):
"""The decorator returned by the parent function
:param func: function to be wrapped
:returns: the function wrapper
:rtype: callable
"""
@functools.wraps(func)
def log_exception_func_wrap(*args, **kwargs):
"""The wrapper function returned by the decorator. Invokes the
function with the given args/kwargs and returns the function's
return value. If the function exits with an exception, logs the
exception traceback and re-raises the
:param args: positional args passed to wrapped function
:param kwargs: keyword args passed to wrapped function
:returns: whatever the wrapped function returns
:rtype: object
"""
try:
return func(*args, **kwargs)
except:
logger.exception(
'Wrapped func exited with exception. Caller\'s stack:\n%s',
''.join(traceback.format_exception(*sys.exc_info())))
raise
return log_exception_func_wrap
return log_exception
|