【问题标题】:Location of Django logs and errorsDjango 日志和错误的位置
【发布时间】:2013-10-15 22:04:55
【问题描述】:

我用 nginx 搭建了 django 服务器,在某些页面出现 403 错误。

在哪里可以找到 django 日志?在哪里可以查看详细的错误?

【问题讨论】:

  • 你在 nginx 和 Django 之间运行什么?独角兽?你在使用主管吗?
  • 您是否在 settings.py 文件中设置了 DEBUG = True ?如果是这样,如果错误看起来像i.imgur.com/TWL6f.png,那么它可能是一个 django 问题。如果 403 错误不是详细消息,那么我猜这是 nginx 问题。如果是 Django 问题,请使用控制台启动 django 服务器,然后转到出现错误的页面。希望控制台输出足以让您找出问题。

标签: django


【解决方案1】:

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

# 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,
        },
    }
}

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

    '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 个历史版本的循环日志。

在上面的loggers 部分,您需要将applogfile 添加到您的应用程序的handlers

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

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

【讨论】:

  • 在这种情况下做logger = logging.getLogger('APPNAME')
  • 您必须将此行添加到每个views.py
【解决方案2】:

添加到您的settings.py

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': 'debug.log',
        },
    },
    'loggers': {
        'django': {
            'handlers': ['file'],
            'level': 'DEBUG',
            'propagate': True,
        },
    },
}

它会在你的根目录中创建一个名为debug.log 的文件。 https://docs.djangoproject.com/en/1.10/topics/logging/

【讨论】:

  • 在 Apache2 上,wsgi 将尝试在/var/www/debug.log 中创建一个文件。如果您的 Django 项目位于 /var/www/ 目录之外,这将创建 2 个单独的文件。请改用'filename': os.path.join(BASE_DIR, 'debug.log'),
  • 接受的答案对我不起作用。这适用于 Django 2!
  • 我正在使用 django 3.1,我得到了Internal Server Error。有什么新的解决方案吗?
【解决方案3】:

设置https://docs.djangoproject.com/en/dev/topics/logging/,然后这些错误将回显您指向它们的位置。默认情况下,它们往往会在杂草中消失,所以我总是先从良好的日志设置开始。

这是一个非常好的基本设置示例: https://ian.pizza/b/2013/04/16/getting-started-with-django-logging-in-5-minutes/

编辑:新链接移至:https://github.com/ianalexander/ianalexander/blob/master/content/blog/getting-started-with-django-logging-in-5-minutes.html

【讨论】:

猜你喜欢
  • 2014-04-15
  • 2013-10-19
  • 2011-01-12
  • 1970-01-01
  • 2017-03-19
  • 2016-05-17
  • 1970-01-01
  • 2021-11-07
  • 2012-11-21
相关资源
最近更新 更多