Kiln » Kiln Storage Service Read More
Clone URL:  
errorloggingmiddleware.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
from datetime import datetime import os import sys import settings from bugzscout import report_exception class ErrorLoggingMiddleware(object): """Report exceptions to a file Only used for licensed Kiln installs, so that people don't worry about what data is getting sent back to Fog Creek.""" def __init__(self, app): self.app = app def __call__(self, environ, start_response): return self.logging_application(environ, start_response) def logging_application(self, environ, start_response): app_iter = None try: app_iter = self.app(environ, start_response) for item in app_iter: yield item if hasattr(app_iter, 'close'): app_iter.close() except Exception, e: if hasattr(app_iter, 'close'): app_iter.close() _report_exception(environ, e) # Once we've reported the exception, reraise so others # (like DebuggingApplication) can handle it. raise def _report_exception(environ, exception): bug = {} bug["Description"] = 'Error (%s IP): %s' % ((environ.get('REMOTE_ADDR', '') in settings.INTERNAL_IPS and 'internal' or 'EXTERNAL'), environ['PATH_INFO']) try: request_repr = repr(environ) except: request_repr = 'Request repr() unavailable' bug["Extra"] = '%s\n\n%s' % (_get_traceback(sys.exc_info()), request_repr) _log_error(bug) if settings.HOSTED: report_exception(exception, extra=request_repr) def _get_traceback(exc_info=None): """Helper function to return the traceback as a string""" import traceback return '\n'.join(traceback.format_exception(*(exc_info or sys.exc_info()))) def _log_error(bug): times = '=' plus = '\n' with open(os.path.join(settings.KILN_REPOSITORY_ROOT, r'errors.txt'), 'a') as f: f.write('%s\n' % datetime.utcnow().isoformat()) f.write(bug['Description']) f.write('\n\n') f.write(bug['Extra']) f.write(plus * 2 + times * 78 + plus * 3)