views:

785

answers:

5

Using something like this:


try:
   #Something...
except BaseException, excep:
   logger = logging.getLogger("componet")
   logger.warning("something raised an exception: " + excep)
   logger.info("something raised an exception: " + excep)


I would rather not have it on the error-level cause in my special case it is not an error. At least not in that python-process.

+5  A: 

Try using Logger.exception.

Logger.exception() creates a log message similar to Logger.error(). The difference is that Logger.exception() dumps a stack trace along with it. Call this method only from an exception handler.

smink
+1  A: 

Use Logger.exception().

try:
   #Something...
except BaseException, excep:
   logger = logging.getLogger("component")
   logger.exception("something raised an exception")
giltay
A: 

It is fairly well explained here.

You are pretty close though. You have the option of just using the default with

logging.warning("something raised an exception: " + excep)

Or you can follow some of the examples on the linked page and get more sophisticated with multiple destinations and filter levels.

Joe Skora
That won't log the traceback, however; use logging.exception().
Thomas Wouters
A: 

In some cases, you might want to use the warnings library. You can have very fine-grained control over how your warnings are displayed.

S.Lott
+2  A: 

From The logging documentation:

There are two keyword arguments in kwargs which are inspected: exc_info which, if it does not evaluate as false, causes exception information to be added to the logging message. If an exception tuple (in the format returned by sys.exc_info()) is provided, it is used; otherwise, sys.exc_info() is called to get the exception information.

So do:

logger.warning("something raised an exception: " + excep,exc_info=True)
Douglas Leeder