File size: 1,948 Bytes
ecca75f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
51
52
53
54
55
56
57
58
59
60
61
# coding=utf-8
from logging.handlers import TimedRotatingFileHandler

import os
import sys
import logging


class LoggerFactory:

    @staticmethod
    def create_logger(name=None, level=logging.INFO):
        """create a logger

        Args:
            name (str): name of the logger
            level: level of logger

        Raises:
            ValueError is name is None
        """

        if name is None:
            raise ValueError("name for logger cannot be None")

        formatter = logging.Formatter("[%(asctime)s] [%(levelname)s] "
                                      "[%(filename)s:%(lineno)d:%(funcName)s] %(message)s")

        logger_ = logging.getLogger(name)
        logger_.setLevel(level)
        logger_.propagate = False
        ch = logging.StreamHandler(stream=sys.stdout)
        ch.setLevel(level)
        ch.setFormatter(formatter)
        logger_.addHandler(ch)
        return logger_

    @staticmethod
    def create_logger_with_file(log_file_path: str = None, logger_level=logging.INFO):
        logger_inner = logging.getLogger()
        logger_inner.setLevel(logger_level)
        logger_inner.propagate = True

        formatter = logging.Formatter(fmt="[%(asctime)s] [%(filename)s:%(lineno)s - %(levelname)s] %(message)s",
                                      datefmt="%Y-%m-%d %H:%M:%S")

        # TimedRotatingFileHandler
        if log_file_path:
            basedir = os.path.dirname(log_file_path)
            if not os.path.isdir(basedir):
                os.makedirs(basedir, exist_ok=True)
            handler_file = TimedRotatingFileHandler(log_file_path, when="d", interval=1, backupCount=30)
            handler_file.setFormatter(formatter)
            logger_inner.addHandler(handler_file)

        # StreamHandler
        handler_console = logging.StreamHandler()
        handler_console.setFormatter(formatter)
        logger_inner.addHandler(handler_console)
        return logger_inner