I define a root logger and handlers for it:
_root = logging.getLogger() _sh = logging.StreamHandler() _fh = logging.FileHandler('./error.log', delay = True) _root.addHandler(_sh) _root.addHandler(_fh)
And an instance of the logger module:
_log = logging.getLogger("Main") # In other file _log = logging.getLogger("Configuration")
Now I _log.exception in try..except block:
_log.exception("Test")
Now I get the trace in the console and file. I am trying to disable exc_info printing using the console handler, but not with the file handler:
class _TraceBackFilter(logging.Filter): def filter(self, rec): rec.exc_info = None return True _sh = logging.StreamHandler() _sh.addFilter(_TraceBackFilter()) _root.addHandler(_sh)
This works with both the file handler and the console, traceback is completely removed for all handlers, but I only need it for the console. Is there a way to suppress exc_info only for the console handler and not for the file handler?
EDIT:
I am _TraceBackFilter class.
class _TraceBackFilter(logging.Filter): def filter(self, rec): if rec.exc_info: logging.getLogger(rec.name).log(rec.levelno, rec.getMessage()) return False else: return True
Now it works for the console handler, but in the file I have double messages, one with one and one without tracing.
Galmi source share