I started a project in Django 1.4, and every mistake I made via try..except
I printed in sys.stderr
- making it fall into the same Apache log file (error.log). It worked, but only for the errors that I explicitly took, and in addition the errors of all running instances went to the same file. But the advantage is that Apache prefixed all errors with the date and time of the error.
When I upgraded to Django 1.9, I started using the logging system based on in this first example (in settings.py):
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'file': {
'level': 'ERROR',
'class': 'logging.FileHandler',
'filename': '/var/www/vhosts/example.com/statistics/logs/django_errors.log',
},
},
'loggers': {
'django.request': {
'handlers': ['file'],
'level': 'ERROR',
'propagate': True,
},
},
}
And he went on to log all 500 errors in the specified file, including those that I did not explicitly catch. The only problem is that it only logs the error itself, gives no indication of the date and time of the error, nor any additional information:
Internal Server Error: /admin/example/projeto/add/
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 132, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py", line 618, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
...
File "/usr/local/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File "/usr/local/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
IntegrityError: (1048, "Column 'area' cannot be null")
Is there a simple way to log the date and time of the error, to make it easier to correlate with the entry in access.log (where something like "POST /meu/caminho HTTP/1.1" 500
is written)? I imagine I have to fiddle with formatters
, but reading the documentation I do not quite understand how it works. Most important to me is the stack trace , of course, but this additional information would be very useful (if it is not possible, or it's something more complicated, I can live without it - after all this kind of error is relatively rare).
P.S. I'm using mod_wsgi, if this is of any relevance.