-
Notifications
You must be signed in to change notification settings - Fork 3
/
demo_handlers.py
75 lines (64 loc) · 2.36 KB
/
demo_handlers.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
import json
import requests
from logging.handlers import HTTPHandler
from common.message_utils import ExtendedJsonEncoder, format_stack_trace
class PersistentHTTPHandler(HTTPHandler):
def __init__(self, logPath, host, url, method):
HTTPHandler.__init__(self, host, url, method)
self.logPath = logPath
self.s = requests.Session()
def mapLogRecord(self, record):
record_modified = HTTPHandler.mapLogRecord(self, record)
record_modified['logPath'] = self.logPath
try:
record_modified['msg'] = self.format(record)
except:
pass
return record_modified
def emit(self, record):
try:
host = self.host
url = self.url
url = 'http://' + host + '/' + url
data = self.mapLogRecord(record)
self.s.post(url, data=data, timeout=10)
except (KeyboardInterrupt, SystemExit):
raise
except:
self.handleError(record)
class MongoHTTPHandler(HTTPHandler):
def __init__(self, db_host, db_port, db_name, collection, host, url, method):
HTTPHandler.__init__(self, host, url, method)
self.db_host = db_host
self.db_port = db_port
self.db_name = db_name
self.collection = collection
self.s = requests.Session()
def mapLogRecord(self, record):
record_modified = HTTPHandler.mapLogRecord(self, record)
try:
record_modified['msg'] = self.format(record)
except:
pass
record_modified['exc_info'] = format_stack_trace(record_modified['exc_info'])
record_modified['args'] = str(record_modified['args'])
return record_modified
def emit(self, record):
try:
host = self.host
url = self.url
url = 'http://' + host + '/' + url
data = self.mapLogRecord(record)
json_data = {
'db_host': self.db_host,
'db_port': self.db_port,
'db_name': self.db_name,
'collection': self.collection,
'data': data,
}
json_data = json.dumps(json_data, cls=ExtendedJsonEncoder)
self.s.post(url, json=json_data, timeout=10)
except (KeyboardInterrupt, SystemExit):
raise
except:
self.handleError(record)