-
Notifications
You must be signed in to change notification settings - Fork 8
/
logger.py
executable file
·66 lines (50 loc) · 2.11 KB
/
logger.py
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
62
63
64
65
66
import logging
class FileLogger:
def __init__(self, is_master=False, is_rank0=False, output_dir=None):
# only call by master
# checked outside the class
self.output_dir = output_dir
if is_rank0:
self.logger_name = 'imagenet_training'
self.logger = self.get_logger(output_dir, log_to_file=is_master)
else:
self.logger_name = None
self.logger = NoOp()
def get_logger(self, output_dir, log_to_file):
logger = logging.getLogger(self.logger_name)
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(message)s')
if output_dir and log_to_file:
vlog = logging.FileHandler(output_dir+'/verbose.log')
vlog.setLevel(logging.INFO)
vlog.setFormatter(formatter)
logger.addHandler(vlog)
eventlog = logging.FileHandler(output_dir+'/event.log')
eventlog.setLevel(logging.WARN)
eventlog.setFormatter(formatter)
logger.addHandler(eventlog)
time_formatter = logging.Formatter('%(asctime)s - %(filename)s:%(lineno)d - %(message)s')
debuglog = logging.FileHandler(output_dir+'/debug.log')
debuglog.setLevel(logging.DEBUG)
debuglog.setFormatter(time_formatter)
logger.addHandler(debuglog)
console = logging.StreamHandler()
console.setFormatter(formatter)
console.setLevel(logging.DEBUG)
logger.addHandler(console)
# Reference: https://stackoverflow.com/questions/21127360/python-2-7-log-displayed-twice-when-logging-module-is-used-in-two-python-scri
logger.propagate = False
return logger
def console(self, *args):
self.logger.debug(*args)
def event(self, *args):
self.logger.warn(*args)
def verbose(self, *args):
self.logger.info(*args)
def info(self, *args):
self.logger.info(*args)
# no_op method/object that accept every signature
class NoOp:
def __getattr__(self, *args):
def no_op(*args, **kwargs): pass
return no_op