Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use Django's logger to log a traceback when I tell it to?

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?

like image 862
TIMEX Avatar asked Jan 29 '11 22:01

TIMEX


People also ask

How do I log traceback exception in Python?

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.

How do I enable logs in Django?

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.

How do I add a logger to Django project?

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!!


1 Answers

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.

like image 140
Lin Avatar answered Sep 20 '22 16:09

Lin