Re: logging django errors also on the server
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?
>
>
> 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.
>
>
>
>
>
>
>
>
>
>
>
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home