import logging
import re

_PROBE_PATH = re.compile(
    r"^(?:/live|/ready|/metrics|/healthz|/health[^ ]*|/ping)/?(?:\?|$)"
)


class _ProbePathFilter(logging.Filter):
    def filter(self, record: logging.LogRecord) -> bool:
        request = getattr(record, "args", None)
        if isinstance(request, dict):
            path = request.get("U") or request.get("r", "")
        else:
            path = record.getMessage()
        return not _PROBE_PATH.search(path)


_filter = _ProbePathFilter()


def on_starting(server):
    logging.getLogger("gunicorn.access").addFilter(_filter)


def post_worker_init(worker):
    logging.getLogger("gunicorn.access").addFilter(_filter)
