I am trying to remove some information while logging to console but then keep that information while logging to a file
Here's a basic example:
import logging
import sys
class MyFilter(logging.Filter):
def filter(self, record):
record.msg = record.msg.replace("test", "")
return True
logger = logging.getLogger("mylogger")
logger.setLevel(logging.DEBUG)
console = logging.StreamHandler(sys.stdout)
console.setLevel("INFO")
logger.addHandler(console)
logfile = logging.FileHandler("log.txt", 'w')
logfile.setLevel("ERROR")
logger.addHandler(logfile)
filt = MyFilter()
console.addFilter(filt)
logger.info("test one")
logger.error("test two")
What I'd like to see on console is
one
two
and then in the log file
test two
but it's actually just
two
I'm assuming editing the LogRecord is what's causing this. Is there a way of achieving what I want or is what I'm trying to do not possible in this way?