-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.py
70 lines (55 loc) · 2.33 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
import logging
import logging.config
import logging.handlers
from path import Path
import multiprocessing_logging
multiprocessing_logging.install_mp_handler()
import sys
class ColorPrint:
@staticmethod
def print_fail(message, end = '\n'):
sys.stderr.write('\x1b[1;31m' + message.strip() + '\x1b[0m' + end)
@staticmethod
def print_pass(message, end = '\n'):
sys.stdout.write('\x1b[1;32m' + message.strip() + '\x1b[0m' + end)
@staticmethod
def print_warn(message, end = '\n'):
sys.stderr.write('\x1b[1;33m' + message.strip() + '\x1b[0m' + end)
@staticmethod
def print_info(message, end = '\n'):
sys.stdout.write('\x1b[1;34m' + message.strip() + '\x1b[0m' + end)
@staticmethod
def print_bold(message, end = '\n'):
sys.stdout.write('\x1b[1;37m' + message.strip() + '\x1b[0m' + end)
class Logger():
def __init__(self, level, log_dir, log_name, filename):
self.logger = logging.getLogger(log_name)
# logging to file
# fh = logging.handlers.RotatingFileHandler(
# Path(log_dir) / 'main_logger_{:%H%M%S}.log'.format(datetime.now()),
# 'w', 20 * 1024 * 1024, 5)
fh = logging.handlers.RotatingFileHandler(
Path(log_dir) / filename, 'w', 20 * 1024 * 1024, 5)
formatter = logging.Formatter('%(asctime)s %(levelname)5s - %(name)s '
'[%(filename)s line %(lineno)d] - %(message)s',
datefmt='%m-%d %H:%M:%S')
fh.setFormatter(formatter)
self.logger.addHandler(fh)
# logging to screen
fh = logging.StreamHandler()
formatter = logging.Formatter('[%(levelname)s] %(message)s', )
fh.setFormatter(formatter)
self.logger.addHandler(fh)
self.logger.setLevel(level)
self.logger.info("Start training")
def info(self, id, input):
id_str = " [" + str(id) + "] "
self.logger.info(id_str + input)
def warning(self, id, input):
id_str = " [" + str(id) + "] "
self.logger.warning(id_str + input)
def error(self, id, input):
id_str = " [" + str(id) + "] "
self.logger.error(id_str + input)
def init_logger(level='INFO', log_dir='./', log_name='main_logger', filename='main.log'):
return Logger(level, log_dir, log_name, filename)