MCPcopy Create free account
hub / github.com/MotrixLab/AiOS / setup_logger

Function setup_logger

util/logger.py:30–88  ·  view source on GitHub ↗

Initialize the detectron2 logger and set its verbosity level to "INFO". Args: output (str): a file name or a directory to save log. If None, will not save log file. If ends with ".txt" or ".log", assumed to be a file name. Otherwise, logs will be saved to `output

(output=None,
                 distributed_rank=0,
                 *,
                 color=True,
                 name='imagenet',
                 abbrev_name=None)

Source from the content-addressed store, hash-verified

28# so that calling setup_logger multiple times won't add many handlers
29@functools.lru_cache()
30def setup_logger(output=None,
31 distributed_rank=0,
32 *,
33 color=True,
34 name='imagenet',
35 abbrev_name=None):
36 """Initialize the detectron2 logger and set its verbosity level to "INFO".
37
38 Args:
39 output (str): a file name or a directory to save log. If None, will not save log file.
40 If ends with ".txt" or ".log", assumed to be a file name.
41 Otherwise, logs will be saved to `output/log.txt`.
42 name (str): the root module name of this logger
43
44 Returns:
45 logging.Logger: a logger
46 """
47 logger = logging.getLogger(name)
48 logger.setLevel(logging.DEBUG)
49 logger.propagate = False
50
51 if abbrev_name is None:
52 abbrev_name = name
53
54 plain_formatter = logging.Formatter(
55 '[%(asctime)s.%(msecs)03d]: %(message)s', datefmt='%m/%d %H:%M:%S')
56 # stdout logging: master only
57 if distributed_rank == 0:
58 ch = logging.StreamHandler(stream=sys.stdout)
59 ch.setLevel(logging.DEBUG)
60 if color:
61 formatter = _ColorfulFormatter(
62 colored('[%(asctime)s.%(msecs)03d]: ', 'green') +
63 '%(message)s',
64 datefmt='%m/%d %H:%M:%S',
65 root_name=name,
66 abbrev_name=str(abbrev_name),
67 )
68 else:
69 formatter = plain_formatter
70 ch.setFormatter(formatter)
71 logger.addHandler(ch)
72
73 # file logging: all workers
74 if output is not None:
75 if output.endswith('.txt') or output.endswith('.log'):
76 filename = output
77 else:
78 filename = os.path.join(output, 'log.txt')
79 if distributed_rank > 0:
80 filename = filename + f'.rank{distributed_rank}'
81 os.makedirs(os.path.dirname(filename), exist_ok=True)
82
83 fh = logging.StreamHandler(_cached_log_stream(filename))
84 fh.setLevel(logging.DEBUG)
85 fh.setFormatter(plain_formatter)
86 logger.addHandler(fh)
87

Callers 1

mainFunction · 0.90

Calls 2

_ColorfulFormatterClass · 0.85
_cached_log_streamFunction · 0.85

Tested by

no test coverage detected