2012-11-20 101 views
22

我在ngninx後面運行gunicorn。我想在gunicorn-error.log中記錄錯誤,並將日誌記錄到gunicorn-access.log。無法訪問日誌爲gunicorn工作

我有錯誤日誌工作,但沒有訪問日誌,我做錯了什麼?

這是我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與錯誤日誌和訪問日誌?

回答

18

我已經改變了我的日誌配置在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, 
     }, 
    }, 
} 
+0

謝謝!也許更好的日誌在那裏。 – Mikael

+1

問題在於文件處理程序不能通過多個進程登錄。 – dalore

+1

只要fyi萬一其他人遇到這種情況,爲Formatter指定一個特定的類,關鍵是'「()」'不是「class」,類的關鍵是處理程序。文檔中提到[此處](https://docs.python.org/2/library/logging.config.html#logging-config-dictschema)。 – danny

16

指定'disable_existing_loggers': Falselogging.config.dictConfig爲我工作。

disable_existing_loggers - 如果指定爲False,當這個調用時所存在記錄儀單獨留在家中。默認值爲True ,因爲這會以向後兼容的方式啓用舊行爲。這種 行爲是禁用任何現有的記錄器,除非它們或其 祖先在記錄配置中明確命名。

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