Overriding logging.Formatter in Python

I created an overriding formatter that masks some lines as follows:

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 as follows:

 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 journal lines, I noticed that the date-time is now also masked !! although I asked him to disguise only "abcde", can anyone help?

All log lines are prefixed with ***** instead of the actual datetime:

 ***** DEBUG mail 
+4
source share
1 answer

Looking at the documentation for registering formats, he says Formatter accepts two optional arguments. The first is the message format, the second is the date and time format. It looks like you are sending the mask in a date and time format. Try to drop the mask argument from the logging.Formatter call. init .

+3
source

Source: https://habr.com/ru/post/1397614/


All Articles