-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogger.py
73 lines (57 loc) · 1.53 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
71
72
73
# Python
import logging
import logging.handlers
import sys
# 3rd Party
# Self
DEBUG_ALL = 5
class ThirdPartyFilter(logging.Filter):
def __init__(self, names):
logging.Filter.__init__(self)
self.names = names
def filter(self, record):
if (record.name in self.names and record.levelno == logging.DEBUG):
return False
return True
def create_console_handler():
h = logging.StreamHandler(stream=sys.stdout)
h.setFormatter(logging.Formatter(
fmt='%(asctime)s> %(message)s',
datefmt='%Y/%m/%d %H:%M:%S'
))
h.setLevel(logging.INFO)
'''
h.addFilter(ThirdPartyFilter([
'prawcore',
'urllib3.connectionpool'
]))
'''
logging.getLogger().addHandler(h)
def create_log_handler():
h = logging.handlers.TimedRotatingFileHandler('logs/log', when='midnight', backupCount=30)
h.setFormatter(format)
h.setLevel(logging.INFO)
logging.getLogger().addHandler(h)
def create_debug_handler():
h = logging.handlers.TimedRotatingFileHandler('logs/debug', when='h', backupCount=48)
h.setFormatter(format)
h.setLevel(logging.DEBUG)
h.addFilter(ThirdPartyFilter([
'prawcore',
'urllib3.connectionpool'
]))
logging.getLogger().addHandler(h)
def init_logging():
log = logging.getLogger()
log.setLevel(logging.DEBUG)
# Remove the default handler
log.removeHandler(log.handlers[0])
#print(len(log.handlers))
global format
format = logging.Formatter(
fmt='%(asctime)s %(levelname)s %(filename)s:%(lineno)d> %(message)s',
datefmt='%Y/%m/%d %H:%M:%S'
)
create_console_handler()
create_log_handler()
create_debug_handler()