1 # -*- mode: python; sh-basic-offset: 4; indent-tabs-mode: nil; -*-
2 # vim: set filetype=python sw=4 sts=4 expandtab autoindent:
4 # Backupninja python reimplementation, based on original backupninja program
6 # Copyright (C) 2010 Matthijs Kooijman <matthijs@stdin.nl>
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.
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.
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.
22 """ Sets up the python logging library for use with backupninja """
25 log = logging.getLogger()
27 def setup_logging(options):
29 Setup the logging library, so other modules can just use
30 logging.getLogger or the root logger directly.
32 options are the parsed commandline options.
34 # We use the default options for now
38 logging.basicConfig(level=level)
39 log.debug("Initialized logging configuration")
41 def log_exception(log=None, msg="%s"):
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.
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.
51 msg is the message to log (which must contain a %s into which the
52 exception message is interpolated).
56 def inner(*args, **kwargs):
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.
64 # No log is passed. Get the self argument (args[0]) and
65 # get is "log" attribute.
73 logger.debug(traceback.format_exc())