What does exc_info do in a logging call?

R

Roy Smith

In http://docs.python.org/release/2.6.7/library/logging.html, it says:

logging.debug(msg[, *args[, **kwargs]])
[...]
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.

I don't get what this is trying to do. I have this code:

try:
f = urllib2.urlopen(url)
except urllib2.HTTPError as ex:
logger.error("Unable to retrieve profile from facebook
(access_token='%r')" % access_token,
exc_info=ex)

which ends up logging:

[...] ERROR _get_profile Unable to retrieve profile from facebook
(access_token='u'[token elided]'')

so what is the exc_info doing? What "exception information" is being
added to the message? Am I just calling this wrong?
 
P

Peter Otten

Roy said:
In http://docs.python.org/release/2.6.7/library/logging.html, it says:

logging.debug(msg[, *args[, **kwargs]])
[...]
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.

I don't get what this is trying to do. I have this code:

try:
f = urllib2.urlopen(url)
except urllib2.HTTPError as ex:
logger.error("Unable to retrieve profile from facebook
(access_token='%r')" % access_token,
exc_info=ex)

which ends up logging:

[...] ERROR _get_profile Unable to retrieve profile from facebook
(access_token='u'[token elided]'')

so what is the exc_info doing? What "exception information" is being
added to the message? Am I just calling this wrong?

I think whatever the formatter's formatException() makes out of the
(exception_type, exception_value, traceback) tuple lands in the logfile:
.... except: logging.error("yadda")
....
ERROR:root:yadda.... except: logging.error("yadda", exc_info=True)
....
ERROR:root:yadda
Traceback (most recent call last):
.... def formatException(self, *args): return "OOPS"
....
logging._handlers.keys()[0].setFormatter(Formatter()) # ;)
try: 1/0
.... except: logging.error("yadda", exc_info=True)
....
yadda
OOPS
 
R

Roy Smith

Duh, I figured out what's going on. I'm using a custom Formatter class, which overrides format(). It's the job of format() to handle this, and ours doesn't!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top