forked from tebeka/crashlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrashlog.py
76 lines (57 loc) · 1.89 KB
/
crashlog.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
'''Send email and log when there is an uncaught exception in your code
Use in your code:
import crashlog
crashlog.install(emails=['[email protected]'])
'''
__version__ = '0.0.1'
import sys
from smtplib import SMTP
from email.mime.text import MIMEText
from os import environ
from traceback import format_exception
if sys.version_info[0] >= 3:
from io import StringIO
else:
from cStringIO import StringIO
from time import ctime
_EMAILS = []
_LOGFILE = None
_PREV_EXCEPTHOOK = None
def send_email(emails, program, message):
message = MIMEText(message)
message['Subject'] = '{} crashed'.format(program)
crashlog_email = '[email protected]'
message['From'] = 'Crashlog <{}>'.format(crashlog_email)
smtp = SMTP('mailhost.somewhere.com')
smtp.sendmail(crashlog_email, _EMAILS, message.as_string())
def format_message(type, value, traceback):
message = StringIO()
out = lambda m: message.write(u'{}\n'.format(m))
out(ctime())
out('== Traceback ==')
out(''.join(format_exception(type, value, traceback)))
out('\n== Command line ==')
out(' '.join(sys.argv))
out('\n== Environment ==')
for key, value in environ.items():
out('{} = {}'.format(key, value))
return message.getvalue()
def excepthook(type, value, traceback):
try:
if not (_EMAILS or _LOGFILE):
return
message = format_message(type, value, traceback)
if _EMAILS:
send_email(_EMAILS, sys.argv[0], message)
if _LOGFILE:
with open(_LOGFILE, 'at') as fo:
fo.write('{}\n'.format(message))
finally:
if _PREV_EXCEPTHOOK:
_PREV_EXCEPTHOOK(type, value, traceback)
def install(emails=None, logfile=None):
global _EMAILS, _PREV_EXCEPTHOOK, _LOGFILE
_EMAILS = emails or _EMAILS
_LOGFILE = logfile
_PREV_EXCEPTHOOK = sys.excepthook
sys.excepthook = excepthook