Projekt

Obecné

Profil

Stáhnout (2.73 KB) Statistiky
| Větev: | Tag: | Revize:
1
import inspect
2
import logging
3
import sys
4
from logging import handlers
5
import os
6
from pathlib import Path
7

    
8
from src.config.configuration import Configuration, LOG_LEVEL_MAPPING
9
from src.constants import LOG_NAME, LOG_DIR, LOG_FILENAME, LOG_FORMAT
10
from src.utils.file_anchor import FileAnchor
11

    
12

    
13
def configure_logging(config: Configuration):
14
    root_path = FileAnchor(config.root_dir, LOG_DIR).shortest_relative_path()
15
    if root_path is None:
16
        print(f"Critical error, '{config.root_dir}' does not exist in '{os.getcwd()}'.")
17
        exit(1)
18

    
19
    if not os.path.exists(root_path):
20
        os.makedirs(FileAnchor(config.root_dir, LOG_DIR).shortest_relative_path())
21

    
22
    handler = logging.handlers.TimedRotatingFileHandler(
23
        os.path.join(FileAnchor(config.root_dir, LOG_DIR).shortest_relative_path(), LOG_FILENAME),
24
        when='H', interval=1)
25
    formatter = logging.Formatter(LOG_FORMAT)
26
    handler.setFormatter(formatter)
27

    
28
    # set log level based on config file
29
    app_logger = logging.getLogger(LOG_NAME)
30
    app_logger.setLevel(LOG_LEVEL_MAPPING.get(config.log_level, logging.DEBUG))
31

    
32
    app_logger.addHandler(handler)
33

    
34
    log = logging.getLogger('werkzeug')
35
    log.disabled = True
36

    
37
    if 'pytest' in sys.modules:
38
        logging.disable(logging.CRITICAL)
39

    
40

    
41
class Logger:
42

    
43
    @staticmethod
44
    def get_names():
45
        stack = inspect.stack()
46

    
47
        file_name = inspect.getmodule(inspect.stack()[2][0]).__file__
48
        class_name = ""
49
        if "self" in stack[2][0].f_locals:
50
            class_name = stack[2][0].f_locals["self"].__class__.__name__ + "."
51
        function_name = stack[2][0].f_code.co_name
52

    
53
        return file_name, class_name, function_name
54

    
55
    @staticmethod
56
    def debug(message: str):
57
        names = Logger.get_names()
58
        app_logger = logging.getLogger(LOG_NAME)
59
        app_logger.debug(f'{Path(names[0]).name}[{names[1]}{names[2]}()]: {message}')
60

    
61
    @staticmethod
62
    def info(message: str):
63
        names = Logger.get_names()
64
        app_logger = logging.getLogger(LOG_NAME)
65
        app_logger.info(f'{Path(names[0]).name}[{names[1]}{names[2]}()]: {message}')
66

    
67
    @staticmethod
68
    def warning(message: str):
69
        names = Logger.get_names()
70
        app_logger = logging.getLogger(LOG_NAME)
71
        app_logger.warning(f'{Path(names[0]).name}[{names[1]}{names[2]}()]: {message}')
72

    
73
    @staticmethod
74
    def error(message: str):
75
        names = Logger.get_names()
76
        app_logger = logging.getLogger(LOG_NAME)
77
        app_logger.error(f'{Path(names[0]).name}[{names[1]}.{names[2]}()]: {message}')
78

    
79
    @staticmethod
80
    def critical(message: str):
81
        names = Logger.get_names()
82
        app_logger = logging.getLogger(LOG_NAME)
83
        app_logger.critical(f'{Path(names[0]).name}[{names[1]}.{names[2]}()]: {message}')
(3-3/6)