Simulating logging.exception with another traceback

Joan Miller peloko45 at gmail.com
Tue Feb 9 05:06:52 EST 2010


On 8 feb, 21:13, Vinay Sajip <vinay_sa... at yahoo.co.uk> wrote:
> On Feb 7, 11:22 am, Joan Miller <pelok... at gmail.com> wrote:
>
>
>
> > 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 withlogging.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
>
> 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

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



More information about the Python-list mailing list