try: print blah except KeyError: traceback.print_exc()
I used to debug like this. I'd print to the console. Now, I want to log everything instead of print, since Apache doesn't allow printing. So, how do I log this entire traceback?
This is how I do it. try: do_something() except: # How can I log my exception here, complete with its traceback? import traceback traceback. format_exc() # this will print a complete trace to stout.
By default, the LOGGING setting is merged with Django's default logging configuration using the following scheme. If the disable_existing_loggers key in the LOGGING dictConfig is set to True (which is the dictConfig default if the key is missing) then all loggers from the default configuration will be disabled.
First Steps With Logging Begin by updating the app/views.py file with the following code: import logging from django. http import HttpResponse # This retrieves a Python logging instance (or creates it) logger = logging. getLogger(__name__) def index(request): # Send the Test!!
You can use python's logging mechanism:
import logging ... logger = logging.getLogger("blabla") ... try: print blah # You can use logger.debug("blah") instead of print except KeyError: logger.exception("An error occurred")
This will print the stack trace and will work with apache.
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