【问题标题】:Django application not logging request info in production log file when deployed with gunicorn and Nginx使用 gunicorn 和 Nginx 部署时,Django 应用程序未在生产日志文件中记录请求信息
【发布时间】:2021-08-06 13:12:08
【问题描述】:

这是我的 Django 应用程序的日志设置。

LOG_DIR = "logs"
LOG_DIR_PATH = os.path.join(BASE_DIR, LOG_DIR)
if not os.path.exists(LOG_DIR_PATH):
    os.mkdir(LOG_DIR_PATH)

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "formatters": {
        "verbose": {
            "format": "{asctime} {levelname} : {filename} line - {lineno:d} : {message}",
            "style": "{",
            "datefmt": "%Y-%m-%d %H:%M:%S",
        },
    },
    "filters": {
        "require_debug_false": {
            "()": "django.utils.log.RequireDebugFalse",
        },
        "require_debug_true": {
            "()": "django.utils.log.RequireDebugTrue",
        },
    },
    "handlers": {
        "console": {
            "level": "INFO",
            "filters": ["require_debug_true"],
            "class": "logging.StreamHandler",
            "formatter": "verbose",
        },
        "production": {
            "level": "INFO",
            "filters": ["require_debug_false"],
            "class": "logging.handlers.RotatingFileHandler",
            "filename": os.path.join(LOG_DIR_PATH, "production.log"),
            "maxBytes": 1024 * 1024 * 5,  # 5 MB
            "backupCount": 5,
            "formatter": "verbose",
        },
        "development": {
            "level": "INFO",
            "filters": ["require_debug_true"],
            "class": "logging.handlers.RotatingFileHandler",
            "filename": os.path.join(LOG_DIR_PATH, "development.log"),
            "maxBytes": 1024 * 1024 * 5,  # 5 MB
            "backupCount": 5,
            "formatter": "verbose",
        },
        "task_handler": {
            "level": "INFO",
            "class": "logging.handlers.RotatingFileHandler",
            "filename": os.path.join(LOG_DIR_PATH, "tasks.log"),
            "maxBytes": 1024 * 1024 * 10,  # 10 MB
            "backupCount": 10,
            "formatter": "verbose",
        },
    },
    "loggers": {
        "django": {
            "handlers": ["console"],
        },
        "py.warnings": {
            "handlers": ["console"],
        },
        "django.request": {
            "handlers": ["console"],
            "level": "ERROR",
        },
        "task_logger": {
            "handlers": ["task_handler"],
            "level": "INFO",
            "propagate": False,
        },
    },
    "root": {
        "handlers": ["production", "development"],
        "level": "INFO",
        "propagate": False,
    },
}

所以,我希望它能在development.logproduction.log 中记录所有请求信息。它在开发模式下运行良好,也适用于 DEBUG=False 和 python manage.py runserver 命令。

但是当我使用 gunicorn 和 Nginx 部署它时,production.log 中没有出现请求日志。除了请求日志之外,production.log 中还会出现其他日志消息。

在 production.log 中记录我期望的消息

2021-08-05 11:48:37 INFO : basehttp.py line - 157 : "GET /api/v1/settings/vm/cloud-status/ HTTP/1.1" 200 46
2021-08-05 11:48:38 INFO : basehttp.py line - 157 : "GET /api/v1/settings/p1-vms/get-data-fetching-status/ HTTP/1.1" 200 30
2021-08-05 11:48:40 INFO : basehttp.py line - 157 : "GET /api/v1/settings/p1-vms/get-data-fetching-status/ HTTP/1.1" 200 30
2021-08-05 11:48:41 INFO : basehttp.py line - 157 : "GET /api/v1/settings/p1-vms/get-data-fetching-status/ HTTP/1.1" 200 30
2021-08-05 11:48:42 INFO : basehttp.py line - 157 : "GET /api/v1/settings/vm/cloud-status/ HTTP/1.1" 200 46

任何帮助将不胜感激。提前致谢。

【问题讨论】:

  • 试试这个:"django.request": {"handlers": ["production", "console"], "level": "INFO"},
  • 试过了,还是不行。 production.log 中没有出现请求日志
  • 然后尝试将 gunicorn 记录器与其他记录器一起添加到记录器字典中,例如"loggers": { 'gunicorn.access' : { 'level': 'DEBUG', 'handlers': ['production', 'console'], 'propagate':False}}
  • 成功了,谢谢。我不知道django.server 只使用runserver 命令记录请求@Wasi

标签: python django django-logging


【解决方案1】:
import logging.config


logging.config.dictConfig({
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'console': {
            'format': '%(name)-12s %(levelname)-8s %(message)s'
        }
    },
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'formatter': 'console'
        },

    },
    'loggers': {
        '': {
            'level': 'DEBUG',
            'handlers': ['console']
        }
    }
})

#试试这个

【讨论】:

  • 此设置不登录生产也不在文件中。
猜你喜欢
  • 2016-11-03
  • 2014-03-31
  • 2016-04-24
  • 1970-01-01
  • 1970-01-01
  • 2015-08-05
  • 2018-09-19
  • 2020-12-26
  • 2016-04-26
相关资源
最近更新 更多