姜戈和赫罗库 |如何详细记录400错误?



我已经在Heroku上创建了一个应用程序,然后将Django应用推向它。

i使用heroku logs --tail监视日志以实时查看它们。

如何在这样的生产环境(当DEBUG=false时)显示400个错误登录?

我知道debug.technical_500_response(request, *exc_info)方法,并通过覆盖Handler500来记录400个错误 (例如handler500=my_custom_exception_logger.server_error)。

,但似乎没有technical_400_response方法。

我正在使用

Django 1.11.5
Python 3.6.1
whitenoise 3.3.0

procfile

web: gunicorn --env DJANGO_SETTINGS_MODULE=myapp.settings myapp.wsgi --log-file -

settings.py

LOGGING = {
    'version': 1,   
    'formatters': { 
        'all': {    
            'format': 't'.join([
                "[%(levelname)s]",
                "asctime:%(asctime)s",
                "module:%(module)s",
                "message:%(message)s",
                "process:%(process)d",
                "thread:%(thread)d",
            ])
        },
    },
    'handlers': {  
        'file': {  
            'level': 'DEBUG',  
            'class': 'logging.FileHandler',  
            'filename': os.path.join(BASE_DIR, 'django.log'),  
            'formatter': 'all',  
        },
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'all'
        },
    },
    'loggers': { 
        'command': {  
            'handlers': ['file', 'console'],  
            'level': 'DEBUG',
        },
    },
}

预先感谢。

在这种情况下,调试400错误的最佳方法是使用Chrome Developer Console。

django,API视图

if item.is_valid():
    // do some thing...
else:
    return Response(item.errors, status=status.HTTP_400_BAD_REQUEST)

客户端,ajax

var xmlHttpRequest = new XMLHttpRequest();
  xmlHttpRequest.onreadystatechange = function()
  {
    if( this.readyState == 4 && this.status == 200 )
    {
      if( this.response )
      {
        var res = this.response;
        console.log(this.response);
        // do something...
      }
    }
  }
  ...

然后对我的django服务器测试了POST请求,ResponseText告诉我为什么失败了。

最新更新