1

There are a lot of exception I don't want to catch, I want them to stop processing, but, I still want the stack trace in the log file. Is there a way to setup a logger so that it automatically logs all exceptions?

Right now I am writing things like this a lot:

try:
  blah
except:
  logging.exception('doing blah')
  raise

which take up a lot of time and space + it'll only log the exceptions for which I have a try clause for, not the other ones.

Thanks.

4

2 回答 2

1

You could use decorators to wrap individual functions and methods with code that logs any uncaught exception.

This means one additional line of code in front of any function definition that should do this logging. Advantage is: Can be quickly added and removed.

The decorator also could get arguments. For example you could specify the base class of those exceptions that you want to log.

Here's a basic question & concise answer on how to write decorators.

于 2012-07-05T08:34:03.570 回答
0

You could put a catch all handler at the topmost level of your code:

try:
  everything
except:
  logging.exception('unhandled')
  raise
于 2012-07-04T18:53:21.847 回答