28

我在 ngninx 后面运行 gunicorn。我想将 gunicorn 中的错误记录到 gunicorn-error.log 并将日志访问到 gunicorn-access.log。

我有错误日志工作但没有访问日志,我做错了什么?

这是我的 gunicorn.conf.py:

bind = '127.0.0.1:8888'
backlog = 2048
workers = 3
errorlog = '/home/my/logs/gunicorn-error.log'
accesslog = '/home/my/logs/gunicorn-access.log'
loglevel = 'debug'
proc_name = 'gunicorn-my'
pidfile = '/var/run/my.pid'

这是运行 gunicorn 的脚本:

#!/bin/bash
set -e
ENV=/home/my/env/bin/activate
GUNICORN=gunicorn_django
SETTINGS_PATH=/home/my/app/app/settings
PROJECT_PATH=/home/my/app
CONFROOT=/home/my/app/conf/gunicorn.conf.py

cd $SETTINGS_PATH
source $ENV
export PYTHONPATH=$PROJECT_PATH
exec $GUNICORN app.settings.staging -c $CONFROOT

它同时创建 gunicorn-error.log 和 gunicorn-access.log 但只有 gunicorn-error.log 获取任何日志,例如:

2012-11-20 11:49:57 [27817] [INFO] Starting gunicorn 0.14.6
2012-11-20 11:49:57 [27817] [DEBUG] Arbiter booted
2012-11-20 11:49:57 [27817] [INFO] Listening at: http://127.0.0.1:8888 (27817)
2012-11-20 11:49:57 [27817] [INFO] Using worker: sync
2012-11-20 11:49:58 [27825] [INFO] Booting worker with pid: 27825
2012-11-20 11:49:58 [27828] [INFO] Booting worker with pid: 27828
2012-11-20 11:49:58 [27830] [INFO] Booting worker with pid: 27830

我究竟做错了什么?任何人都想与错误日志和访问日志分享他们的工作 gunicorn.conf.py 吗?

4

2 回答 2

24

我已将 Django 中的日志记录配置更改为以下内容,它有所帮助:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'root': {
        'level': 'WARNING',
        'handlers': ['sentry'],
    },
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
        },
        'generic': {
            'format': '%(asctime)s [%(process)d] [%(levelname)s] %(message)s',
            'datefmt': '%Y-%m-%d %H:%M:%S',
            '()': 'logging.Formatter',
        },
    },
    'handlers': {
        'sentry': {
            'level': 'ERROR',
            'class': 'raven.contrib.django.handlers.SentryHandler',
        },
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose'
        },
        'error_file': {
            'class': 'logging.FileHandler',
            'formatter': 'generic',
            'filename': '/home/fungine/gunicorn.error.log',
        },
        'access_file': {
            'class': 'logging.FileHandler',
            'formatter': 'generic',
            'filename': '/home/fungine/gunicorn.access.log',
        },
    },
    'loggers': {
        'django.db.backends': {
            'level': 'ERROR',
            'handlers': ['console'],
            'propagate': False,
        },
        'raven': {
            'level': 'DEBUG',
            'handlers': ['console'],
            'propagate': False,
        },
        'sentry.errors': {
            'level': 'DEBUG',
            'handlers': ['console'],
            'propagate': False,
        },
        'gunicorn.error': {
            'level': 'INFO',
            'handlers': ['error_file'],
            'propagate': True,
        },
        'gunicorn.access': {
            'level': 'INFO',
            'handlers': ['access_file'],
            'propagate': False,
        },
    },
}
于 2012-11-27T10:00:59.310 回答
22

为我指定'disable_existing_loggers': False工作logging.config.dictConfig

disable_existing_loggers – 如果指定为False,则在进行此调用时存在的记录器将被单独保留。默认是True 因为这会以向后兼容的方式启用旧行为。此行为是禁用任何现有记录器,除非它们或其祖先在日志记录配置中明确命名。

http://docs.python.org/2/library/logging.config.html#logging.config.fileConfig

于 2013-11-26T10:11:48.437 回答