Python logging - using filters to remove information -
i trying remove information while logging console keep information while logging file
here's 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 see on console is
one 2
and in log file
test 2
but it's just
two
i'm assuming editing logrecord what's causing this. there way of achieving want or i'm trying not possible in way?
i think need logging.formatter
:
class myformatter(logging.formatter): def format(self, record): return record.msg.replace("test", "") #... console.setformatter(myformatter()) #...
Comments
Post a Comment