2017-11-16 19:51:44 +01:00
|
|
|
# System documented in https://zulip.readthedocs.io/en/latest/subsystems/logging.html
|
2013-04-23 18:51:17 +02:00
|
|
|
|
2012-12-06 22:00:34 +01:00
|
|
|
import logging
|
2013-01-02 23:25:40 +01:00
|
|
|
import platform
|
2017-11-16 00:50:28 +01:00
|
|
|
import traceback
|
|
|
|
from typing import Any, Dict, Optional
|
2012-12-06 22:00:34 +01:00
|
|
|
|
2017-11-16 00:50:28 +01:00
|
|
|
from django.conf import settings
|
2013-01-16 20:45:07 +01:00
|
|
|
from django.core import mail
|
2016-06-05 07:51:18 +02:00
|
|
|
from django.http import HttpRequest
|
2013-01-16 20:45:07 +01:00
|
|
|
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
|
|
|
|
|
2017-03-26 06:53:10 +02:00
|
|
|
def add_request_metadata(report, request):
|
|
|
|
# type: (Dict[str, Any], HttpRequest) -> None
|
|
|
|
report['path'] = request.path
|
|
|
|
report['method'] = request.method
|
|
|
|
report['remote_addr'] = request.META.get('REMOTE_ADDR', None),
|
|
|
|
report['query_string'] = request.META.get('QUERY_STRING', None),
|
|
|
|
report['server_name'] = request.META.get('SERVER_NAME', None),
|
|
|
|
try:
|
|
|
|
from django.contrib.auth.models import AnonymousUser
|
|
|
|
user_profile = request.user
|
|
|
|
if isinstance(user_profile, AnonymousUser):
|
|
|
|
user_full_name = None
|
|
|
|
user_email = None
|
|
|
|
else:
|
|
|
|
user_full_name = user_profile.full_name
|
|
|
|
user_email = user_profile.email
|
|
|
|
except Exception:
|
|
|
|
# Unexpected exceptions here should be handled gracefully
|
|
|
|
traceback.print_exc()
|
|
|
|
user_full_name = None
|
|
|
|
user_email = None
|
|
|
|
report['user_email'] = user_email
|
|
|
|
report['user_full_name'] = user_full_name
|
|
|
|
|
|
|
|
exception_filter = get_exception_reporter_filter(request)
|
|
|
|
try:
|
|
|
|
report['data'] = request.GET if request.method == 'GET' else \
|
|
|
|
exception_filter.get_post_parameters(request)
|
|
|
|
except Exception:
|
|
|
|
# exception_filter.get_post_parameters will throw
|
|
|
|
# RequestDataTooBig if there's a really big file uploaded
|
|
|
|
report['data'] = {}
|
|
|
|
|
|
|
|
try:
|
|
|
|
report['host'] = request.get_host().split(':')[0]
|
|
|
|
except Exception:
|
|
|
|
# request.get_host() will throw a DisallowedHost
|
|
|
|
# exception if the host is invalid
|
|
|
|
report['host'] = platform.node()
|
|
|
|
|
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):
|
2016-06-05 07:51:18 +02:00
|
|
|
# type: () -> None
|
2012-12-06 22:00:34 +01:00
|
|
|
logging.Handler.__init__(self)
|
|
|
|
|
|
|
|
def emit(self, record):
|
2017-03-26 06:53:10 +02:00
|
|
|
# type: (logging.LogRecord) -> None
|
2012-12-06 22:00:34 +01:00
|
|
|
try:
|
2013-11-13 19:12:22 +01:00
|
|
|
if record.exc_info:
|
2017-02-11 05:26:24 +01:00
|
|
|
stack_trace = ''.join(traceback.format_exception(*record.exc_info)) # type: Optional[str]
|
2017-03-26 07:45:10 +02:00
|
|
|
message = str(record.exc_info[1])
|
2013-11-13 19:12:22 +01:00
|
|
|
else:
|
|
|
|
stack_trace = None
|
2017-03-26 07:45:10 +02:00
|
|
|
message = record.getMessage()
|
2017-04-26 03:52:12 +02:00
|
|
|
if '\n' in message:
|
|
|
|
# Some exception code paths in queue processors
|
|
|
|
# seem to result in super-long messages
|
|
|
|
stack_trace = message
|
|
|
|
message = message.split('\n')[0]
|
2013-11-13 19:12:22 +01:00
|
|
|
|
2017-03-26 06:51:26 +02:00
|
|
|
report = dict(
|
|
|
|
node = platform.node(),
|
2017-04-25 22:46:11 +02:00
|
|
|
host = platform.node(),
|
2017-03-26 07:45:10 +02:00
|
|
|
message = message,
|
2017-03-26 06:51:26 +02:00
|
|
|
stack_trace = stack_trace,
|
|
|
|
)
|
2017-03-26 06:54:56 +02:00
|
|
|
if hasattr(record, "request"):
|
|
|
|
add_request_metadata(report, record.request) # type: ignore # record.request is added dynamically
|
2017-01-08 16:36:19 +01:00
|
|
|
except Exception:
|
2013-11-13 19:12:22 +01:00
|
|
|
traceback.print_exc()
|
|
|
|
report = dict(
|
|
|
|
node = platform.node(),
|
2017-01-30 04:56:50 +01:00
|
|
|
host = platform.node(),
|
2013-11-13 19:12:22 +01:00
|
|
|
message = record.getMessage(),
|
2017-03-26 07:00:08 +02:00
|
|
|
stack_trace = "See /var/log/zulip/errors.log",
|
2013-11-13 19:12:22 +01:00
|
|
|
)
|
2012-12-06 22:00:34 +01:00
|
|
|
|
2013-01-25 20:15:26 +01:00
|
|
|
try:
|
2016-07-19 06:41:55 +02:00
|
|
|
if settings.STAGING_ERROR_NOTIFICATIONS:
|
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
|
2017-03-26 07:00:08 +02:00
|
|
|
from zerver.lib.error_notify import notify_server_error
|
2013-11-13 19:12:22 +01:00
|
|
|
notify_server_error(report)
|
2015-08-21 09:02:03 +02:00
|
|
|
else:
|
|
|
|
queue_json_publish('error_reports', dict(
|
|
|
|
type = "server",
|
|
|
|
report = report,
|
2017-10-19 15:02:03 +02:00
|
|
|
), lambda x: None, call_consume_in_tests=True)
|
2017-01-08 16:36:19 +01:00
|
|
|
except Exception:
|
2013-01-25 20:15:26 +01:00
|
|
|
# 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)
|