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
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:
import sys
class Logger(object):
def __init__(self):
self.console = sys.stderr
self.file = open("runserver.log", "a", 0)
def write(self, msg):
self.console.write(msg)
self.file.write(msg)
sys.stderr = Logger()
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With