"""Keep lifecycle/status diagnostics without logging URL query credentials.""" import logging import re class QueryRedactingFormatter(logging.Formatter): def format(self, record): message = super().format(record) return re.sub(r'(https?://[^\s?"<>]+)\?[^\s"<>]+', r'\1?[redacted]', message) def configure_logging(): pattern = "%(asctime)s %(name)s %(levelname)s %(message)s" logging.basicConfig(level=logging.INFO, format=pattern) # Format after exception rendering too; preserve method, path and HTTP status. for handler in logging.getLogger().handlers: handler.setFormatter(QueryRedactingFormatter(pattern))