forked from Holmes-Alan/ABPN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_logger.py
65 lines (50 loc) · 1.51 KB
/
utils_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
import os
import sys
import datetime
import logging
'''
modified by Kai Zhang (github: https://github.com/cszn)
03/03/2019
https://github.com/xinntao/BasicSR
'''
def log(*args, **kwargs):
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S:"), *args, **kwargs)
'''
# ===============================
# logger
# logger_name = None = 'base' ???
# ===============================
'''
def logger_info(logger_name, log_path='default_logger.log'):
''' set up logger
modified by Kai Zhang (github: https://github.com/cszn)
'''
log = logging.getLogger(logger_name)
if log.hasHandlers():
print('LogHandlers exist!')
else:
print('LogHandlers setup!')
level = logging.INFO
formatter = logging.Formatter('%(asctime)s.%(msecs)03d : %(message)s', datefmt='%y-%m-%d %H:%M:%S')
fh = logging.FileHandler(log_path, mode='a')
fh.setFormatter(formatter)
log.setLevel(level)
log.addHandler(fh)
# print(len(log.handlers))
sh = logging.StreamHandler()
sh.setFormatter(formatter)
log.addHandler(sh)
'''
# ===============================
# print to file and std_out simultaneously
# ===============================
'''
class logger_print(object):
def __init__(self, log_path="default.log"):
self.terminal = sys.stdout
self.log = open(log_path, 'a')
def write(self, message):
self.terminal.write(message)
self.log.write(message) # write the message
def flush(self):
pass