File size: 1,592 Bytes
88b0dcb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
""" 
@Date: 2021/07/17
@description:
"""
import os
import sys
import logging
import functools
from termcolor import colored


def build_logger(config):
    output_dir = config.LOGGER.DIR
    local_rank = config.LOCAL_RANK
    name = config.MODEL.NAME
    logger = get_logger(output_dir, local_rank, name)
    return logger


@functools.lru_cache()
def get_logger(output_dir=None, local_rank=None, name="PLTNet"):
    if output_dir and not os.path.exists(output_dir):
        os.makedirs(output_dir)

    # create logger
    logger = logging.getLogger(name)
    logger.setLevel(logging.DEBUG)
    logger.propagate = False

    # create formatter
    fmt = f'[%(asctime)s %(name)s][%(levelname)1.1s](%(filename)s %(lineno)d): %(message)s'
    color_fmt = colored(f'[%(asctime)s %(name)s][%(levelname)1.1s][{local_rank}]', 'green') + colored(
        f'(%(filename)s %(lineno)d)',
        'yellow') + ': %(message)s'
    if local_rank in [0] or local_rank is None:
        console_handler = logging.StreamHandler(sys.stdout)
        console_handler.setLevel(logging.DEBUG)
        console_handler.setFormatter(
            logging.Formatter(fmt=color_fmt, datefmt='%Y-%m-%d %H:%M:%S'))
        logger.addHandler(console_handler)

    if output_dir is not None:
        # create file handlers
        file_handler = logging.FileHandler(os.path.join(output_dir, f'log_rank{local_rank}.log'), mode='a')
        file_handler.setLevel(logging.DEBUG)
        file_handler.setFormatter(logging.Formatter(fmt=fmt, datefmt='%Y-%m-%d %H:%M:%S'))
        logger.addHandler(file_handler)

    return logger