level = logging.DEBUG
logging.basicConfig(level=level)
log.debug("Initialized logging configuration")
+
+def log_exception(log, msg="%s"):
+ """
+ This is a decorator that catches an exception, logs the exception
+ and a backtrace and then swallows it. log is the Logging instance to
+ log to (using the "error" level for the message, and "debug" for the
+ backtrace), msg is the message to log (which must contain a %s into
+ which the exception message is interpolated).
+ """
+ def decorator(f):
+ def inner(*args, **kwargs):
+ try:
+ f(*args, **kwargs)
+ except Exception, e:
+ log.error(msg, e)
+ import traceback
+ log.debug(traceback.format_exc())
+ return inner
+ return decorator