关于python:如何在django站点上记录服务器错误

How do you log server errors on django sites

因此,在使用开发时,我可以将settings.DEBUG设置为True,如果发生错误,我可以看到它格式良好,具有良好的堆栈跟踪和请求信息。

但是在生产网站上,我宁愿使用DEBUG=False并向访问者显示一些标准错误500页,其中包含我正在修复此错误的信息;)
同时我想有办法将所有这些信息(堆栈跟踪和请求信息)记录到我服务器上的文件中 - 所以我可以将它输出到我的控制台并观察错误滚动,将日志通过电子邮件发送给我 每小时或类似的事情。

您会为django站点推荐哪些日志记录解决方案,以满足这些简单的要求? 我将应用程序作为fcgi服务器运行,我使用apache web服务器作为前端(虽然考虑转到lighttpd)。


好吧,当DEBUG = False时,Django会自动将任何错误的完整回溯邮寄给ADMINS设置中列出的每个人,这样可以免费获得通知。如果您想要更精细的控件,可以编写一个中间件类并将其添加到您的设置中,该类定义了一个名为process_exception()的方法,该方法可以访问引发的异常:

http://docs.djangoproject.com/en/dev/topics/http/middleware/#process-exception

然后,您的process_exception()方法可以执行您喜欢的任何类型的日志记录:写入控制台,写入文件等,等等。

编辑:虽然它没那么有用,但您也可以监听got_request_exception信号,只要在请求处理过程中遇到异常,就会发送该信号:

http://docs.djangoproject.com/en/dev/ref/signals/#got-request-exception

但是,这并不能让您访问异常对象,因此中间件方法更容易使用。


正如前面提到的,Django Sentry是一个很好的方法,但是正确设置它需要做一些工作(作为一个单独的网站)。如果您只想将所有内容记录到一个简单的文本文件中,那么就是要放入settings.py的日志记录配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        # Include the default Django email handler for errors
        # This is what you'd get without configuring logging at all.
        'mail_admins': {
            'class': 'django.utils.log.AdminEmailHandler',
            'level': 'ERROR',
             # But the emails are plain text by default - HTML is nicer
            'include_html': True,
        },
        # Log to a text file that can be rotated by logrotate
        'logfile': {
            'class': 'logging.handlers.WatchedFileHandler',
            'filename': '/var/log/django/myapp.log'
        },
    },
    'loggers': {
        # Again, default Django configuration to email unhandled exceptions
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
        # Might as well log any errors anywhere else in Django
        'django': {
            'handlers': ['logfile'],
            'level': 'ERROR',
            'propagate': False,
        },
        # Your own app - this assumes all your logger names start with"myapp."
        'myapp': {
            'handlers': ['logfile'],
            'level': 'WARNING', # Or maybe INFO or DEBUG
            'propagate': False
        },
    },
}


另一个答案中提到的django-db-log已被替换为:

https://github.com/dcramer/django-sentry


显然James是正确的,但如果您想在数据存储区中记录异常,那么已经有一些开源解决方案可用:

1)CrashLog是一个不错的选择:http://code.google.com/p/django-crashlog/

2)Db-Log也是一个不错的选择:http://code.google.com/p/django-db-log/

两者有什么区别?几乎没有什么我能看到的,所以任何一个都足够了。

我已经使用过它们并且效果很好。


自EMP最有用的代码提交以来已经过去了一段时间。我刚刚实现了它,并且在使用一些manage.py选项时,为了试图追查一个bug,我得到了一个弃用警告,结果是我当前版本的Django(1.5。?)现在有一个require_debug_false过滤器mail_admins处理程序所需的。

这是修改后的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
         'require_debug_false': {
             '()': 'django.utils.log.RequireDebugFalse'
         }
     },
    'handlers': {
        # Include the default Django email handler for errors
        # This is what you'd get without configuring logging at all.
        'mail_admins': {
            'class': 'django.utils.log.AdminEmailHandler',
            'level': 'ERROR',
            'filters': ['require_debug_false'],
             # But the emails are plain text by default - HTML is nicer
            'include_html': True,
        },
        # Log to a text file that can be rotated by logrotate
        'logfile': {
            'class': 'logging.handlers.WatchedFileHandler',
            'filename': '/home/username/public_html/djangoprojectname/logfilename.log'
        },
    },
    'loggers': {
        # Again, default Django configuration to email unhandled exceptions
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
        # Might as well log any errors anywhere else in Django
        'django': {
            'handlers': ['logfile'],
            'level': 'ERROR',
            'propagate': False,
        },
        # Your own app - this assumes all your logger names start with"myapp."
        'myapp': {
            'handlers': ['logfile'],
            'level': 'DEBUG', # Or maybe INFO or WARNING
            'propagate': False
        },
    },
}


我的fcgi脚本遇到了烦人的问题。它发生在django甚至开始之前。伐木的缺乏令人痛苦。无论如何,将stderr重定向到文件,因为第一件事有很多帮助:

1
2
#!/home/user/env/bin/python
sys.stderr = open('/home/user/fcgi_errors', 'a')