【问题标题】:Django Logging not outputting to log file on nginx serverDjango Logging 未输出到 nginx 服务器上的日志文件
【发布时间】:2017-09-28 14:06:28
【问题描述】:

我的 Django Logger 在我的本地机器上运行良好,但在我将它部署到服务器时无法运行。

本地:OS X 10.9.5、Python 2.7、Django 1.10.5

AWS 服务器:Ubuntu、Nginx、Gunicorn、Django

在我的本地设置中,我运行 python manage.py runserver,这会在我的根目录中创建 debug.log 文件。当我通过虚拟环境启动本地服务器时,我通过 localhost:8000 访问该站点,并在我的 debug.log 中获取这些日志。

"GET / HTTP/1.1" 200 2908
"GET /projects HTTP/1.1" 200 3461
Inside app views index

在我的生产服务器上,我运行python manage.py runserver,这将创建 debug.log 文件。我通过sudo service nginx restart 重新启动 Nginx 服务器,然后访问我的网页并单击与运行本地服务器时相同的链接。

当我vim debug.log 进入 debug.log 文件时,在我访问该网站并单击网页中的相同链接后,我看不到任何调试消息。

为什么在我的生产环境中没有记录数据记录到 debug.log 中?我错过了什么?

views.py

import logging
logger = logging.getLogger(__name__)

def index(request):
    logger.debug('Inside app views index')
    return render(request, "first_app/index.html")

local_settings.py(没关系,但我在本地机器和生产服务器中使用 local_settings.py)

#BASE_DIR was already part of django settings.py file when created.
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': os.path.join(BASE_DIR, 'debug.log'),
        },
        'null': {
            'class': 'logging.NullHandler',
        },
    },
    'loggers': {
        'django': {
            'handlers': ['file'],
            'level': 'DEBUG',
            'propagate': True,
        },
        'apps': {
            'handlers': ['file'],
            'level': 'DEBUG',
            'propagate': True,
        },
        'django.db.backends': {
            'handlers': ['null'],
            'propagate': False,
        },
    },
}

我的文件路径是

-homepage
  -apps
  -homepage
  -static
  -venv
  manage.py
  homepage.sock
  db.sqlite3
  debug.log
  requirement.txt

【问题讨论】:

  • 您的 nginx 用户是否有足够的权限来写入文件?
  • 你能显示你的gunicorn命令吗?
  • @scott 我不确定这是否是您要找的。当我输入 ls -all 时,我的 debug.log 文件在生产服务器中有“-rw-rw-r-- 1 ubuntu ubuntu 0 May 1 04:21 debug.log”。如果这不是您要查找的内容,请告诉我如何确定 nginx 用户对该文件的权限状态。
  • @DeanChristianArmada,我对 gunicorn 和一般部署非常陌生,您能给我指点一下如何检索您正在寻找的 gunicorn 命令吗?
  • 这样gunicorn project.wsgi:application -w 2 -b 0.0.0.0:8000 --log-level info --access-logfile /usr/logs/access.project.log --reload..这个命令会把所有的调试日志放到access.project.log上

标签: django nginx gunicorn


【解决方案1】:

我相信请求(GET、POST 等)日志会被 Nginx 拦截并记录到它的 nginx-access.log 中,而您的 INFO、ERROR 日志应该在 debug.log 中

【讨论】:

    猜你喜欢
    • 2018-02-09
    • 2014-03-10
    • 2021-09-19
    • 2021-06-24
    • 2018-03-29
    • 2016-02-21
    • 2015-06-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多