-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.py
57 lines (46 loc) · 1.44 KB
/
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
51
52
53
54
55
56
import datetime
class Log:
class Level:
DEBUG = 0
INFO = 1
WARN = 2
ERROR = 3
def str(level):
return {
Log.Level.DEBUG: 'DEBUG',
Log.Level.INFO: 'INFO',
Log.Level.WARN: 'WARN',
Log.Level.ERROR: 'ERROR',
}.get(level, 'ERROR')
import io
def __init__(self, level=Level.DEBUG, output=io.StringIO()):
self.level = level
self.output = output
def read(self):
logs = ''
if self.output:
self.output.seek(0)
logs = self.output.read()
self.output.flush()
return logs
def log(self, level, *args):
form_msg = ''
if self.level <= level:
form_msg = '{time} 【{level}】: {msg}'.format(
time=datetime.datetime.now().strftime(
'%Y-%m-%d %H:%M:%S'),
level=Log.Level.str(level),
msg=' '.join([str(arg) for arg in args])
)
print(form_msg)
if self.output:
self.output.write(form_msg+'\n')
return form_msg
def err(self, *args):
return self.log(Log.Level.ERROR, *args)
def warn(self, *args):
return self.log(Log.Level.WARN, *args)
def info(self, *args):
return self.log(Log.Level.INFO, *args)
def debug(self, *args):
return self.log(Log.Level.DEBUG, *args)