From 92b73b563bb5f4aa55edab7081e250f6304db29a Mon Sep 17 00:00:00 2001 From: pran1990 Date: Mon, 14 Apr 2014 00:53:50 -0700 Subject: [PATCH] Make entropy suitable for pypi distribution, part 1 Set logging handlers in each file, instead of a global one Move CLI output to stdout Remove one hardcoded value Change-Id: I0d1bfcbd642bdc43547bf177bed53c32eaf956b9 --- entropy/__main__.py | 24 ++++++++++++++---------- entropy/engine.py | 11 +++++++++++ entropy/examples/audit/vmbooter.py | 10 ++++++++-- entropy/examples/repair/vmbooter.py | 6 ++++++ 4 files changed, 39 insertions(+), 12 deletions(-) diff --git a/entropy/__main__.py b/entropy/__main__.py index 7263401..29864f1 100644 --- a/entropy/__main__.py +++ b/entropy/__main__.py @@ -33,15 +33,17 @@ LOG = logging.getLogger(__name__) # TODO(praneshp): Only hardcoded stuff in the project. Find a way to move engine_cfg = os.path.join(os.getcwd(), 'entropy', 'examples', 'cfg', 'engines.cfg') -log_file = os.path.join(os.getcwd(), 'entropy', 'examples', - 'logs', 'entropy.log') def get_cfg_file(engine, script_type): cfg_key = {'audit': 'audit_cfg', 'repair': 'repair_cfg'} - engine_config = dict(utils.load_yaml(engine_cfg).next())[engine] - this_engine_cfg = dict(utils.load_yaml(engine_config).next())[engine] - return this_engine_cfg[cfg_key[script_type]] + try: + engine_config = dict(utils.load_yaml(engine_cfg).next())[engine] + this_engine_cfg = dict(utils.load_yaml(engine_config).next())[engine] + return this_engine_cfg[cfg_key[script_type]] + except KeyError: + LOG.exception('Could not find engine/react script') + return None def add_to_list(engine, script_type, **kwargs): @@ -149,9 +151,11 @@ def parse(): if __name__ == '__main__': - #TODO(praneshp): AMQP, json->yaml, reaction scripts(after amqp) - FORMAT = '%(filename)s %(lineno)s %(message)s' - logging.basicConfig(filename=log_file, - level=logging.DEBUG, - format=FORMAT) + FORMAT = '%(lineno)s %(message)s' + console = logging.StreamHandler() + console.setLevel(logging.DEBUG) + console.setFormatter(FORMAT) + LOG.addHandler(console) + print LOG.handlers +# logging.basicConfig(level=logging.DEBUG) parse() diff --git a/entropy/engine.py b/entropy/engine.py index e81a890..b04f69b 100644 --- a/entropy/engine.py +++ b/entropy/engine.py @@ -31,6 +31,7 @@ LOG = logging.getLogger(__name__) class Engine(object): def __init__(self, name, **cfg_data): + Engine.set_logger(**cfg_data) # constants # TODO(praneshp): Hardcode for now, could/should be cmdline input self.max_workers = 8 @@ -50,6 +51,16 @@ class Engine(object): self.futures = [] LOG.info('Created engine obj %s', self.name) + @staticmethod + def set_logger(**cfg_data): + # Set the logger + LOG.handlers = [] + log_to_file = logging.FileHandler(cfg_data['log_file']) + log_to_file.setLevel(logging.DEBUG) + log_format = logging.Formatter(cfg_data['log_format']) + log_to_file.setFormatter(log_format) + LOG.addHandler(log_to_file) + def run(self): LOG.info('Starting Scheduler for %s', self.name) self.start_scheduler() diff --git a/entropy/examples/audit/vmbooter.py b/entropy/examples/audit/vmbooter.py index a39190b..4cc1d4c 100644 --- a/entropy/examples/audit/vmbooter.py +++ b/entropy/examples/audit/vmbooter.py @@ -23,12 +23,12 @@ from kombu.pools import producers from novaclient.client import Client import paramiko -from entropy.audit import base +from entropy.audit.base import AuditBase LOG = logging.getLogger(__name__) -class Audit(base.AuditBase): +class Audit(AuditBase): # TODO(praneshp): this can be done with plumbum instead. @staticmethod def remote_call(cmd, **kwargs): @@ -120,6 +120,12 @@ class Audit(base.AuditBase): 'boot': Audit.remote_call(boot_command, **kwargs)} def send_message(self, **kwargs): + LOG.handlers = [] + log_to_file = logging.FileHandler(kwargs['log_file']) + log_to_file.setLevel(logging.DEBUG) + log_format = logging.Formatter(kwargs['log_format']) + log_to_file.setFormatter(log_format) + LOG.addHandler(log_to_file) connection = BrokerConnection('amqp://%(mq_user)s:%(mq_password)s@' '%(mq_host)s:%(mq_port)s//' % kwargs['mq_args']) diff --git a/entropy/examples/repair/vmbooter.py b/entropy/examples/repair/vmbooter.py index 8d30f3c..7ef6afd 100644 --- a/entropy/examples/repair/vmbooter.py +++ b/entropy/examples/repair/vmbooter.py @@ -84,6 +84,12 @@ def parse_conf(conf): def main(**kwargs): + LOG.handlers = [] + log_to_file = logging.FileHandler(kwargs['log_file']) + log_to_file.setLevel(logging.DEBUG) + log_format = logging.Formatter(kwargs['log_format']) + log_to_file.setFormatter(log_format) + LOG.addHandler(log_to_file) LOG.info('starting react script %s' % kwargs['name']) args = parse_conf(kwargs['conf']) recv_message(**args)