A great way to process exceptions is to use Logger.exception
:
try:
do_something()
except BaseException:
logger.exception('some message')
... this not only prints out the user message, but also the exception message and exception type, and also a full stacktrace.
My logger here has two handlers: a FileHandler
and a StreamHandler
.
Using colorlog to colour my logger output according to the various levels, I can see that in the above case "some message" is printed according to the logger (level logging.ERROR
I believe, i.e. red)... but the stacktrace itself is printed in ordinary console colours. It could be stdout
, but more likely to be stderr
.
As it happens, I want this stacktrace in such cases to be printed out in full to my logger's file handler, but not to the stream handler which handles console output: stacktraces are fantastic for debugging purposes but by default the user shouldn't be exposed to them.
I looked at logging docs on StreamHandler, but couldn't see a way to suppress this stacktrace printout for a given such handler.