File size: 789 Bytes
0ffeb19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import logging
import sys
import colorlog


formatter = colorlog.ColoredFormatter(
    "%(asctime)s [%(blue)s%(name)s:%(lineno)s%(reset)s] [%(log_color)s%(levelname)s%(reset)s] >>>> %(message)s",
    log_colors={ # 'DEBUG': cyan',
        'INFO': 'green',
        'WARNING': 'yellow',
        'ERROR': 'red',
        'CRITICAL': 'red,bg_white',
    }
)
stream_handler = colorlog.StreamHandler(stream=sys.stdout)
stream_handler.setFormatter(formatter)


def setup_logger(name, level=logging.DEBUG):
    """Setup a logger with the given name and level.

    :param name: The name of the logger.
    :param level: The level of the logger.
    :return: The logger.
    """
    logger = logging.getLogger(name)
    logger.setLevel(level)
    logger.addHandler(stream_handler)
    return logger