/
bugLogClient.py
executable file
·90 lines (72 loc) · 2.95 KB
/
bugLogClient.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
"""BugLogClient.py
This is a simple client class to facilitate sending bug
reports and other messages to a bugLog server
"""
import httplib, urllib
import traceback
import sys
from socket import gethostname
from os import environ
import logging
from urlparse import urlparse
class BugLogClient:
INFO = "INFO"
ERROR = "ERROR"
FATAL = "FATAL"
def __init__(self, listener, appName):
# parse the listener URL into subcomponents
parts = urlparse(listener)
self.listener = listener
self.listenerHost = parts[1]
self.listenerPath = parts[2]
self.listenerSecure = (parts[0] == "https")
self.appName = appName
try:
self.hostName = gethostname()
except:
self.hostName = "unknown"
logging.info("BugLogClient initialized. I will be talking to the buglog listener at:" + self.listenerHost)
def notifyMessage(self, msg, severity = INFO, extra = None):
stack = traceback.format_stack()
htmlReport = self._stackToHTML(stack)
if extra:
htmlReport += extra
self._notify(msg,severity,htmlReport)
def notifyException(self, severity = ERROR, extra = None):
exc_type, exc_value, exc_tb = sys.exc_info()
try:
msg = str(exc_type) + ": " + str(exc_value)
except:
msg = "error"
stack = traceback.format_exception(exc_type, exc_value, exc_tb)
htmlReport = self._stackToHTML(stack)
if extra:
htmlReport += extra
self._notify(msg,severity,htmlReport)
def _notify(self,msg,severity,htmlReport):
userAgent = environ.get("HTTP_USER_AGENT", "")
headers = {"Content-type": "application/x-www-form-urlencoded",
"Accept": "text/plain"}
params = urllib.urlencode({
"message" : msg,
"severityCode" : severity,
"applicationCode" : self.appName,
"hostName" : self.hostName,
"userAgent" : userAgent,
"exceptionMessage" : msg,
"HTMLReport" : htmlReport
})
endPoint = self.listenerHost
if self.listenerSecure:
conn = httplib.HTTPSConnection(endPoint)
else:
conn = httplib.HTTPConnection(endPoint)
conn.request("POST", self.listenerPath, params, headers)
response = conn.getresponse()
logging.info("BugLog Response: " + str(response.status) + " " + str(response.reason))
conn.close()
def _stackToHTML(self, stack):
html = ""
for x in stack:
html += x + "<br />"
return html