From afd7469178c34cbd5389e70b42901f71cf3d5da5 Mon Sep 17 00:00:00 2001
From: Patrick Jentsch <pjentsch@sfb1288inf-Laptop.fritz.box>
Date: Wed, 27 May 2020 11:03:50 +0200
Subject: [PATCH] Rework logging code

---
 config.py | 43 ++++++++++++++++---------------------------
 1 file changed, 16 insertions(+), 27 deletions(-)

diff --git a/config.py b/config.py
index 3a93dbc1..0128ca86 100644
--- a/config.py
+++ b/config.py
@@ -24,6 +24,13 @@ class Config:
     NOPAQUE_MAIL_SUBJECT_PREFIX = '[nopaque]'
     NOPAQUE_STORAGE = os.environ.get('NOPAQUE_STORAGE')
 
+    os.makedirs('logs', exist_ok=True)
+    logging.basicConfig(filename='logs/nopaque.log',
+                        format='[%(asctime)s] %(levelname)s in '
+                               '%(name)s/%(filename)s, line %(lineno)d:'
+                               '%(message)s',
+                        datefmt='%Y-%m-%d %H:%M:%S', filemode='w')
+
     @staticmethod
     def init_app(app):
         pass
@@ -38,19 +45,10 @@ class DevelopmentConfig(Config):
         os.environ.get('POSTGRES_USER'),
         os.environ.get('POSTGRES_PASSWORD'),
         os.environ.get('POSTGRES_DB_NAME'))
-    if not os.path.isfile('logs/nopaque.log'):
-        file_path = os.path.join(os.getcwd(), 'logs/nopaque.log')
-        log = open(file_path, 'w+')
-        log.close()
-    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/nopaque.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.')
+
+    ''' ### nopaque ### '''
+    NOPAQUE_LOG_LEVEL = os.environ.get('NOPAQUE_LOG_LEVEL') or 'DEBUG'
+    logging.basicConfig(level=NOPAQUE_LOG_LEVEL)
 
 
 class TestingConfig(Config):
@@ -71,24 +69,15 @@ class ProductionConfig(Config):
         os.environ.get('POSTGRES_PASSWORD'),
         os.environ.get('POSTGRES_DB_NAME')
     )
-    if not os.path.isfile('logs/nopaque.log'):
-        file_path = os.path.join(os.getcwd(), 'logs/nopaque.log')
-        log = open(file_path, 'w+')
-        log.close()
-    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/nopaque.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.')
+
+    ''' ### nopaque ### '''
+    NOPAQUE_LOG_LEVEL = os.environ.get('NOPAQUE_LOG_LEVEL') or 'ERROR'
+    logging.basicConfig(level=NOPAQUE_LOG_LEVEL)
 
 
 config = {
     'development': DevelopmentConfig,
     'testing': TestingConfig,
     'production': ProductionConfig,
-    'default': DevelopmentConfig
+    'default': DevelopmentConfig,
 }
-- 
GitLab