1
# Copyright 2011 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
9
from django.conf import settings
10
from django.db import connection
11
from django.views import debug
13
debug.HIDDEN_SETTINGS = re.compile(
14
'SECRET|PASSWORD|PROFANITIES_LIST|PRIVATE|secret|password|private')
17
def _sanitize_dict(dirty):
19
for key in dirty.iterkeys():
20
if debug.HIDDEN_SETTINGS.search(key):
21
clean[key] = '********'
24
def _sanitize_request(request):
25
"""Remove sensitive information from the request before it is
27
request.GET = _sanitize_dict(request.GET)
28
request.POST = _sanitize_dict(request.POST)
31
def _sanitize_vars(items):
34
if debug.HIDDEN_SETTINGS.search(item[0]):
35
sanitized.append((item[0], '********'))
37
sanitized.append(item)
41
class _ExceptionReporter(debug.ExceptionReporter):
43
# This function was taken from the django project.
44
# Please see the license file in the third-party/django directory.
45
def get_traceback_frames(self):
47
tb = sys.exc_info()[2]
49
# support for __traceback_hide__ which is used by a few libraries
50
# to hide internal frames.
51
if tb.tb_frame.f_locals.get('__traceback_hide__'):
54
filename = tb.tb_frame.f_code.co_filename
55
function = tb.tb_frame.f_code.co_name
56
lineno = tb.tb_lineno - 1
57
loader = tb.tb_frame.f_globals.get('__loader__')
58
module_name = tb.tb_frame.f_globals.get('__name__')
59
pre_context_lineno, pre_context, context_line, post_context = \
60
self._get_lines_from_file(filename, lineno, 7, loader,
62
if pre_context_lineno is not None:
68
'vars': _sanitize_vars(tb.tb_frame.f_locals.items()),
70
'pre_context': pre_context,
71
'context_line': context_line,
72
'post_context': post_context,
73
'pre_context_lineno': pre_context_lineno + 1,
79
'filename': '<unknown>',
82
'context_line': '???',
88
def log_request(request):
89
"""Log the request, so that it can be kept in the oops file"""
90
request = _sanitize_request(request)
91
reporter = _ExceptionReporter(request, *sys.exc_info())
92
template_debug = settings.TEMPLATE_DEBUG
93
settings.TEMPLATE_DEBUG = True
95
logging.exception('Unhandled exception in application')
96
logging.warn(reporter.get_traceback_html())
97
logging.warn(traceback.format_exc())
98
for query in connection.queries:
99
logging.warn("time: %(time)s sql: %(sql)s" % query)
101
settings.TEMPLATE_DEBUG = template_debug