File size: 1,718 Bytes
2f07364
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import logging
import os
import socket
import sys
from logging.handlers import TimedRotatingFileHandler
from pathlib import Path

from src.config import settings

_FORMATTER = logging.Formatter(
    "%(asctime)s — [%(threadName)s] - %(name)s -  %(filename)s.%(funcName)s(%(lineno)d) - %(levelname)s - %(message)s")
LOGS_MODEL_BASE_PATH = Path(os.environ.get('model_logs_base_path', os.getcwd()))
LOG_FILE = f"tts_inference_" + socket.gethostname() + ".log"

_FILE_LOGGER_HANDLER = None
_CONSOLE_LOGGER_HANDLER = None
_LOGGING_DEBUG = settings.log_level.upper()


def get_console_handler():
    global _CONSOLE_LOGGER_HANDLER
    if _CONSOLE_LOGGER_HANDLER is None:
        _CONSOLE_LOGGER_HANDLER = logging.StreamHandler(sys.stdout)
        _CONSOLE_LOGGER_HANDLER.setFormatter(_FORMATTER)

    return _CONSOLE_LOGGER_HANDLER


def get_file_handler():
    global _FILE_LOGGER_HANDLER
    if _FILE_LOGGER_HANDLER is None:
        if not os.path.exists(LOGS_MODEL_BASE_PATH):
            os.makedirs(LOGS_MODEL_BASE_PATH)
        _FILE_LOGGER_HANDLER = TimedRotatingFileHandler(LOGS_MODEL_BASE_PATH / LOG_FILE,
                                                        when='midnight',
                                                        backupCount=30)
        _FILE_LOGGER_HANDLER.setFormatter(_FORMATTER)

    return _FILE_LOGGER_HANDLER


def get_logger(logger_name):
    logger = logging.getLogger(logger_name)
    logger.setLevel(_LOGGING_DEBUG)  # better to have too much log than not enough
    logger.addHandler(get_console_handler())
    logger.addHandler(get_file_handler())
    # with this pattern, it's rarely necessary to propagate the error up to parent
    logger.propagate = False
    return logger