2013-04-23 18:51:17 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2013-10-31 18:33:19 +01:00
|
|
|
from django.conf import settings
|
|
|
|
|
2012-12-06 22:00:34 +01:00
|
|
|
import logging
|
|
|
|
import traceback
|
2013-01-02 23:25:40 +01:00
|
|
|
import platform
|
2012-12-06 22:00:34 +01:00
|
|
|
|
2013-01-16 20:45:07 +01:00
|
|
|
from django.core import mail
|
|
|
|
from django.utils.log import AdminEmailHandler
|
|
|
|
from django.views.debug import ExceptionReporter, get_exception_reporter_filter
|
2012-12-06 22:00:34 +01:00
|
|
|
|
2013-11-13 19:12:22 +01:00
|
|
|
from zerver.lib.queue import queue_json_publish
|
|
|
|
|
2013-08-06 21:35:33 +02:00
|
|
|
class AdminZulipHandler(logging.Handler):
|
2013-11-13 19:12:22 +01:00
|
|
|
"""An exception log handler that sends the exception to the queue to be
|
|
|
|
sent to the Zulip feedback server.
|
2012-12-06 22:00:34 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
# adapted in part from django/utils/log.py
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
logging.Handler.__init__(self)
|
|
|
|
|
|
|
|
def emit(self, record):
|
|
|
|
try:
|
|
|
|
request = record.request
|
2012-12-19 08:35:10 +01:00
|
|
|
|
|
|
|
filter = get_exception_reporter_filter(request)
|
2013-11-13 19:12:22 +01:00
|
|
|
|
|
|
|
if record.exc_info:
|
|
|
|
stack_trace = ''.join(traceback.format_exception(*record.exc_info))
|
|
|
|
else:
|
|
|
|
stack_trace = None
|
|
|
|
|
|
|
|
try:
|
|
|
|
user_profile = request.user
|
|
|
|
user_full_name = user_profile.full_name
|
|
|
|
user_email = user_profile.email
|
|
|
|
except Exception:
|
|
|
|
traceback.print_exc()
|
|
|
|
# Error was triggered by an anonymous user.
|
|
|
|
user_full_name = None
|
|
|
|
user_email = None
|
|
|
|
|
|
|
|
report = dict(
|
|
|
|
node = platform.node(),
|
|
|
|
method = request.method,
|
|
|
|
path = request.path,
|
|
|
|
data = request.GET if request.method == 'GET'
|
|
|
|
else filter.get_post_parameters(request),
|
|
|
|
remote_addr = request.META.get('REMOTE_ADDR', None),
|
|
|
|
query_string = request.META.get('QUERY_STRING', None),
|
|
|
|
server_name = request.META.get('SERVER_NAME', None),
|
|
|
|
message = record.getMessage(),
|
|
|
|
stack_trace = stack_trace,
|
|
|
|
user_full_name = user_full_name,
|
|
|
|
user_email = user_email,
|
|
|
|
)
|
|
|
|
except:
|
|
|
|
traceback.print_exc()
|
|
|
|
report = dict(
|
|
|
|
node = platform.node(),
|
|
|
|
message = record.getMessage(),
|
|
|
|
)
|
2012-12-06 22:00:34 +01:00
|
|
|
|
2013-01-25 20:15:26 +01:00
|
|
|
try:
|
2015-08-21 09:02:03 +02:00
|
|
|
if settings.ZULIP_COM_STAGING:
|
2013-11-13 19:12:22 +01:00
|
|
|
# On staging, process the report directly so it can happen inside this
|
|
|
|
# try/except to prevent looping
|
|
|
|
from zilencer.error_notify import notify_server_error
|
|
|
|
notify_server_error(report)
|
2015-08-21 09:02:03 +02:00
|
|
|
else:
|
|
|
|
queue_json_publish('error_reports', dict(
|
|
|
|
type = "server",
|
|
|
|
report = report,
|
|
|
|
), lambda x: None)
|
2013-01-25 20:15:26 +01:00
|
|
|
except:
|
|
|
|
# If this breaks, complain loudly but don't pass the traceback up the stream
|
|
|
|
# However, we *don't* want to use logging.exception since that could trigger a loop.
|
|
|
|
logging.warning("Reporting an exception triggered an exception!", exc_info=True)
|