File size: 658 Bytes
c392a82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
"""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))