-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_support.py
118 lines (114 loc) · 3.58 KB
/
log_support.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
from sanic.log import DefaultFilter
import sys
import logging
LOGGING = {
'version': 1,
'filters': {
'showFilter': {
'()': DefaultFilter,
'param': [0, 10, 20, 30, 40, 50]
},
'accessFilter': {
'()': DefaultFilter,
'param': [20]
},
'errorFilter': {
'()': DefaultFilter,
'param': [30, 40, 50]
}
},
'formatters': {
'simple': {
'format': '%(asctime)s - (%(name)s)[%(levelname)s]: %(message)s',
'datefmt': '%Y-%m-%d %H:%M:%S'
},
'net' : {
'format': '%(asctime)s - (%(name)s)[%(levelname)s][%(host)s]: ' +
'%(request)s %(message)s %(status)d %(byte)d',
'datefmt': '%Y-%m-%d %H:%M:%S'
}
},
'handlers': {
'internal': {
'class': 'logging.StreamHandler',
'filters': ['showFilter'],
'formatter': 'simple',
'stream': sys.stderr
},
'internalnet': {
'class': 'logging.StreamHandler',
'filters': ['showFilter'],
'formatter': 'net',
'stream': sys.stderr
},
'accessTimedRotatingFile': {
'class': 'logging.handlers.TimedRotatingFileHandler',
'filters': ['accessFilter'],
'formatter': 'simple',
'when': 'midnight',
'interval': 1,
'backupCount': 0,
'filename': './log/access.log'
},
'errorTimedRotatingFile': {
'class': 'logging.handlers.TimedRotatingFileHandler',
'filters': ['errorFilter'],
'when': 'midnight',
'interval': 1,
'backupCount': 0,
'filename': './log/error.log',
'formatter': 'simple'
},
'accessnetTimedRotatingFile': {
'class': 'logging.handlers.TimedRotatingFileHandler',
'filters': ['accessFilter'],
'formatter': 'net',
'when': 'midnight',
'interval': 1,
'backupCount': 0,
'filename': './log/access.log'
},
'errornetTimedRotatingFile': {
'class': 'logging.handlers.TimedRotatingFileHandler',
'filters': ['errorFilter'],
'when': 'midnight',
'interval': 1,
'backupCount': 0,
'filename': './log/error.log',
'formatter': 'net'
}
},
'loggers': {
'sanic': {
'level': 'DEBUG',
'handlers': ['accessTimedRotatingFile', 'errorTimedRotatingFile','internal']
},
'network': {
'level': 'DEBUG',
'handlers': ['internalnet','accessnetTimedRotatingFile','errornetTimedRotatingFile']
}
}
}
logging.basicConfig(filename="access.log", filemode="a",
level=logging.INFO, format='%(levelname)s %(asctime)s %(message)s')
ERROR = logging.FileHandler("error.log")
ERROR.setLevel(logging.ERROR)
formatter = logging.Formatter('%(levelname)s %(asctime)s %(message)s')
ERROR.setFormatter(formatter)
logging.getLogger('').addHandler(ERROR)
console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatters = logging.Formatter('%(asctime)s %(message)s')
console.setFormatter(formatters)
logging.getLogger('').addHandler(console)
def do_log(level, info):
if level == 5:
logging.critical(info)
elif level == 4:
logging.error(info)
elif level == 3:
logging.warning(info)
elif level == 2:
logging.info(info)
elif level == 1:
logging.debug(info)