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?
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', } } (...) }
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/ .
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.
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.
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.)
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())))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With