2016-06-09 52 views
2

當我運行Django開發服務器(./manage.py runserver)所有請求的網址方便地登錄到工藝標準輸出,配合精確的時間和響應代碼:記錄所有請求提交的Django

[09/Jun/2016 23:35:53] "GET /api/game/ HTTP/1.1" 404 3185 
[09/Jun/2016 23:36:01] "GET /api/game/123/ HTTP/1.1" 404 1735 

這是非常方便,因爲分析輸出時,您可以立即看到該請求相對應的日誌信息,例如:

WARNING:2016-06-09 23:41:27,806:views:7449:140139847718656: No such object in the database: u'123' 
[09/Jun/2016 23:41:27] "GET /api/game/123/ HTTP/1.1" 404 1735 

我使用uwsgi工作+ nginx的,所以我用「控制檯」日誌處理程序的一切,然後開始uwsgi這樣:

exec uwsgi --master --die-on-term --logto /var/log/uwsgi.log 

因此,我在/var/log/uwsgi.log,uwsgi的請求記錄和我自己的日誌消息中獲得了所有必要的日誌記錄。

現在我想用Apache + mod WSGI + django實現相同的結果。我希望唯一的文件包含來自我的django應用程序的所有請求和所有日誌。

我試圖用Django的日誌記錄配置實現這一點,但即使當我將django.requests重定向到同一個文件時,我在日誌中只獲取自己的消息,根本沒有請求。下面是配置的一部分:

'handlers': { 
    'file_handler': { 
     'level': DEBUG and 'DEBUG' or 'INFO', 
     'class': 'logging.handlers.RotatingFileHandler', 
     'filename': join(LOG_DIRECTORY, 'api_log.log'), 
     'maxBytes': 1024 * 1024 * 5, # 5 MB 
     'backupCount': 15, 
     'formatter': 'verbose', 
    }, 
}, 
'loggers': { 
    'api': { 
     'handlers': ['file_handler'], 
     'level': DEBUG and 'DEBUG' or 'INFO', 
    }, 
    'django': { 
     'handlers': ['file_handler'], 
     'level': DEBUG and 'DEBUG' or 'INFO', 
    }, 
    'django.request': { 
     'handlers': ['file_handler'], 
     'level': DEBUG and 'DEBUG' or 'INFO', 
    }, 
    'django.db.backends': { 
     'handlers': ['file_handler'], 
     'level': DEBUG and 'INFO' or 'WARNING', 
     'propagate': False, 
    }, 
} 

有沒有辦法實現的nginx + uwsgi + Django的登錄行爲與Apache + WSGI + Django的?或者唯一的辦法是保持apache access.log和我的日誌在單獨的文件?

我想在第一種情況下,它是開發服務器誰記錄了請求,在第二種情況下,它是uwsgi進程。也許有一種方法可以告訴WSGIDaemonProcess執行相同的操作嗎?

回答

3

對於標準的Apache安裝,您正試圖混合訪問日誌和錯誤日誌,從而違背了最佳實踐。傳統上,它們已經分開,因此可以對服務器流量上的訪問日誌進行分析。

這就是說,你有沒有試過改變Apache中的ErrorLogCustomLog指令來使用相同的文件?

當我使用mod_wsgi的快車與命令:

mod_wsgi-express start-server --access-log --access-log-name application.log --error-log-name application.log 

它產生:

<IfDefine MOD_WSGI_ROTATE_LOGS> 
ErrorLog "|/usr/sbin/rotatelogs \ 
    /tmp/mod_wsgi-localhost:8000:502/application.log.%Y-%m-%d-%H_%M_%S 5M" 
</IfDefine> 
<IfDefine !MOD_WSGI_ROTATE_LOGS> 
ErrorLog "/tmp/mod_wsgi-localhost:8000:502/application.log" 
</IfDefine> 
LogLevel warn 

<IfDefine MOD_WSGI_ACCESS_LOG> 
<IfModule !log_config_module> 
LoadModule log_config_module ${MOD_WSGI_MODULES_DIRECTORY}/mod_log_config.so 
</IfModule> 
LogFormat "%h %l %u %t \"%r\" %>s %b" common 
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" combined 
LogFormat "undefined" custom 
<IfDefine MOD_WSGI_ROTATE_LOGS> 
CustomLog "|/usr/sbin/rotatelogs \ 
    /tmp/mod_wsgi-localhost:8000:502/application.log.%Y-%m-%d-%H_%M_%S 5M" common 
</IfDefine> 
<IfDefine !MOD_WSGI_ROTATE_LOGS> 
CustomLog "/tmp/mod_wsgi-localhost:8000:502/application.log" common 
</IfDefine> 
</IfDefine> 

與所得到的application.log之中:

[Fri Jun 10 07:17:30.845264 2016] [mpm_prefork:notice] [pid 84334] AH00163: Apache/2.4.18 (Unix) mod_wsgi/4.5.2 Python/2.7.10 configured -- resuming normal operations 
[Fri Jun 10 07:17:30.845518 2016] [core:notice] [pid 84334] AH00094: Command line: 'httpd (mod_wsgi-express) -f /tmp/mod_wsgi-localhost:8000:502/httpd.conf -D MOD_WSGI_ACCESS_LOG -D FOREGROUND' 
::1 - - [10/Jun/2016:07:17:36 +1000] "GET/HTTP/1.1" 200 709 
::1 - - [10/Jun/2016:07:17:37 +1000] "GET/HTTP/1.1" 200 709 
::1 - - [10/Jun/2016:07:17:37 +1000] "GET/HTTP/1.1" 200 709 
::1 - - [10/Jun/2016:07:17:38 +1000] "GET/HTTP/1.1" 200 709 
::1 - - [10/Jun/2016:07:17:38 +1000] "GET/HTTP/1.1" 200 709 
[Fri Jun 10 07:17:39.784486 2016] [mpm_prefork:notice] [pid 84334] AH00169: caught SIGTERM, shutting down 

因此從技術上講它應該工作對於同時出現錯誤和訪問日誌的獨立Apache安裝只需將ErrorLogCustomLog設置爲相同的文件即可。

至於額外的Django的記錄也可能在內部產生因異常,你仍然還需要:

LOGGING = { 
    'version': 1, 
    'disable_existing_loggers': False, 
    'handlers': { 
     'console': { 
      'class': 'logging.StreamHandler', 
     }, 
    }, 
    'loggers': { 
     'django': { 
      'handlers': ['console'], 
      'level': os.getenv('DJANGO_LOG_LEVEL', 'INFO'), 
     }, 
    }, 
} 

這告訴Django,以名義上登錄到終端,這mod_wsgi的將攔截和發送到Apache的錯誤日誌,這與上面的將是組合的應用程序日誌。

順便說一句,如果想在運行Apache/mod_wsgi的日誌需要轉到標準輸出的容器中,請不要自己動手。使用mod_wsgi-express,因爲它專門設計用於容器。在這種情況下,您只需使用:

mod_wsgi-express start-server --access-log --log-to-terminal 

如果要啓用訪問日誌記錄(默認關閉的通常只是容器部署噪聲),它會擔心派出了訪問和錯誤日​​志發送到終端,從而泊塢窗可以捕獲它。

如果需要更多信息或幫助,請使用mod_wsgi郵件列表。