This repository has been archived by the owner on May 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.py
53 lines (41 loc) · 1.48 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
import datetime
import logging
import time
class formatter():
def __init__(self):
self.begin_time = time.time()
def format(self, rec):
diff = round(rec.created - self.begin_time)
prefix = "{} - {} - {} - {} - {}".format(
rec.levelname,
time.strftime('%x %X'),
datetime.timedelta(seconds=diff),
rec.funcName,
rec.lineno
)
msg = rec.getMessage()
msg = msg.replace('\n', '\n' + ' ' * (len(prefix) + 3))
return "%s - %s" % (prefix, msg)
def create_logger(log_address, verbose=2):
log_formatter = formatter()
# create file handler and set level to debug
file_handler = logging.FileHandler(log_address, "a")
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(log_formatter)
# create console handler and set level to info
log_level = logging.DEBUG if verbose == 2 else logging.INFO if verbose == 1 else logging.WARNING
console_handler = logging.StreamHandler()
console_handler.setLevel(log_level)
console_handler.setFormatter(log_formatter)
# create logger and set level to debug
logger = logging.getLogger()
logger.handlers = []
logger.setLevel(logging.DEBUG)
logger.propagate = False
logger.addHandler(file_handler)
logger.addHandler(console_handler)
# reset logger elapsed time
def reset_time():
log_formatter.begin_time = time.time()
logger.reset_time = reset_time
return logger