Django日志和错误的位置 [英] Location of Django logs and errors

查看:103
本文介绍了Django日志和错误的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用nginx设置了django服务器,并且在某些页面中出现403错误。

I've set up django server with nginx, and it gets 403 error in some of the pages.

在哪里可以找到django日志?在哪里可以看到错误?

Where can I find the django logs? where can I see the errors in detail?

推荐答案

日志设置在您的 settings.py 文件中。一个新的默认项目如下所示:

Logs are set in your settings.py file. A new, default project, looks like this:

# A sample logging configuration. The only tangible logging
# performed by this configuration is to send an email to
# the site admins on every HTTP 500 error when DEBUG=False.
# See http://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
    }
}

默认情况下,这些不会创建日志文件。如果你想要这些,你需要在你的处理程序中添加一个 filename 参数

By default, these don't create log files. If you want those, you need to add a filename parameter to your handlers

    'applogfile': {
        'level':'DEBUG',
        'class':'logging.handlers.RotatingFileHandler',
        'filename': os.path.join(DJANGO_ROOT, 'APPNAME.log'),
        'maxBytes': 1024*1024*15, # 15MB
        'backupCount': 10,
    },

这将设置一个可以获得15 MB大小的旋转日志,并保留10个历史版本。

This will set up a rotating log that can get 15 MB in size and keep 10 historical versions.

在上述记录器部分中,您需要添加 applogfile 到您的应用程序的处理程序

In the loggers section from above, you need to add applogfile to the handlers for your application

'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
        'APPNAME': {
            'handlers': ['applogfile',],
            'level': 'DEBUG',
        },
    }

此示例将您的日志放在您的Django根目录中的文件名为 APPNAME.log

This example will put your logs in your Django root in a file named APPNAME.log

这篇关于Django日志和错误的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆