How to log the cause of a 500 error

2,335 views
Skip to first unread message

Delta20

unread,
Nov 5, 2008, 3:58:49 PM11/5/08
to Django users
Is there a way to have django log 500 errors to a file, and otherwise
log stderr/stdout to a file somewhere?

The problem I'm having right now is that I'm getting a 500 error
instead of 404s when Debug=False. I get 404s as expected when
Debug=True. I can't for the life of me figure out what is breaking,
since I don't have any error data to go off.

If I create a 500.html template, that displays. If I remove the
500.html template, I get:

--
Traceback (most recent call last):

File "/Library/Python/2.5/site-packages/django/core/servers/
basehttp.py", line 277, in run
self.result = application(self.environ, self.start_response)

File "/Library/Python/2.5/site-packages/django/core/servers/
basehttp.py", line 634, in __call__
return self.application(environ, start_response)

File "/Library/Python/2.5/site-packages/django/core/handlers/
wsgi.py", line 239, in __call__
response = self.get_response(request)

File "/Library/Python/2.5/site-packages/django/core/handlers/
base.py", line 128, in get_response
return self.handle_uncaught_exception(request, resolver, exc_info)

File "/Library/Python/2.5/site-packages/django/core/handlers/
base.py", line 160, in handle_uncaught_exception
return callback(request, **param_dict)

File "/Library/Python/2.5/site-packages/django/views/defaults.py",
line 88, in server_error
t = loader.get_template(template_name) # You need to create a
500.html template.

File "/Library/Python/2.5/site-packages/django/template/loader.py",
line 80, in get_template
source, origin = find_template_source(template_name)

File "/Library/Python/2.5/site-packages/django/template/loader.py",
line 73, in find_template_source
raise TemplateDoesNotExist, name

TemplateDoesNotExist: 500.html
--

Even when I explicitly throw a 404 from a view, I still get a 500
error, so I definitely need some way to debug this.

Daniel Hepper

unread,
Nov 5, 2008, 4:45:26 PM11/5/08
to django...@googlegroups.com
> The problem I'm having right now is that I'm getting a 500 error
> instead of 404s when Debug=False. I get 404s as expected when
> Debug=True. I can't for the life of me figure out what is breaking,
> since I don't have any error data to go off.

Django sends you exception logs by mail if you set DEBUG=False. Just
define mail settings (1_) and supply a valid email adress with the
ADMINS variable in settings.py.

Concerning your original issue, did you define a 404.html template?

Regards,
Daniel

_1 http://docs.djangoproject.com/en/dev/topics/email/

Delta20

unread,
Nov 5, 2008, 5:11:25 PM11/5/08
to Django users
I just realized that I accidentally named the template 400.html not
404.html... d'oh. I definitely need a better way of diagnosing 500
errors.

Thanks to the pointer to the email settings. I will set that up, but
I'm wondering if there's a way to direct django to log those errors
rather than email it. I see there is a middleware component, django-db-
log that seems to do that. I am using django-logging, and it would be
very handy to find a way to get the error messages to go the log file
instead.

That aside, I couldn't imagine a live production deployment of django
using email for logging errors.





Eric Abrahamsen

unread,
Nov 5, 2008, 7:05:04 PM11/5/08
to django...@googlegroups.com

On Nov 6, 2008, at 6:11 AM, Delta20 wrote:

>
> I just realized that I accidentally named the template 400.html not
> 404.html... d'oh. I definitely need a better way of diagnosing 500
> errors.
>
> Thanks to the pointer to the email settings. I will set that up, but
> I'm wondering if there's a way to direct django to log those errors
> rather than email it. I see there is a middleware component, django-
> db-
> log that seems to do that. I am using django-logging, and it would be
> very handy to find a way to get the error messages to go the log file
> instead.

Django-logging is pretty much just python's standard logging module
with a custom handler and some fancy additions, so you can define your
own file handler and use it in middleware or views to log messages to
a local file:

http://docs.python.org/library/logging.html#filehandler

>
>
> That aside, I couldn't imagine a live production deployment of django
> using email for logging errors.

It's actually not a bad idea! Given that server errors should NOT be a
ten-per-minute kind of thing, you definitely want to know as soon as
possible when one occurs. The logging module is impressive: silly
things like getting an IM or a twitter when an error occur are
relatively easy to set up.

Even getting internal 404s via email isn't too onerous, provided you
make good use of ignorable_404_ends.

Yours,
Eric


>
>
>
>
>
>
> >

Reply all
Reply to author
Forward
0 new messages