X-Git-Url: https://git.stderr.nl/gitweb?a=blobdiff_plain;f=src%2Flib%2Fbackupninja%2Flog.py;h=86a31813e50e5f723d1cec9df8fc787ba891b471;hb=96f1913c89c0d934c9b75ff9fae2c0eda44e4823;hp=d079c8887c38ee58b2c41a5148c8a1440339c36b;hpb=2a876c79d5c003fb06fff19881258d6eeb187c0e;p=matthijs%2Fprojects%2Fbackupninja.git diff --git a/src/lib/backupninja/log.py b/src/lib/backupninja/log.py index d079c88..86a3181 100644 --- a/src/lib/backupninja/log.py +++ b/src/lib/backupninja/log.py @@ -32,5 +32,27 @@ def setup_logging(options): options are the parsed commandline options. """ # We use the default options for now - logging.basicConfig(level=logging.INFO) + level = logging.INFO + if(options.debug): + 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