fix log output message and config parameter for log level

This commit is contained in:
Robert Kaussow 2018-06-23 19:38:05 +02:00
parent 6942af8c29
commit fe0b8ce102
2 changed files with 28 additions and 1 deletions

View File

@ -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")

View File

@ -5,3 +5,6 @@ PASSWORD = secret
[DNS]
ZONE = example.com
[LOG]
LEVEL = error