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

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

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

有帮助吗?

解决方案

好吧,当 DEBUG = False 时,Django会自动将任何错误的完整追溯邮件发送给 ADMINS 设置中列出的每个人,这会给你很多通知自由。如果您想要更细粒度的控制,您可以编写一个中间件类并将其添加到您的设置中,该类定义了一个名为 process_exception()的方法,该方法可以访问引发的异常: / p>

http://docs.djangoproject.com/en的/ dev /主题/ HTTP /中间件/#过程的异常

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

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

http://docs.djangoproject.com/en的/ dev / REF /信号/#得到请求的异常

允许您访问异常对象,因此中间件方法更容易使用。

其他提示

如前所述,Django Sentry是一个很好的方法,但正确设置(作为一个单独的网站)需要做一些工作。如果您只想将所有内容记录到一个简单的文本文件中,请在 settings.py

中输入日志记录配置
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的崩溃日志/

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

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

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

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

以下是修订后的代码:

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重定向到文件,因为第一件事有很多帮助:

#!/home/user/env/bin/python
sys.stderr = open('/home/user/fcgi_errors', 'a')
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top