From fe0b8ce1021b81720e7f7dfafba3419766712a87 Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Sat, 23 Jun 2018 19:38:05 +0200 Subject: [PATCH] fix log output message and config parameter for log level --- authenticator.py | 26 +++++++++++++++++++++++++- example_config.ini | 3 +++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/authenticator.py b/authenticator.py index ba87c65..2628f33 100755 --- a/authenticator.py +++ b/authenticator.py @@ -27,7 +27,8 @@ def setup_logger(): ch = logging.StreamHandler() ch.setLevel(logging.INFO) - formatter = logging.Formatter('%(asctime)s - %(name)s- %(levelname)s - %(message)s') + formatter = logging.Formatter( + '%(asctime)s - %(name)s - %(levelname)s: %(message)s', datefmt='%Y-%m-%d,%H:%M:%S') ch.setFormatter(formatter) # add handler to logger logger.addHandler(ch) @@ -35,6 +36,28 @@ def setup_logger(): return logger +def set_log_level(config): + try: + loglevel = str(config.get('LOG', 'LEVEL')).upper() + except (configparser.NoSectionError, configparser.NoOptionError) as e: + logger.error("Config error: %s" % (e)) + sys.exit(1) + + if loglevel == 'INFO': + level = logging.INFO + elif loglevel == 'ERROR': + level = logging.ERROR + elif loglevel == 'DEBUG': + level = logging.DEBUG + else: + level = logging.INFO + + logger = logging.getLogger('certbot_dns_corenetworks') + logger.setLevel(level) + for handler in logger.handlers: + handler.setLevel(level) + + def load_config(logger): config_path = os.path.join(os.path.expanduser("~"), ".certbot_dns_corenetworks", "config.ini") @@ -181,6 +204,7 @@ def main(): logger = setup_logger() config = load_config(logger) + set_log_level(config) # get auth token logger.info("Try to get auth token") diff --git a/example_config.ini b/example_config.ini index 63092be..5465ed7 100644 --- a/example_config.ini +++ b/example_config.ini @@ -5,3 +5,6 @@ PASSWORD = secret [DNS] ZONE = example.com + +[LOG] +LEVEL = error