2012-02-15 27 views
5

Cách easist bao gồm tên người dùng, tên và họ e-amil trong lỗi django Traceback là gì.Bao gồm người dùng đăng nhập django trong lỗi django Traceback

Tôi biết rằng đường là create a custom error report:

  1. Tạo một lớp mới mà innherit từ django.views.debug.SafeExceptionReporterFilter
  2. Set DEFAULT_EXCEPTION_REPORTER_FILTER

Nhưng, những gì phương pháp một nên ghi đè để nhận được traceback cùng với thông tin này?

Tôi muốn rằng cái nhìn treceback tôi thích:

Traceback (most recent call last): 

File "/usr...o/core/handlers/base.py", line 89, in get_response 
    response = middleware_method(request) 

File "/.../g...ap/utils/middleware.py", line 23,... 
    if elapsedTime.min > 15: 

TypeError: can't compare datetime.timedelta to int 

Logged user information: 
User: pepito 
name: Pepito Grillo 
e-mail: [email protected] 
+0

Đây là một cách sạch hơn: http://stackoverflow.com/a/4946443/565259 – Tobu

+0

@tobu, thanks a lot. – danihp

Trả lời

7

tôi đã làm nó sử dụng Tuỳ chỉnh Middleware. Tôi không chắc đây là câu trả lời hay nhất, nhưng đó là cách tôi giải quyết nó cho dự án của tôi.

settings.py:

MIDDLEWARE_CLASSES = (
    ... 
    'utilities.custom_middleware.CustomMiddleware', 
    ... 
) 

tiện ích/custom_middleware.py:

from utilities.request import AddRequestDetails 

class CustomMiddleware(object): 
""" 
    Adds user details to request context during request processing, so that they 
    show up in the error emails. Add to settings.MIDDLEWARE_CLASSES and keep it 
    outermost(i.e. on top if possible). This allows it to catch exceptions in 
    other middlewares as well. 
""" 

    def process_exception(self, request, exception): 
     """ 
     Process the request to add some variables to it. 
     """ 

     # Add other details about the user to the META CGI variables. 
     try: 
      if request.user.is_authenticated(): 
       AddRequestDetails(request) 
       request.META['AUTH_VIEW_ARGS'] = str(view_args) 
       request.META['AUTH_VIEW_CALL'] = str(view_func) 
       request.META['AUTH_VIEW_KWARGS'] = str(view_kwargs) 
     except: 
      pass 

tiện ích/request.py:

def AddRequestDetails(request): 
""" 
    Adds details about the user to the request, so any traceback will include the 
    details. Good for troubleshooting; this will be included in the email sent to admins 
    on error. 
""" 
if request.user.is_anonymous(): 
    request.META['AUTH_NAME'] = "Anonymous User" 
    request.META['AUTH_USER'] = "Anonymous User" 
    request.META['AUTH_USER_EMAIL'] = "" 
    request.META['AUTH_USER_ID'] = 0 
    request.META['AUTH_USER_IS_ACTIVE'] = False 
    request.META['AUTH_USER_IS_SUPERUSER'] = False 
    request.META['AUTH_USER_IS_STAFF'] = False 
    request.META['AUTH_USER_LAST_LOGIN'] = "" 
else: 
    request.META['AUTH_NAME'] = str(request.user.first_name) + " " + str(request.user.last_name) 
    request.META['AUTH_USER'] = str(request.user.username) 
    request.META['AUTH_USER_EMAIL'] = str(request.user.email) 
    request.META['AUTH_USER_ID'] = str(request.user.id) 
    request.META['AUTH_USER_IS_ACTIVE'] = str(request.user.is_active) 
    request.META['AUTH_USER_IS_SUPERUSER'] = str(request.user.is_superuser) 
    request.META['AUTH_USER_IS_STAFF'] = str(request.user.is_staff) 
    request.META['AUTH_USER_LAST_LOGIN'] = str(request.user.last_login) 
+0

Tôi sẽ kiểm tra. Tôi sẽ quay lại với tin tức. – danihp

+0

Tôi đang đọc lại bài đăng của bạn. Tôi đang tìm một cách để thêm thông tin meta chỉ trong trường hợp ngoại lệ, không phải cho tất cả các yêu cầu. Để tránh chi phí. Bạn nghĩ gì về điều này? – danihp

+0

Trong lớp CustomMiddleware, bạn có thể ghi đè lên process_exception và sử dụng cùng mã như trên. Chỉ cần thay đổi 'def process_view (...' để 'def process_exception (...'. Tôi đã không thử nghiệm này, nhưng tôi nghĩ rằng nó sẽ làm việc. Nhiều hơn về middleware tùy chỉnh ở đây: https://docs.djangoproject.com/en/dev/topics/http/middleware/ – Furbeenator

8

giải pháp tầm thường của tôi (tác phẩm trong django 1.5)

settings.py:

MIDDLEWARE_CLASSES = (
    ... 
    'utilities.custom_middleware.UserTracebackMiddleware', 
    ... 
) 

custom_middleware.py:

class UserTracebackMiddleware(object): 
    """ 
    Adds user to request context during request processing, so that they 
    show up in the error emails. 
    """ 
    def process_exception(self, request, exception): 
     if request.user.is_authenticated(): 
      request.META['AUTH_USER'] = unicode(request.user.username) 
     else: 
      request.META['AUTH_USER'] = "Anonymous User" 

hy vọng nó sẽ giúp

+0

Tại sao chuyển đổi sang unicode? –

Các vấn đề liên quan