1 |
1bdc90c0
|
David Friesecký
|
import inspect
|
2 |
dbca269d
|
David Friesecký
|
import logging
|
3 |
1769e2e2
|
David Friesecký
|
import sys
|
4 |
5678036c
|
David Friesecký
|
from logging import handlers
|
5 |
|
|
import os
|
6 |
1bdc90c0
|
David Friesecký
|
from pathlib import Path
|
7 |
dbca269d
|
David Friesecký
|
|
8 |
5678036c
|
David Friesecký
|
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 |
ed35ce72
|
David Friesecký
|
|
32 |
1769e2e2
|
David Friesecký
|
if 'pytest' in sys.modules:
|
33 |
|
|
logging.disable(logging.CRITICAL)
|
34 |
|
|
|
35 |
dbca269d
|
David Friesecký
|
|
36 |
|
|
class Logger:
|
37 |
|
|
|
38 |
|
|
@staticmethod
|
39 |
1bdc90c0
|
David Friesecký
|
def get_names():
|
40 |
|
|
stack = inspect.stack()
|
41 |
ca436714
|
David Friesecký
|
|
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 |
1bdc90c0
|
David Friesecký
|
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 |
ed35ce72
|
David Friesecký
|
app_logger = logging.getLogger(LOG_NAME)
|
54 |
|
|
app_logger.debug(f'{Path(names[0]).name}[{names[1]}{names[2]}()]: {message}')
|
55 |
dbca269d
|
David Friesecký
|
|
56 |
|
|
@staticmethod
|
57 |
1bdc90c0
|
David Friesecký
|
def info(message: str):
|
58 |
|
|
names = Logger.get_names()
|
59 |
ed35ce72
|
David Friesecký
|
app_logger = logging.getLogger(LOG_NAME)
|
60 |
|
|
app_logger.info(f'{Path(names[0]).name}[{names[1]}{names[2]}()]: {message}')
|
61 |
dbca269d
|
David Friesecký
|
|
62 |
|
|
@staticmethod
|
63 |
1bdc90c0
|
David Friesecký
|
def warning(message: str):
|
64 |
|
|
names = Logger.get_names()
|
65 |
ed35ce72
|
David Friesecký
|
app_logger = logging.getLogger(LOG_NAME)
|
66 |
|
|
app_logger.warning(f'{Path(names[0]).name}[{names[1]}{names[2]}()]: {message}')
|
67 |
dbca269d
|
David Friesecký
|
|
68 |
|
|
@staticmethod
|
69 |
1bdc90c0
|
David Friesecký
|
def error(message: str):
|
70 |
|
|
names = Logger.get_names()
|
71 |
ed35ce72
|
David Friesecký
|
app_logger = logging.getLogger(LOG_NAME)
|
72 |
|
|
app_logger.error(f'{Path(names[0]).name}[{names[1]}.{names[2]}()]: {message}')
|
73 |
dbca269d
|
David Friesecký
|
|
74 |
|
|
@staticmethod
|
75 |
1bdc90c0
|
David Friesecký
|
def critical(message: str):
|
76 |
|
|
names = Logger.get_names()
|
77 |
ed35ce72
|
David Friesecký
|
app_logger = logging.getLogger(LOG_NAME)
|
78 |
|
|
app_logger.critical(f'{Path(names[0]).name}[{names[1]}.{names[2]}()]: {message}')
|