Simulating logging.exception with another traceback

J

Joan Miller

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
 
V

Vinay Sajip

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
 
J

Joan Miller

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:
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?

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
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top