forked from wujcan/SGL-TensorFlow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.py
80 lines (62 loc) · 2.06 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
67
68
69
70
71
72
73
74
75
76
77
78
79
"""
@author: Zhongchuan Sun
"""
import sys
import os
import logging
from util import Configurator
class Logger(object):
"""`Logger` is a simple encapsulation of python logger.
This class can show a message on standard output and write it into the
file named `filename` simultaneously. This is convenient for observing
and saving training results.
"""
def __init__(self, filename):
"""Initializes a new `Logger` instance.
Args:
filename (str): File name to create. The directory component of this
file will be created automatically if it is not existing.
"""
dir_name = os.path.dirname(filename)
if not os.path.exists(dir_name):
os.makedirs(dir_name)
self.logger = logging.getLogger(filename)
self.logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s.%(msecs)03d: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
# write into file
fh = logging.FileHandler(filename)
fh.setLevel(logging.DEBUG)
fh.setFormatter(formatter)
# show on console
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.DEBUG)
ch.setFormatter(formatter)
# add to Handler
self.logger.addHandler(fh)
self.logger.addHandler(ch)
def _flush(self):
for handler in self.logger.handlers:
handler.flush()
def debug(self, message):
self.logger.debug(message)
self._flush()
def info(self, message):
self.logger.info(message)
self._flush()
def warning(self, message):
self.logger.warning(message)
self._flush()
def error(self, message):
self.logger.error(message)
self._flush()
def critical(self, message):
self.logger.critical(message)
self._flush()
if __name__ == '__main__':
log = Logger('NeuRec_test.log')
log.debug('debug')
log.info('info')
log.warning('warning')
log.error('error')
log.critical('critical')