Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get Django to print the debug information to the console?

Tags:

django

I'm using urlib to hit my app not a browser so I can't see the debug screen when an error occurs. What's the best way to send the normal debug info to the console or a file?

Edit: I'm already catching the output of the page but when I print that to the screen it's filled with tons of useless html. can I get just the error?

like image 581
Ryan Detzel Avatar asked Feb 28 '11 00:02

Ryan Detzel


People also ask

How do I use Django console?

LOGGING = { 'handlers' = { 'file': { 'level': 'DEBUG', 'class': 'logging. FileHandler', 'filename': 'mysite. log', 'formatter': 'verbose' }, 'console':{ 'level': 'DEBUG', 'class': 'logging. StreamHandler', }, }, (...) 'loggers'={ (...) 'page_processors': { 'handlers': ['console','file'], 'level': 'DEBUG', } } (...) }

Where does Django print to?

If you are using apache2 server to run django application and enabled access & error logs, your print statements will be printed in the error logs. mostly apache2 logs will be in this location /var/log/apache2/ .

How do I enable debug mode in Django?

The debug mode (DEBUG=True) is turned on by default in the Django framework. It provides a detailed traceback with the local variables to find out the error with the line numbers. The error can be triggered from the view page by setting the value of assert to False in the view file.

How do I access Django logs?

Open the settings.py file from the Django project folder and add the following content to define the logging information. The properties of the handlers and loggers are set here. According to the logging property values, DEBUG level logging information will be stored in a log file named djangoapp.


1 Answers

Update - 2016

This post is still getting hits.

I recommend using one of the approaches below that use the built in logging system.

In the same way exceptions are piped to the admin email handler, you can send the data to a console logger, file logger, or anywhere else (solely, or in addition to the email handler.)

Original answer

Well, the easiest way is to set debug mode OFF and let django email you the error, since that literally takes 5 seconds: http://docs.djangoproject.com/en/dev/howto/error-reporting/

Otherwise, I'd write a middleware that logs the exception, but it's a little more of a pain if you want the stack trace. Here's the documentation.

import traceback import sys from __future__ import print_function  class ProcessExceptionMiddleware(object):     def process_exception(self, request, exception):         # Just print the exception object to stdout         print(exception)          # Print the familiar Python-style traceback to stderr         traceback.print_exc()          # Write the traceback to a file or similar         myfile.write(''.join(traceback.format_exception(*sys.exc_info()))) 
like image 110
Yuji 'Tomita' Tomita Avatar answered Oct 24 '22 09:10

Yuji 'Tomita' Tomita