Projekt

Obecné

Profil

« Předchozí | Další » 

Revize d78aa613

Přidáno uživatelem Jan Pašek před téměř 4 roky(ů)

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

Zobrazit rozdíly:

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'

Také k dispozici: Unified diff