I am trying to configure my django settings.py
to properly use the python logging facility but I've stumbled upon a rather strange problem:
Even after reading the docs, I simply can't find out how to redirect the console printed debug request lines from Django to a file I've specified; Below is part of my logging configuration.
LOGGING = {
'version': 1,
'formatters': {
'simple': {
'format': '%(levelname)s %(message)s'
},
}
'handlers': {
'file_http': {
'level': 'DEBUG',
'class': 'logging.FileHandler',
'filename': r'C:\mysystem-http.log',
'formatter': 'verbose'
}
},
'loggers': {
'django.request': {
'handlers': ['file_http'],
'level': 'DEBUG',
'propagate': False
}
}
}
I keep seeing my console print line of the following format:
[19/Dec/2014 11:48:03] "POST /api/v1/ HTTP/1.1" 200 10
How may I redirect these to a file using the logging facility?
Thanks in advance
These outputs are handled by your HTTP server (WSGIServer from the standard library if running in dev mode).
The configuration of your
settings.py
has nothing to do with it.manage.py runserver
is not using logging system for messages like[19/Dec/2014 11:48:03] "POST /api/v1/ HTTP/1.1" 200 10
. Instead of this, runserver uses sys.stderr (and sys.stdout for others messages). If you really need to redirect this to file you can override sys.stderr settings.py. Example - logging sys.stderr to file and console:In write method you can use logging system to handle this by LOGGING settings as well.
Update:
In Django 1.10, runserver output goes through logging: https://docs.djangoproject.com/en/dev/releases/1.10/#runserver-output-goes-through-logging