You can solve this problem using a custom Formatter
.
import logging
import re
class SensitiveFormatter(logging.Formatter):
"""Formatter that removes sensitive information in logs."""
@staticmethod
def _filter(s):
# Filter out the password with regex
# or replace etc.
# Replace here with your own regex..
return re.sub(r"ABCDEF", r"<MASKED>", s)
def format(self, record):
original = logging.Formatter.format(self, record) # call parent method
return self._filter(original)
Then, use it inside your handlers:
# Create the specific logger
mylogger = logging.getLogger("foobar")
mylogger.setLevel(logging.DEBUG)
mylogger.propagate = False
# Create the handler
streamhandler = logging.StreamHandler()
streamhandler.setLevel(logging.INFO)
# Create the specific formatter
sensitive_formatter = SensitiveFormatter(
fmt="[pid:%(process)d] - %(asctime)s - %(levelname)-8s - %(message).1000s"
)
streamhandler.setFormatter(sensitive_formatter)
mylogger.addHandler(streamhandler)
mylogger.info("This is a password: ABCDEF")
[pid:453381] - 2023-02-07 14:47:56,075 - INFO - This is a password: <MASKED>