commit d78aa613e5f634a26b9e473d5e432e98c20bff3e
Author: Jan Pašek <janpasek97@gmail.com>
Date:   Wed Apr 28 08:24:53 2021 +0200

    Re #8707 - Enhanced logging to set log level based on config file

diff --git a/app.py b/app.py
index 7eb5815..a7dca67 100644
--- a/app.py
+++ b/app.py
@@ -5,6 +5,7 @@ from injector import Injector
 from flask_injector import FlaskInjector
 
 from src.config import configuration
+from src.config.configuration import Configuration
 from src.config.connection_provider import ConnectionProvider
 from src.controllers.certificates_controller import CertController
 from src.controllers.crl_ocsp_controller import CrlOcspController
@@ -92,35 +93,31 @@ def initialize_app(application) -> bool:
     :param application Flask Application to be initialized.
     :return: boolean flag indicating whether initialization was successful or not
     """
-
-    configuration.configure_logging()
-
     modules = [configuration.configure_env_variable, ConnectionProvider]
     injector = Injector(modules)
     FlaskInjector(app=application, modules=modules)
 
+    config = injector.get(Configuration)
+    configuration.configure_logging(config)
+
     # There's a little dependency on the CryptoService, which is not a pretty thing from
     # architectural point of view. However it is only a minimal piece of code and
     # it makes sense to do it in this way instead of trying to run openssl via subprocess here
     cryptography_service = injector.get(CryptographyService)
     try:
         # if version string is returned, OpenSSL is present on the system
-        print(f"Using {cryptography_service.get_openssl_version()}")
         Logger.info(f"Using {cryptography_service.get_openssl_version()}")
-        # TODO log the version instead of prining it out
         return True
     except CryptographyException:
         # If getting the version string throws an exception the OpenSSL is not available
         print("OpenSSL was not located on the system. Application will now exit.")
         Logger.error(f"OpenSSL was not located on the system. Application will now exit.")
-        # TODO add logging here
         return False
 
 
 # app initialization must follow endpoint declaration (after all Flask decoration)
 with app.app_context():
     if not initialize_app(app):
-        # TODO log this
         print("Failed to initialize app, aborting...")
         Logger.error(f"Failed to initialize app, aborting...")
         exit(-1)
diff --git a/db/database_sqlite.db b/db/database_sqlite.db
index 3091ac6..c7e0ef7 100644
Binary files a/db/database_sqlite.db and b/db/database_sqlite.db differ
diff --git a/src/config/configuration.py b/src/config/configuration.py
index f723cd2..b1e21c8 100644
--- a/src/config/configuration.py
+++ b/src/config/configuration.py
@@ -4,10 +4,10 @@ import configparser
 import logging
 from logging import handlers
 
-from injector import singleton
+from injector import singleton, inject
 
 from src.constants import DEFAULT_CONNECTION_STRING, TEST_DATABASE_FILE, DEFAULT_SERVER_BASE_URL, LOG_NAME, \
-    LOG_DIR_LOCATION
+    LOG_DIR_LOCATION, DEFAULT_LOG_LEVEL
 from src.constants import LOG_FILE_LOCATION, LOG_FORMAT
 from src.utils.logger import Logger
 
@@ -16,6 +16,7 @@ DATABASE_CONNECTION_STRING = "ConnectionString"
 
 SERVER_SECTION = "Server"
 SERVER_BASE_URL = "ServerBaseURL"
+SERVER_LOG_LEVEL = "LogLevel"
 
 
 class Configuration:
@@ -31,6 +32,7 @@ class Configuration:
         """
         self.connection_string = DEFAULT_CONNECTION_STRING
         self.base_server_url = DEFAULT_SERVER_BASE_URL
+        self.log_level = DEFAULT_LOG_LEVEL
 
 
 def test_configuration():
@@ -50,9 +52,7 @@ def configure_env_variable(binder):
     :param binder: injector configuration binder instance
     :return: N/A
     """
-    # TODO check: log which configuration is going to be used
     config_name = "X509_CONFIG"
-    Logger.info(f"Using configuration '{config_name}'")
     config_file = os.environ.get(config_name)
     app_configuration = Configuration()
     # if configuration file is not specified use the default configuration
@@ -68,11 +68,11 @@ def configure_env_variable(binder):
             server_config = config[SERVER_SECTION]
             app_configuration.base_server_url = server_config.get(SERVER_BASE_URL,
                                                                   DEFAULT_SERVER_BASE_URL)
-
+            app_configuration.log_level = server_config.get(SERVER_LOG_LEVEL, DEFAULT_LOG_LEVEL)
     binder.bind(Configuration, to=app_configuration, scope=singleton)
 
 
-def configure_logging():
+def configure_logging(config: Configuration):
     if not os.path.exists(LOG_DIR_LOCATION.shortest_relative_path()):
         os.makedirs(LOG_DIR_LOCATION.shortest_relative_path())
 
@@ -82,8 +82,19 @@ def configure_logging():
     formatter = logging.Formatter(LOG_FORMAT)
     handler.setFormatter(formatter)
 
+    # set log level based on config file
     app_logger = logging.getLogger(LOG_NAME)
-    app_logger.setLevel(logging.DEBUG)
+    if config.log_level == "DEBUG":
+        app_logger.setLevel(logging.DEBUG)
+    elif config.log_level == "INFO":
+        app_logger.setLevel(logging.INFO)
+    elif config.log_level == "WARNING":
+        app_logger.setLevel(logging.WARNING)
+    elif config.log_level == "ERROR":
+        app_logger.setLevel(logging.ERROR)
+    else:
+        app_logger.setLevel(logging.DEBUG)#
+
     app_logger.addHandler(handler)
 
     # TODO check is 'valid'
diff --git a/src/constants.py b/src/constants.py
index 3177bd3..63a3062 100644
--- a/src/constants.py
+++ b/src/constants.py
@@ -59,6 +59,7 @@ COL_USAGE_TYPE_ID = "usage_type_id"
 # configuration default
 DEFAULT_CONNECTION_STRING = "db/database_sqlite.db"
 DEFAULT_SERVER_BASE_URL = "http://localhost"
+DEFAULT_LOG_LEVEL = "DEBUG"
 
 # available certificate states and revocation reasons
 CERTIFICATE_STATES = {"valid", "revoked"}
diff --git a/test_server.cfg b/test_server.cfg
index 5fae22f..7340917 100644
--- a/test_server.cfg
+++ b/test_server.cfg
@@ -3,3 +3,4 @@ ConnectionString=db/database_sqlite.db
 
 [Server]
 ServerBaseURL=http://78.128.250.101
+LogLevel=DEBUG
