mirror of
https://github.com/zulip/zulip.git
synced 2026-07-12 21:04:41 +08:00
AdminZulipHandler: Extract add_request_metadata.
This commit is contained in:
parent
4276face7f
commit
efa151b488
@ -1,7 +1,7 @@
|
||||
from __future__ import absolute_import
|
||||
|
||||
from django.conf import settings
|
||||
from typing import Optional
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
import logging
|
||||
import traceback
|
||||
@ -14,6 +14,46 @@ from django.views.debug import ExceptionReporter, get_exception_reporter_filter
|
||||
|
||||
from zerver.lib.queue import queue_json_publish
|
||||
|
||||
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()
|
||||
|
||||
class AdminZulipHandler(logging.Handler):
|
||||
"""An exception log handler that sends the exception to the queue to be
|
||||
sent to the Zulip feedback server.
|
||||
@ -26,12 +66,8 @@ class AdminZulipHandler(logging.Handler):
|
||||
logging.Handler.__init__(self)
|
||||
|
||||
def emit(self, record):
|
||||
# type: (ExceptionReporter) -> None
|
||||
# type: (logging.LogRecord) -> None
|
||||
try:
|
||||
request = record.request # type: HttpRequest
|
||||
|
||||
exception_filter = get_exception_reporter_filter(request)
|
||||
|
||||
if record.exc_info:
|
||||
stack_trace = ''.join(traceback.format_exception(*record.exc_info)) # type: Optional[str]
|
||||
else:
|
||||
@ -42,44 +78,7 @@ class AdminZulipHandler(logging.Handler):
|
||||
message = record.getMessage(),
|
||||
stack_trace = stack_trace,
|
||||
)
|
||||
|
||||
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
|
||||
|
||||
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()
|
||||
|
||||
add_request_metadata(report, record.request) # type: ignore # record.request is added dynamically
|
||||
except Exception:
|
||||
traceback.print_exc()
|
||||
report = dict(
|
||||
|
||||
Loading…
Reference in New Issue
Block a user