【问题标题】:Error Logging in Django and GunicornDjango 和 Gunicorn 中的错误日志记录
【发布时间】:2016-05-17 17:59:58
【问题描述】:

当我遇到错误时,我想登录 Django 和 Gunicorn。 我用 Python 学习 TDD,http://chimera.labs.oreilly.com/books/1234000000754/ch17.html#_setting_up_logging

这是我的代码。 /etc/init/gunicorn-superlists-staging.mysite.com.conf

description "Gunicorn server for superlists-staging.mysite.com"

start on net-device-up
stop on shutdown

respawn

setuid junsu
chdir /home/junsu/sites/superlists-staging.mysite.com/source

exec ../virtualenv/bin/gunicorn \
    --bind unix:/tmp/superlists-staging.mysite.com.socket \
    --access-logfile ../access.log \
    --error-logfile ../error.log \
    superlists.wsgi:application

accounts/authentication.py

import requests
import logging
from django.contrib.auth import get_user_model
User = get_user_model()

PERSONA_VERIFY_URL = 'https://verifier.login.persona.org/verify'
DOMAIN = 'localhost'

class PersonaAuthenticationBackend(object):

    def authenticate(self, assertion):
        logging.warning('authenticate function')
        response = requests.post(
            PERSONA_VERIFY_URL,
            data={'assertion': assertion, 'audience': settings.DOMAIN}
        )
        logging.warning('got response form persona')
        logging.warning(response.content.decode())
        if response.ok and response.json()['status'] == 'okay':
            email = response.json()['email']
            try:
                return User.objects.get(email=email)
            except User.DoesNotExist:
                return User.objects.create(email=email)

    def get_user(self, email):
        try:
            return User.objects.get(email=email)
        except User.DoesNotExist:
            return None

超级列表/settings.py

[....]
LOGGING = {
    'version': 1,
    'disable_existing_logger': False,
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
        },
    },
    'loggers': {
        'django': {
            'handlers': ['console'],
        },
    },
    'root': {'level': 'INFO'},
}

我的“error.log”只是记录这个。

[2016-02-08 14:42:56 +0900] [3355] [INFO] Listening at: unix:/tmp/superlists-staging.mysite.com.socket (3355)
[2016-02-08 14:42:56 +0900] [3355] [INFO] Using worker: sync
[2016-02-08 14:42:56 +0900] [3359] [INFO] Booting worker with pid: 3359
[2016-02-08 14:58:22 +0900] [3355] [INFO] Handling signal: term
[2016-02-08 14:58:22 +0900] [3355] [INFO] Shutting down: Master
[2016-02-08 14:58:22 +0900] [3470] [INFO] Starting gunicorn 19.4.3
[2016-02-08 14:58:22 +0900] [3470] [INFO] Listening at: unix:/tmp/superlists-staging.mysite.com.socket (3470)
[2016-02-08 14:58:22 +0900] [3470] [INFO] Using worker: sync
[2016-02-08 14:58:22 +0900] [3474] [INFO] Booting worker with pid: 3474

我想查看错误日志,我该怎么办?

【问题讨论】:

    标签: python django logging


    【解决方案1】:

    对于那些搜索 错误日志 的 (nginx + gunicorn + django) 设置:

    只需使用这些 (一些命令也特定于您的套接字文件所在的位置)

    • 通过键入以下内容检查 Nginx 进程日志:sudo journalctl -u nginx
    • 通过键入以下内容检查 Nginx 访问日志:sudo less /var/log/nginx/access.log
    • 通过键入以下内容检查 Nginx 错误日志:sudo less /var/log/nginx/error.log
    • 通过键入以下内容检查 Gunicorn 应用程序 日志:sudo journalctl -u gunicorn
    • 通过键入以下内容检查 Gunicorn 套接字日志:sudo journalctl -u gunicorn.socket

    Reference

    【讨论】:

    • 检查各种日志的好方法。它帮助了我。就我而言,我终于从 Ubunut 的系统日志中发现了错误。运行dmesg
    【解决方案2】:

    tl;dr 你的代码没有问题

    您似乎正确地遵循了链接教程,并且可能会在 /home/junsu/sites/superlists-staging.mysite.com/ 目录中找到您的日志文件。

    不管怎样,你的问题有几点需要解决,我会尽力做到的。

    记录器和处理程序

    您在上面引用的设置模块设置了一个日志处理程序console (StreamHandler),以及一个可以使用该处理程序的django 记录器。

    root 记录器没有定义任何处理程序,“django”会将任何内容记录到stderr,并且仅用于级别 INFO 及以上。我进行了快速测试,root 记录器还默认定义了StreamHandler

    您的authentication.py 模块当前调用logging.warning which logs to root logger(即它调用logger = logging.getLogger(); logger.warning('stuff'))。但是,您可能希望定义更具体的处理程序以更轻松地定位模块的日志。这是解释in this section of the referenced tutorial

    Gunicorn 默认重定向标准错误

    似乎by default is set up to capture the stderr stream,您当前将其重定向到日志文件。但是,我的建议是使用您的守护程序应用程序(好像您正在使用upstart)来记录标准错误/输出。

    新贵记录

    正如gunicorn docs 中所述,配置upstart 非常简单。

    如果您在/etc/init/gunicorn-superlists-staging.mysite.com.conf 配置中删除--error-logfile 选项,gunicorn 将默认将其输出记录到stderr,然后可以由暴发户以您喜欢的任何方式捕获。

    如果您使用的是 upstart 1.7 或更高版本,stdout/err capturing should be enabled by default。但是,如果您使用较早版本的 upstart,我的建议是添加 a console log option in your config,所有输出(stdout/stderr)将简单地记录到(我假设)/var/log/upstart/gunicorn-superlists-staging.mysite.com.log

    【讨论】:

    • 谢谢!,我看到了暴发户的日志文件。我认为,它非常有用。但我无法解决 error.log 文件问题。教程完我会继续的。
    猜你喜欢
    • 1970-01-01
    • 2021-11-07
    • 1970-01-01
    • 2014-03-31
    • 2016-04-24
    • 2011-09-12
    • 2020-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多