logging django errors also on the server

69 views
Skip to first unread message

Gelonida N

unread,
Sep 4, 2011, 6:58:44 PM9/4/11
to django...@googlegroups.com
Hi,

Im am debugging a django application.

If I set DEBUG=True
then I can see error messages on the browser.

Is there any way to see the same error messages in the server log file?
Occasionally (especially for rpc client or Ajax requests I would prefer
looking at the log file instead of looking at the browser error messages)

Thanks for any suggestions.


Reinout van Rees

unread,
Sep 4, 2011, 8:03:50 PM9/4/11
to django...@googlegroups.com

With django 1.3, those 500 errors are logged to python's default
logging. So if you've got django 1.3 and a properly set up logging,
you'd get those logs.

If you're using django 1.2, you could use a middlewar such as
http://djangosnippets.org/snippets/421/ . I've used that one for almost
a year. Works like a charm.

But... hurray for django 1.3 for including it in the regular python
logging :-)


Reinout

--
Reinout van Rees http://reinout.vanrees.org/
rei...@vanrees.org http://www.nelen-schuurmans.nl/
"If you're not sure what to do, make something. -- Paul Graham"

Gelonida N

unread,
Sep 4, 2011, 8:25:18 PM9/4/11
to django...@googlegroups.com
On 09/05/2011 02:03 AM, Reinout van Rees wrote:
> On 05-09-11 00:58, Gelonida N wrote:
>> Im am debugging a django application.
>>
>> If I set DEBUG=True
>> then I can see error messages on the browser.
>>
>> Is there any way to see the same error messages in the server log file?
>> Occasionally (especially for rpc client or Ajax requests I would prefer
>> looking at the log file instead of looking at the browser error messages)
>
> With django 1.3, those 500 errors are logged to python's default
> logging. So if you've got django 1.3 and a properly set up logging,
> you'd get those logs.
>
This is what I expected, but I didn't see all the messages showing up.

I see my own log traces.
In many situations I do see error messages in the log output.

At a certain moment I managed to have a broken system, where nothing
showed up in the log files.
However when I accessed the server with a browser I got django's error
report pointing out a syntax error (typo) in my code.

Unfortunately I did not take a snapshot of my code and can't reproduce
this issue now.


I will repost as soon as I encounter this kind of issue again,

Reinout van Rees

unread,
Sep 4, 2011, 9:05:52 PM9/4/11
to django...@googlegroups.com
On 05-09-11 02:25, Gelonida N wrote:
> I see my own log traces.
> In many situations I do see error messages in the log output.

An important thing that's not mentioned too clearly in Django 1.3's
logging docs: *add a root logger*. So a logger with an empty string as a
name. That one catches all messages, including the ones to the
django.something.error logger.

Gelonida N

unread,
Sep 5, 2011, 4:24:25 AM9/5/11
to django...@googlegroups.com
On 09/05/2011 03:05 AM, Reinout van Rees wrote:
> On 05-09-11 02:25, Gelonida N wrote:
>> I see my own log traces.
>> In many situations I do see error messages in the log output.
>
> An important thing that's not mentioned too clearly in Django 1.3's
> logging docs: *add a root logger*. So a logger with an empty string as a
> name. That one catches all messages, including the ones to the
> django.something.error logger.
>
True, That's not added in the doc and could be added for clarity.
I had already a root logger added though.
So my problem must be somewhere else.

Will keep you informed when I manage to replicate the issue or when I
understand what went wrong.

Uros Trebec

unread,
Sep 5, 2011, 9:37:55 AM9/5/11
to Django users
Take a look at Sentry: http://readthedocs.org/docs/sentry/

It works pretty well, uses polling to update the log view, can track
multiple sites with separate Sentry Server, etc.

Good luck,
Uros

Gelonida N

unread,
Nov 19, 2011, 4:26:00 PM11/19/11
to django...@googlegroups.com

I am using Django 1.3
I configured logging and set up a root logger with log level 'DEBUG'

I added one log command and one explicit error in the urls.py file.
I can see the error report in the browser but not in my log files.

For me it would be very helpful if I can log ALL errors on the server
side if I wish to.

Is there any trick?

How to reproduce my problem:

# create a new django project
#----------------------------
django-admin.py startproject logproblem

# enter your project directory
# -----------------------------
cd logproblem

# create a urls.py which prints a log message
# and which causes an error afterwards
# file contents as in the next four lines
#------------------------------------------

# faulty urls.py file
import logging
logging.debug("hello")
1/0 # this will raise a ZeroDivisionError


# Now edit settings.py and change the LOGGING section to:
---------------------------------------------------------
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler'
}
},
'loggers': {
'': {
'handlers': ['console'],
'level': 'DEBUG',
'propagate': True,
},
}
}

# Now run the server
# ----------------------
./manage.py runserver


The result on the console will only be:
> Quit the server with CONTROL-C.
> hello
> [19/Nov/2011 15:18:32] "GET / HTTP/1.1" 500 79664

On the browser however I see the division by 0 error and the back trace.

For debugging of errors which are caused by a remote host I would really
like to see such kind of errors in the server logs or an any other log.

Gelonida N

unread,
Nov 19, 2011, 4:36:43 PM11/19/11
to django...@googlegroups.com
Hi Uros,

On 09/05/2011 03:37 PM, Uros Trebec wrote:
> Take a look at Sentry: http://readthedocs.org/docs/sentry/
>
> It works pretty well, uses polling to update the log view, can track
> multiple sites with separate Sentry Server, etc.

This looks like an option, but it is doing much more than I really want
to use.

I'm just looking for a trick to capture the django exceptions and send
the related backtraces to stdout (or a log file) (which will be stored
in a log file)

Gelonida N

unread,
Nov 19, 2011, 5:52:42 PM11/19/11
to django...@googlegroups.com
On 11/19/2011 10:26 PM, Gelonida N wrote:
> On 09/05/2011 12:58 AM, Gelonida N wrote:
>>
>> Im am debugging a django application.
>>
>> If I set DEBUG=True
>> then I can see error messages on the browser.
>>
>> Is there any way to see the same error messages in the server log file?

> I am using Django 1.3
> I configured logging and set up a root logger with log level 'DEBUG'
>
> I added one log command and one explicit error in the urls.py file.
> I can see the error report in the browser but not in my log files.
>

The only way, that was working for me was connecting myself to the
got_request_exception signal of the django framework.

so what I do now is:

import sys
import traceback
from django.dispatch import receiver
from django.core.signals import got_request_exception

import logging
logger = logging.getLogger('exceptionlogger')

@receiver(got_request_exception)
def got_request_exception_hndlr(signal, **kwargs):
request = kwargs.get('request')
meta = request.META
logger.error("ReqException %s %s" %
(meta['REMOTE_ADDR'], request.path ))
ex_type, ex_value, _e_b = sys.exc_info()
logger.error(traceback.format_exc())

This code has of course to be imported as soon as possible in order to
catch as many exceptions as possible.

What would be the correct place for it?

- First line of urls.py?
- import it as dummy middleware?

Reply all
Reply to author
Forward
0 new messages