5

I am trying to get Django logging to work on our dev server. I have read How do you log server errors on django sites which seems to be the definitive answer on the subject, and have copied the configuration from the answer https://stackoverflow.com/a/6395837/1094910 into my settings.py (changing the output file for the WatchedFileHandler, of course; and adding my admin email address to settings.ADMINS).

Neither the AdminEmailHandler nor the WatchedFileHandler are doing what they're supposed to do when I induce an error 500 on the site. I have set settings.DEBUG to False and have confirmed that Django can both send email and write to the file. I've also tried logging directly from the Django shell:

$ /usr/local/pythonenv/DJANGO/bin/python manage.py shell
...
>>> import logging
>>> l = logging.getLogger('django')
>>> l.handlers[0]
<logging.handlers.WatchedFileHandler instance at 0xcd3b48>
>>> l.error("This should write to a file")
>>> l = logging.getLogger('django.request')
>>> l.handlers[0]
<django.utils.log.AdminEmailHandler instance at 0xcd37e8>
>>> l.error("This should email the addresses in settings.ADMINS")

Neither logger emits a peep in the above case.

However, generic Python logging will work (for the file, at least), proving that it's not a file permission issue:

$ python     # (or /usr/local/pythonenv/DJANGO/bin/python manage.py shell)
...
>>> import logging
>>> logging.basicConfig(filename='/path/to/same/file/as/above', level=logging.DEBUG)
>>> logging.error("This one shows up")

As noted above, Django can and happily will send emails throughout the rest of my project, just not in this case.

The WatchedFileHandler works on my local machine with the same configuration (except for file path of course); the AdminEmailHandler does not, but I suspect that is because I am using Gmail as an email server as described here. (Local machine is Windows 7; dev server on which the config doesn't work is Debian 6.0.3.)

I feel as though I'm missing something simple and obvious but have spent most of a day trying to figure out what it could be. Any suggestions as to what configurations I might have missed, or what other troubleshooting I should try, to figure out why Python's logging works but Django's doesn't?

Community
  • 1
  • 1
Eric P
  • 4,587
  • 6
  • 24
  • 19
  • Not sure why this was closed when it's very obviously a programming/development question. Regardless, for the reference of searchers finding this page, I figured out the source of the problems: the WatchedFileHandler was my bad as I was using a settings override like [this](https://gist.github.com/962776) which wasn't overriding the log file path properly. The AdminEmailHandler wasn't working because I hadn't set a value for `SERVER_EMAIL` in `settings.py` and sendmail failed silently on the default value of `root@localhost`. Hope this helps anyone else with a similar problem. – Eric P Feb 03 '12 at 22:51
  • thanks for the trick of using the shell to find out how the logger is configured – gipi Feb 20 '13 at 16:45

0 Answers0