log: Make the log_exception support a per instance logger.
[matthijs/projects/backupninja.git] / src / lib / backupninja / log.py
1 # -*- mode: python; sh-basic-offset: 4; indent-tabs-mode: nil; -*-
2 # vim: set filetype=python sw=4 sts=4 expandtab autoindent:
3 #
4 #    Backupninja python reimplementation, based on original backupninja program
5 #    by riseup.net.
6 #    Copyright (C) 2010  Matthijs Kooijman <matthijs@stdin.nl>
7 #
8 #    This program is free software; you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation; either version 2 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License along
19 #    with this program; if not, write to the Free Software Foundation, Inc.,
20 #    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
22 """ Sets up the python logging library for use with backupninja """
23
24 import logging
25 log = logging.getLogger()
26
27 def setup_logging(options):
28     """
29     Setup the logging library, so other modules can just use
30     logging.getLogger or the root logger directly.
31     
32     options are the parsed commandline options.
33     """
34     # We use the default options for now
35     level = logging.INFO
36     if(options.debug):
37         level = logging.DEBUG
38     logging.basicConfig(level=level)
39     log.debug("Initialized logging configuration")
40
41 def log_exception(log=None, msg="%s"):
42     """
43     This is a decorator that catches an exception, logs the exception
44     and a backtrace and then swallows it.  The exception is logged using
45     the "error" level for the message, and "debug" for the backtrace.
46
47     log is the Logger instance to log to. If this is not present, the
48     decorated function should be a method of an object that contains a
49     "log" attribute that contains a Logger instance.
50
51     msg is the message to log (which must contain a %s into which the
52     exception message is interpolated).
53
54     """
55     def decorator(f):
56         def inner(*args, **kwargs):
57             try:
58                 f(*args, **kwargs)
59             except Exception, e:
60                 # Find out which logger to use. We create a new variabel
61                 # logger here, since it seems to be impossible to assign
62                 # the log variable from the log_exception scope.
63                 if log is None:
64                     # No log is passed. Get the self argument (args[0]) and
65                     # get is "log" attribute.
66                     logger = args[0].log
67                 else:
68                     # Use the passed log
69                     logger = log
70
71                 logger.error(msg, e)
72                 import traceback
73                 logger.debug(traceback.format_exc())
74         return inner
75     return decorator