Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Simulating logging.exception with another traceback

2 views
Skip to first unread message

Joan Miller

unread,
Feb 7, 2010, 6:22:59 AM2/7/10
to
I would want to get the output from `logging.exception` but with
traceback from the caller function (I've already all that
information).

This would be the error with logging.exception:
--------------------
ERROR:
PipeError('/bin/ls -l | ', 'no command after of pipe')
Traceback (most recent call last):
File "/data/neo/Proyectos/Python/Scripy/lib/scripy/shell.py", line
160, in __call__
raise PipeError(command, 'no command after of pipe')
PipeError: ('/bin/ls -l | ', 'no command after of pipe')
--------------------

And I've trying it with:
--------------------
message = "File \"{0}\", line {1}, in {2}\n\n {3}".format(
file, line, function, caller)
logging.error(message)

ERROR:
File "/data/neo/Proyectos/Python/Scripy/lib/scripy/shell.py", line
163, in __call__ return self.throw(PipeError, command, 'no
command after of pipe')
--------------------

Could be used `logging.LogRecord` [1] to get it? How?


[1] http://docs.python.org/library/logging.html#logging.LogRecord

Vinay Sajip

unread,
Feb 8, 2010, 4:13:33 PM2/8/10
to

Sorry, Joan,

I don't understand your question. Can you create a short script which
throws an exception, and show exactly how you want it formatted?

The logger.exception method does the same as logger.error, except that
it prints exception trace information and is intended to be called
from exception handling clauses. You can format exceptions how you
like by subclassing Formatter and overriding formatException.

Regards,

Vinay Sajip

Joan Miller

unread,
Feb 9, 2010, 5:06:52 AM2/9/10
to

Hi!
I want to throw `logging.exception()` [1] but using the traceback of
caller's function, so:

---------
up=1
frame = traceback.extract_stack(limit=up+2)
---------

Reading the code, `traceback.print_exception` prints the message from
a record got with `LogRecord.getMessage()` [2] So I'm supposed that
I'd have to use it to get a new record that can be used by
`traceback.print_exception`.


[1] http://code.python.org/hg/branches/release2.6-maint-full/file/f5a05355fe48/Lib/logging/__init__.py#l408
[2] http://code.python.org/hg/branches/release2.6-maint-full/file/f5a05355fe48/Lib/logging/__init__.py#l423

0 new messages