Revize d78aa613
Přidáno uživatelem Jan Pašek před téměř 4 roky(ů)
app.py | ||
---|---|---|
5 | 5 |
from flask_injector import FlaskInjector |
6 | 6 |
|
7 | 7 |
from src.config import configuration |
8 |
from src.config.configuration import Configuration |
|
8 | 9 |
from src.config.connection_provider import ConnectionProvider |
9 | 10 |
from src.controllers.certificates_controller import CertController |
10 | 11 |
from src.controllers.crl_ocsp_controller import CrlOcspController |
... | ... | |
92 | 93 |
:param application Flask Application to be initialized. |
93 | 94 |
:return: boolean flag indicating whether initialization was successful or not |
94 | 95 |
""" |
95 |
|
|
96 |
configuration.configure_logging() |
|
97 |
|
|
98 | 96 |
modules = [configuration.configure_env_variable, ConnectionProvider] |
99 | 97 |
injector = Injector(modules) |
100 | 98 |
FlaskInjector(app=application, modules=modules) |
101 | 99 |
|
100 |
config = injector.get(Configuration) |
|
101 |
configuration.configure_logging(config) |
|
102 |
|
|
102 | 103 |
# There's a little dependency on the CryptoService, which is not a pretty thing from |
103 | 104 |
# architectural point of view. However it is only a minimal piece of code and |
104 | 105 |
# it makes sense to do it in this way instead of trying to run openssl via subprocess here |
105 | 106 |
cryptography_service = injector.get(CryptographyService) |
106 | 107 |
try: |
107 | 108 |
# if version string is returned, OpenSSL is present on the system |
108 |
print(f"Using {cryptography_service.get_openssl_version()}") |
|
109 | 109 |
Logger.info(f"Using {cryptography_service.get_openssl_version()}") |
110 |
# TODO log the version instead of prining it out |
|
111 | 110 |
return True |
112 | 111 |
except CryptographyException: |
113 | 112 |
# If getting the version string throws an exception the OpenSSL is not available |
114 | 113 |
print("OpenSSL was not located on the system. Application will now exit.") |
115 | 114 |
Logger.error(f"OpenSSL was not located on the system. Application will now exit.") |
116 |
# TODO add logging here |
|
117 | 115 |
return False |
118 | 116 |
|
119 | 117 |
|
120 | 118 |
# app initialization must follow endpoint declaration (after all Flask decoration) |
121 | 119 |
with app.app_context(): |
122 | 120 |
if not initialize_app(app): |
123 |
# TODO log this |
|
124 | 121 |
print("Failed to initialize app, aborting...") |
125 | 122 |
Logger.error(f"Failed to initialize app, aborting...") |
126 | 123 |
exit(-1) |
src/config/configuration.py | ||
---|---|---|
4 | 4 |
import logging |
5 | 5 |
from logging import handlers |
6 | 6 |
|
7 |
from injector import singleton |
|
7 |
from injector import singleton, inject
|
|
8 | 8 |
|
9 | 9 |
from src.constants import DEFAULT_CONNECTION_STRING, TEST_DATABASE_FILE, DEFAULT_SERVER_BASE_URL, LOG_NAME, \ |
10 |
LOG_DIR_LOCATION |
|
10 |
LOG_DIR_LOCATION, DEFAULT_LOG_LEVEL
|
|
11 | 11 |
from src.constants import LOG_FILE_LOCATION, LOG_FORMAT |
12 | 12 |
from src.utils.logger import Logger |
13 | 13 |
|
... | ... | |
16 | 16 |
|
17 | 17 |
SERVER_SECTION = "Server" |
18 | 18 |
SERVER_BASE_URL = "ServerBaseURL" |
19 |
SERVER_LOG_LEVEL = "LogLevel" |
|
19 | 20 |
|
20 | 21 |
|
21 | 22 |
class Configuration: |
... | ... | |
31 | 32 |
""" |
32 | 33 |
self.connection_string = DEFAULT_CONNECTION_STRING |
33 | 34 |
self.base_server_url = DEFAULT_SERVER_BASE_URL |
35 |
self.log_level = DEFAULT_LOG_LEVEL |
|
34 | 36 |
|
35 | 37 |
|
36 | 38 |
def test_configuration(): |
... | ... | |
50 | 52 |
:param binder: injector configuration binder instance |
51 | 53 |
:return: N/A |
52 | 54 |
""" |
53 |
# TODO check: log which configuration is going to be used |
|
54 | 55 |
config_name = "X509_CONFIG" |
55 |
Logger.info(f"Using configuration '{config_name}'") |
|
56 | 56 |
config_file = os.environ.get(config_name) |
57 | 57 |
app_configuration = Configuration() |
58 | 58 |
# if configuration file is not specified use the default configuration |
... | ... | |
68 | 68 |
server_config = config[SERVER_SECTION] |
69 | 69 |
app_configuration.base_server_url = server_config.get(SERVER_BASE_URL, |
70 | 70 |
DEFAULT_SERVER_BASE_URL) |
71 |
|
|
71 |
app_configuration.log_level = server_config.get(SERVER_LOG_LEVEL, DEFAULT_LOG_LEVEL) |
|
72 | 72 |
binder.bind(Configuration, to=app_configuration, scope=singleton) |
73 | 73 |
|
74 | 74 |
|
75 |
def configure_logging(): |
|
75 |
def configure_logging(config: Configuration):
|
|
76 | 76 |
if not os.path.exists(LOG_DIR_LOCATION.shortest_relative_path()): |
77 | 77 |
os.makedirs(LOG_DIR_LOCATION.shortest_relative_path()) |
78 | 78 |
|
... | ... | |
82 | 82 |
formatter = logging.Formatter(LOG_FORMAT) |
83 | 83 |
handler.setFormatter(formatter) |
84 | 84 |
|
85 |
# set log level based on config file |
|
85 | 86 |
app_logger = logging.getLogger(LOG_NAME) |
86 |
app_logger.setLevel(logging.DEBUG) |
|
87 |
if config.log_level == "DEBUG": |
|
88 |
app_logger.setLevel(logging.DEBUG) |
|
89 |
elif config.log_level == "INFO": |
|
90 |
app_logger.setLevel(logging.INFO) |
|
91 |
elif config.log_level == "WARNING": |
|
92 |
app_logger.setLevel(logging.WARNING) |
|
93 |
elif config.log_level == "ERROR": |
|
94 |
app_logger.setLevel(logging.ERROR) |
|
95 |
else: |
|
96 |
app_logger.setLevel(logging.DEBUG)# |
|
97 |
|
|
87 | 98 |
app_logger.addHandler(handler) |
88 | 99 |
|
89 | 100 |
# TODO check is 'valid' |
src/constants.py | ||
---|---|---|
59 | 59 |
# configuration default |
60 | 60 |
DEFAULT_CONNECTION_STRING = "db/database_sqlite.db" |
61 | 61 |
DEFAULT_SERVER_BASE_URL = "http://localhost" |
62 |
DEFAULT_LOG_LEVEL = "DEBUG" |
|
62 | 63 |
|
63 | 64 |
# available certificate states and revocation reasons |
64 | 65 |
CERTIFICATE_STATES = {"valid", "revoked"} |
test_server.cfg | ||
---|---|---|
3 | 3 |
|
4 | 4 |
[Server] |
5 | 5 |
ServerBaseURL=http://78.128.250.101 |
6 |
LogLevel=DEBUG |
Také k dispozici: Unified diff
Re #8707 - Enhanced logging to set log level based on config file