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.
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"
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,
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.
Will keep you informed when I manage to replicate the issue or when I
understand what went wrong.
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.
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)
> 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?