Simulating logging.exception with another traceback

Discussion in 'Python' started by Joan Miller, Feb 7, 2010.

  1. Joan Miller

    Joan Miller Guest

    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
    Joan Miller, Feb 7, 2010
    #1
    1. Advertising

  2. Joan Miller

    Vinay Sajip Guest

    On Feb 7, 11:22 am, Joan Miller <> 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
    Vinay Sajip, Feb 8, 2010
    #2
    1. Advertising

  3. Joan Miller

    Joan Miller Guest

    On 8 feb, 21:13, Vinay Sajip <> wrote:
    > On Feb 7, 11:22 am, Joan Miller <> 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/...ile/f5a05355fe48/Lib/logging/__init__.py#l408
    [2] http://code.python.org/hg/branches/...ile/f5a05355fe48/Lib/logging/__init__.py#l423
    Joan Miller, Feb 9, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jan Decaluwe
    Replies:
    0
    Views:
    299
    Jan Decaluwe
    Nov 7, 2003
  2. Joe Peterson
    Replies:
    4
    Views:
    294
    Joe Peterson
    Jul 5, 2005
  3. Royan
    Replies:
    1
    Views:
    1,415
    Andrea Francia
    Apr 1, 2008
  4. Kottiyath

    Traceback in Logging

    Kottiyath, Jan 6, 2009, in forum: Python
    Replies:
    7
    Views:
    550
    Kottiyath
    Jan 8, 2009
  5. Jack Bates
    Replies:
    0
    Views:
    267
    Jack Bates
    May 2, 2011
Loading...

Share This Page