From: Angus Salkeld Date: Thu, 26 Apr 2012 11:45:01 +0000 (+1000) Subject: Re-add the file logging to cfn tools. X-Git-Tag: 2014.1~1923^2~6 X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=be49adb7fc2c0f0e25c98e6b842a5a0fad90542b;p=openstack-build%2Fheat-build.git Re-add the file logging to cfn tools. Signed-off-by: Angus Salkeld --- diff --git a/heat/cfntools/cfn-hup b/heat/cfntools/cfn-hup index 2d0c1cbf..44f62226 100755 --- a/heat/cfntools/cfn-hup +++ b/heat/cfntools/cfn-hup @@ -22,6 +22,7 @@ import os import os.path import sys + if os.path.exists('/opt/aws/bin'): sys.path.insert(0, '/opt/aws/bin') from cfn_helper import * @@ -46,18 +47,23 @@ parser.add_argument('-v', '--verbose', args = parser.parse_args() # FIXME: implement real arg +logger = logging.getLogger('cfn-hup') +log_file_name = "/var/log/cfn-hup.log" log_format = '%(levelname)s [%(asctime)s] %(message)s' -# setup stdout logging +file_handler = logging.FileHandler(log_file_name) +file_handler.setFormatter(logging.Formatter(log_format)) +logging.getLogger().addHandler(file_handler) + if args.verbose: - logging.basicConfig(format=log_format, level=logging.DEBUG) + logger.basicConfig(format=log_format, level=logging.DEBUG) else: - logging.basicConfig(format=log_format, level=logging.INFO) + logger.basicConfig(format=log_format, level=logging.INFO) main_conf_path = '/etc/cfn/cfn-hup.conf' try: main_config_file = open(main_conf_path) except IOError as exc: - logging.error('Could not open main configuration at %s' % main_conf_path) + logger.error('Could not open main configuration at %s' % main_conf_path) exit(1) config_files = [] @@ -66,7 +72,7 @@ if os.path.exists(hooks_conf_path): try: config_files.append(open(hooks_conf_path)) except IOError as exc: - logging.exception(exc) + logger.exception(exc) if args.config_dir and os.path.exists(args.config_dir): try: @@ -74,21 +80,21 @@ if args.config_dir and os.path.exists(args.config_dir): config_files.append(open(os.path.join(args.config_dir, f))) except OSError as exc: - logging.exception(exc) + logger.exception(exc) if not config_files: - logging.error('No hook files found at %s or %s' % (hooks_conf_path, + logger.error('No hook files found at %s or %s' % (hooks_conf_path, args.config_dir)) exit(1) try: mainconfig = HupConfig([main_config_file] + config_files) except Exception as ex: - logging.error('Cannot load configuration: %s' % str(ex)) + logger.error('Cannot load configuration: %s' % str(ex)) exit(1) if not mainconfig.unique_resources_get(): - logging.error('No hooks were found. Add some to %s or %s' % (hooks_conf_path, + logger.error('No hooks were found. Add some to %s or %s' % (hooks_conf_path, args.config_dir)) exit(1) @@ -102,5 +108,5 @@ for r in mainconfig.unique_resources_get(): try: metadata.cfn_hup(mainconfig.hooks) except Exception as e: - logging.exception("Error processing metadata") + logger.exception("Error processing metadata") exit(1) diff --git a/heat/cfntools/cfn-init b/heat/cfntools/cfn-init index 155289e4..37e7a911 100755 --- a/heat/cfntools/cfn-init +++ b/heat/cfntools/cfn-init @@ -42,8 +42,13 @@ if os.path.exists('/opt/aws/bin'): else: from heat.cfntools.cfn_helper import * +logger = logging.getLogger('cfn-init') +log_file_name = "/var/log/cfn-init.log" log_format = '%(levelname)s [%(asctime)s] %(message)s' -logging.basicConfig(format=log_format, level=logging.INFO) +file_handler = logging.FileHandler(log_file_name) +file_handler.setFormatter(logging.Formatter(log_format)) +logger.getLogger().addHandler(file_handler) +logger.basicConfig(format=log_format, level=logging.DEBUG) description = " " parser = argparse.ArgumentParser(description=description) @@ -79,5 +84,5 @@ metadata.retrieve() try: metadata.cfn_init() except Exception as e: - logging.exception("Error processing metadata") + logger.exception("Error processing metadata") exit(1)