When I run the django development server (./manage.py runserver
) all requested urls are conveniently logged into process stdout, with the precise time and the response code:
[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
It is very handy because when analysing output you immediately see the request corresponding to your log messages, for example:
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
I used to work with uwsgi+nginx, so I used 'console' logging handler for everything, and then started uwsgi like this:
exec uwsgi --master --die-on-term --logto /var/log/uwsgi.log
As a result I got all the necessary logging in /var/log/uwsgi.log
, uwsgi's request records and my own logging messages.
Now I want to achieve the same result with Apache+mod WSGI+django. I want the only file to contain all requests and all the logs from my django app in one place.
I've tried to achieve this with Django logging configuration, but even when I redirect django.requests to the same file I get only my own messages in the logs, no requests at all. Here is the portion of the configuration:
'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,
},
}
Is there a way to achieve nginx+uwsgi+django logging behavior with apache+WSGI+django? Or the only way is to keep apache access.log and my logs in separate files?
I guess in the first case it was development server who logged requests, and in the second case it was uwsgi process. Maybe there's a way to tell WSGIDaemonProcess to do the same?
For a standard Apache installation you are going against what is regarded as best practice by trying to mingle both access logs and error logs. Traditionally they have been left separate so that analysis could be done on access logs on server traffic.
That said, have you tried changing the
ErrorLog
andCustomLog
directives in Apache to use the same file?When I used mod_wsgi-express with the command:
it is generating:
with the resulting
application.log
being:So technically it should work for a standalone Apache installation with both error and access log going to the same file just by setting
ErrorLog
andCustomLog
to the same file.As for additional Django logging it may generate internally due to exceptions, you still would also need:
This tells Django to notionally log to the terminal, which mod_wsgi will intercept and send to the Apache error log, which with the above will be the combined application log.
BTW, if wanting to run Apache/mod_wsgi in a container where logging needs to go to standard output, do not do it yourself. Use mod_wsgi-express as it is specifically designed for use in containers. In that case you would just use:
if want to enable access logging (off by default as generally just noise for container deployments) and it will worry about sending both access and error logs to the terminal so Docker can capture it.
If need more information or assistance, use the mod_wsgi mailing list.