-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_log.py
50 lines (37 loc) · 1.5 KB
/
simple_log.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
import time
__LEVELS = ['ERROR', 'WARN', 'INFO', 'DEBUG']
__RED = '\033[31m'
__DEFAULT = '\033[38m'
__YELLOW = '\033[33m'
__GREEN = '\033[32m'
__COLOR_CODE = {'ERROR': __RED, 'INFO': __DEFAULT, 'WARN':__YELLOW,'DEBUG':__GREEN}
class Log:
def __init__(self,):
self.min_level = 'DEBUG'
def set_min_level(level: str):
if level not in __LEVELS:
raise ValueError('{level} is not supported as log levels: {__LEVELS}!')
self.min_level = level
def console_log(self, log_level, *args):
if __LEVELS.index(log_level) > __LEVELS.index(self.min_level):
return
def zfill(src:str, width: int):
src = str(src)
distance = width-len(src)
if distance>0:
src = '0'* distance + src
return src
current = time.localtime()
content = ''.join([str(item) for item in args])
log_line=f'[{zfill(current[0],4)}-{zfill(current[1],2)}-{zfill(current[2],2)} {zfill(current[3],2)}:{zfill(current[4],2)}:{zfill(current[5],2)}] [{log_level}] - {content}'
log_line = __COLOR_CODE.get(log_level, __DEFAULT)+log_line+"\033[0m"
print(log_line)
def info(self, *args):
self.console_log('INFO', *args)
def error(self, *args):
self.console_log('ERROR', *args)
def debug(self, *args):
self.console_log('DEBUG', *args)
def warn(self, *args):
self.console_log('WARN', *args)
log = Log()