From: Matthijs Kooijman Date: Sat, 7 Aug 2010 19:38:10 +0000 (+0200) Subject: action: Create and use a different Logger for each action. X-Git-Url: https://git.stderr.nl/gitweb?a=commitdiff_plain;h=refs%2Fheads%2Fmaster;p=matthijs%2Fprojects%2Fbackupninja.git action: Create and use a different Logger for each action. This logger is created using the action name as the logger name, so it is included in the log messages. --- diff --git a/src/lib/backupninja/action.py b/src/lib/backupninja/action.py index 7707f3b..fe1947f 100644 --- a/src/lib/backupninja/action.py +++ b/src/lib/backupninja/action.py @@ -22,6 +22,7 @@ """ Running backup actions """ import os +import logging import logging as log from backupninja import config @@ -52,17 +53,20 @@ def run_action(action_config, opts, global_config): configuration file. opts are the parsed commandline options, global_config is the parsed global configuration. """ + config_name = os.path.basename(action_config) # Split the action filename - parts = os.path.basename(action_config).split('.') + parts = os.path.basename(config_name).split('.') if (len(parts) != 2): log.error('Invalid action filename: "%s". Should be in the form name.type, where type is a valid handler.' % action_config) return (action_name, action_ty) = parts - log.info('Running action "%s.%s"', action_name, action_ty) + + action_log = logging.getLogger(config_name) + action_log.info('Running') try: # Create a handler for this action - action = handlers.create_action(action_ty) + action = handlers.create_action(action_ty, logger=action_log) # Let the handler load its configuration file action.load_config(action_config) # Run it @@ -71,12 +75,12 @@ def run_action(action_config, opts, global_config): # Ask the action if there where any failures success = not action.failed except Exception, e: - log.error('Unexpected exception: %s', e) + action_log.error('Unexpected exception: %s', e) import traceback log.debug(traceback.format_exc()) success = False if success: - log.info('Running action "%s.%s" succeeded', action_name, action_ty) + action_log.info('Succeeded') else: - log.info('Running action "%s.%s" failed', action_name, action_ty) + action_log.info('Failed') diff --git a/src/lib/backupninja/handlers/__init__.py b/src/lib/backupninja/handlers/__init__.py index 50abb41..22f9589 100644 --- a/src/lib/backupninja/handlers/__init__.py +++ b/src/lib/backupninja/handlers/__init__.py @@ -47,7 +47,7 @@ class Action(object): which is a combination of a action type and a specific action configuration). """ - def __init__(self): + def __init__(self, logger): # Subclasses should overwrite this with their default config # See backupninja.config.load_config for the structure of this # value. @@ -58,7 +58,7 @@ class Action(object): # A logger object for this action. In the future, this might # become a specific logger, that includes the action name and # type. - self.log = log + self.log = logger def run(self, **kwargs): """