Projekt

Obecné

Profil

Stáhnout (2.57 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
    if not os.path.exists(FileAnchor(config.root_dir, LOG_DIR).shortest_relative_path()):
15
        os.makedirs(FileAnchor(config.root_dir, LOG_DIR).shortest_relative_path())
16

    
17
    handler = logging.handlers.TimedRotatingFileHandler(
18
        os.path.join(FileAnchor(config.root_dir, LOG_DIR).shortest_relative_path(), LOG_FILENAME),
19
        when='H', interval=1)
20
    formatter = logging.Formatter(LOG_FORMAT)
21
    handler.setFormatter(formatter)
22

    
23
    # set log level based on config file
24
    app_logger = logging.getLogger(LOG_NAME)
25
    app_logger.setLevel(LOG_LEVEL_MAPPING.get(config.log_level, logging.DEBUG))
26

    
27
    app_logger.addHandler(handler)
28

    
29
    log = logging.getLogger('werkzeug')
30
    log.disabled = True
31

    
32
    if 'pytest' in sys.modules:
33
        logging.disable(logging.CRITICAL)
34

    
35

    
36
class Logger:
37

    
38
    @staticmethod
39
    def get_names():
40
        stack = inspect.stack()
41

    
42
        file_name = inspect.getmodule(inspect.stack()[2][0]).__file__
43
        class_name = ""
44
        if "self" in stack[2][0].f_locals:
45
            class_name = stack[2][0].f_locals["self"].__class__.__name__ + "."
46
        function_name = stack[2][0].f_code.co_name
47

    
48
        return file_name, class_name, function_name
49

    
50
    @staticmethod
51
    def debug(message: str):
52
        names = Logger.get_names()
53
        app_logger = logging.getLogger(LOG_NAME)
54
        app_logger.debug(f'{Path(names[0]).name}[{names[1]}{names[2]}()]: {message}')
55

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

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

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

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