Created
December 13, 2025 12:40
-
-
Save gchait/be63ca62e3e88ed093acb2e7ab3ffe15 to your computer and use it in GitHub Desktop.
Hide an endpoint from Uvicorn access logs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class EndpointFilter(Filter): | |
| """A generic way to filter out web endpoints from the logs.""" | |
| # pylint: disable=too-few-public-methods | |
| def __init__( | |
| self, | |
| path: str, | |
| *args, | |
| **kwargs, | |
| ): | |
| super().__init__(*args, **kwargs) | |
| self._path = path | |
| def filter(self, record: LogRecord) -> bool: | |
| return record.getMessage().find(self._path) == -1 | |
| # Tap into the default uvicorn log stream and manipulate it | |
| logger = getLogger("uvicorn") | |
| uvicorn_access_logger = getLogger("uvicorn.access") | |
| uvicorn_access_logger.addFilter(EndpointFilter(path=HEALTHCHECK_URI)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment