I am watching how the registration module works in a funny way. Did I miss something?
I do the usual thing with two handlers: StreamHandler for logging only INFO and higher for the console and FileHandler, which will also handle all DEBUG information.
This worked until I decided that I had a different format for exceptions. I wanted to get the full stack in the file, but only the type of exception and value on the console. Since handlers have a setFormatter function, and since it seems easy to write a subclass of logging.Formatter, I thought it would work.
The console handler and file handler have their own formatter. Print statements in code prove this. However, when calling logger.exception, only the Exception format will be used for the first handler added => the exception is logged in a file with the format that it should have in the console. Reorder the line order of logger.addHandler and then the formatException of the file handler that is used everywhere.
import logging import sys class ConsoleFormatter(logging.Formatter): def formatException(self, exc_info):
What's happening?
EDIT: By the way, I'm using python 2.6. EDIT: Assign code in code with the variable name "console_formatter".
python logging
Niriel
source share