I created an overriding formatter which masks some strings like this:
class MaskFormatter(logging.Formatter):
def __init__(self, fmt, mask):
logging.Formatter.__init__(self, fmt, mask)
self.mask = mask
def format(self, record):
result = logging.Formatter.format(self, record)
if result is not None and result.find(self.mask) != -1:
result = result.replace(self.mask, '*' * len(self.mask))
return result
I use it like this:
formatter = MaskFormatter('%(asctime)s %(levelname)s' +
' %(module)s %(lineno)d %(message)s', mask='abcde')
hdlr.setLevel(logging.DEBUG)
logger.setLevel(logging.DEBUG)
hdlr.setFormatter(formatter)
however in my log lines i noticed that the datetime is now also masked!! althought i asked it to mask only “abcde” can someone help please?
all log lines has prefix ***** instead of real datetime:
***** DEBUG mail
Looking at the documentation for logging formatters, it says that Formatter takes two optional arguments. The first is the message format, the second is the datetime format. It looks like you are sending the mask in as the datetime format. Try dropping the mask argument from the logging.Formatter.init call.