-
Notifications
You must be signed in to change notification settings - Fork 4
/
logger.py
43 lines (36 loc) · 1.05 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
import os
from os.path import dirname
from datetime import datetime
class Logger:
"""
Logger class to record training log
"""
def __init__(self, file_path, verbose=False):
self.verbose = verbose
self.create_dir(file_path)
self.logger = open(file_path, 'a+')
def create_dir(self, file_path):
dir = dirname(file_path)
os.makedirs(dir, exist_ok=True)
def __call__(self, *args, prefix='', timestamp=True):
if timestamp:
now = datetime.now()
now = now.strftime("%Y/%m/%d, %H:%M:%S - ")
else:
now = ''
if prefix == '':
info = prefix + now
else:
info = prefix + ' ' + now
for msg in args:
if not isinstance(msg, str):
msg = str(msg)
info += msg + '\n'
self.logger.write(info)
if self.verbose:
print(info, end='')
self.logger.flush()
def __del__(self):
self.logger.close()
def close(self):
self.logger.close()