Revize 5678036c
Přidáno uživatelem David Friesecký před téměř 4 roky(ů)
src/config/configuration.py | ||
---|---|---|
2 | 2 |
|
3 | 3 |
import configparser |
4 | 4 |
import logging |
5 |
from logging import handlers |
|
6 | 5 |
|
7 |
from injector import singleton, inject
|
|
6 |
from injector import singleton |
|
8 | 7 |
|
9 |
from src.constants import DEFAULT_CONNECTION_STRING, TEST_DATABASE_FILE, DEFAULT_SERVER_BASE_URL, LOG_NAME, \ |
|
10 |
LOG_DIR, DEFAULT_LOG_LEVEL, LOG_FILENAME |
|
11 |
from src.constants import LOG_FILE_LOCATION, LOG_FORMAT |
|
12 |
from src.utils.logger import Logger |
|
8 |
from src.constants import DEFAULT_CONNECTION_STRING, TEST_DATABASE_FILE, DEFAULT_SERVER_BASE_URL, \ |
|
9 |
DEFAULT_LOG_LEVEL, DEFAULT_ROOT_DIR |
|
13 | 10 |
|
14 | 11 |
DATABASE_SECTION = "Database" |
15 | 12 |
DATABASE_CONNECTION_STRING = "ConnectionString" |
... | ... | |
17 | 14 |
SERVER_SECTION = "Server" |
18 | 15 |
SERVER_BASE_URL = "ServerBaseURL" |
19 | 16 |
SERVER_LOG_LEVEL = "LogLevel" |
17 |
SERVER_ROOT_DIR = "RootDir" |
|
20 | 18 |
|
21 | 19 |
LOG_LEVEL_MAPPING = { |
22 | 20 |
"DEBUG": logging.DEBUG, |
... | ... | |
42 | 40 |
self.connection_string = DEFAULT_CONNECTION_STRING |
43 | 41 |
self.base_server_url = DEFAULT_SERVER_BASE_URL |
44 | 42 |
self.log_level = DEFAULT_LOG_LEVEL |
43 |
self.root_dir = DEFAULT_ROOT_DIR |
|
45 | 44 |
|
46 | 45 |
|
47 | 46 |
def test_configuration(): |
... | ... | |
79 | 78 |
app_configuration.base_server_url = server_config.get(SERVER_BASE_URL, |
80 | 79 |
DEFAULT_SERVER_BASE_URL) |
81 | 80 |
app_configuration.log_level = server_config.get(SERVER_LOG_LEVEL, DEFAULT_LOG_LEVEL) |
81 |
app_configuration.root_dir = server_config.get(SERVER_ROOT_DIR, DEFAULT_ROOT_DIR) |
|
82 | 82 |
|
83 | 83 |
binder.bind(Configuration, to=app_configuration, scope=singleton) |
84 |
|
|
85 |
|
|
86 |
def configure_logging(config: Configuration): |
|
87 |
if not os.path.exists(LOG_DIR): |
|
88 |
os.makedirs(LOG_DIR) |
|
89 |
|
|
90 |
handler = logging.handlers.TimedRotatingFileHandler( |
|
91 |
os.path.join(LOG_DIR, LOG_FILENAME), |
|
92 |
when='H', interval=1) |
|
93 |
formatter = logging.Formatter(LOG_FORMAT) |
|
94 |
handler.setFormatter(formatter) |
|
95 |
|
|
96 |
# set log level based on config file |
|
97 |
app_logger = logging.getLogger(LOG_NAME) |
|
98 |
app_logger.setLevel(LOG_LEVEL_MAPPING.get(config.log_level, logging.DEBUG)) |
|
99 |
|
|
100 |
app_logger.addHandler(handler) |
|
101 |
|
|
102 |
# TODO check is 'valid' |
|
103 |
log = logging.getLogger('werkzeug') |
|
104 |
log.disabled = True |
Také k dispozici: Unified diff
Re #8880 - Edited config file (inserted RootDir)
- improved creating of logs and db directories