From 2991082f1f5bd9d3dfd892c2132322ec54780234 Mon Sep 17 00:00:00 2001 From: Stephan Porada Date: Thu, 12 Sep 2019 16:44:53 +0200 Subject: [PATCH] Add different logging levels. --- config.py | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/config.py b/config.py index d4ac7314..1177ded2 100644 --- a/config.py +++ b/config.py @@ -40,13 +40,15 @@ class DevelopmentConfig(Config): file_path = os.path.join(os.getcwd(), 'logs/opaque_web.log') log = open(file_path, 'w+') log.close() - log_format = ("%(asctime)s - %(levelname)s - %(name)s - " - "%(filename)s - %(lineno)d - %(message)s") - logging.basicConfig(filename='logs/opaque_web.log', level='WARNING', - format=log_format, datefmt='%Y-%m-%d %H:%M:%S', - filemode='w') - logger = logging.getLogger(__name__) - logger.warning('Logging has started with level WARNING.') + if os.environ.get('FLASK_CONFIG') == 'development': + log_format = ("%(asctime)s - %(levelname)s - %(name)s - " + "%(filename)s - %(lineno)d - %(message)s") + logging.basicConfig(filename='logs/opaque_web.log', level='WARNING', + format=log_format, datefmt='%Y-%m-%d %H:%M:%S', + filemode='w') + logger = logging.getLogger(__name__) + logger.warning('Logging has started with level WARNING.' + ' From development config.') class TestingConfig(Config): @@ -71,13 +73,15 @@ class ProductionConfig(Config): file_path = os.path.join(os.getcwd(), 'logs/opaque_web.log') log = open(file_path, 'w+') log.close() - log_format = ("%(asctime)s - %(levelname)s - %(name)s - " - "%(filename)s - %(lineno)d - %(message)s") - logging.basicConfig(filename='logs/opaque_web.log', level='ERROR', - format=log_format, datefmt='%Y-%m-%d %H:%M:%S', - filemode='w') - logger = logging.getLogger(__name__) - logger.error('Logging has started with level ERROR.') + if os.environ.get('FLASK_CONFIG') == 'production': + log_format = ("%(asctime)s - %(levelname)s - %(name)s - " + "%(filename)s - %(lineno)d - %(message)s") + logging.basicConfig(filename='logs/opaque_web.log', level='ERROR', + format=log_format, datefmt='%Y-%m-%d %H:%M:%S', + filemode='w') + logger = logging.getLogger(__name__) + logger.error('Logging has started with level ERROR.' + ' From production config.') config = {