From 4fbd52ce5a4a781f1d426753663bf8e533b4ec98 Mon Sep 17 00:00:00 2001 From: Wrench56 Date: Mon, 8 Jul 2024 05:48:49 -0400 Subject: [PATCH] Filter out `/ping` logs --- src/backend/server/endpoint_filter.py | 18 ++++++++++++++++++ src/backend/server/main.py | 5 +++++ 2 files changed, 23 insertions(+) create mode 100644 src/backend/server/endpoint_filter.py diff --git a/src/backend/server/endpoint_filter.py b/src/backend/server/endpoint_filter.py new file mode 100644 index 0000000..ede47bf --- /dev/null +++ b/src/backend/server/endpoint_filter.py @@ -0,0 +1,18 @@ +from typing import Any + +import logging + + +# Source: https://github.com/encode/starlette/issues/864 +class EndpointFilter(logging.Filter): + def __init__( + self, + path: str, + *args: Any, + **kwargs: Any, + ): + super().__init__(*args, **kwargs) + self._path = path + + def filter(self, record: logging.LogRecord) -> bool: + return record.getMessage().find(self._path) == -1 diff --git a/src/backend/server/main.py b/src/backend/server/main.py index 94069ea..0f37958 100644 --- a/src/backend/server/main.py +++ b/src/backend/server/main.py @@ -12,6 +12,7 @@ from db import users from plugins import downloader, handler from server import build +from server.endpoint_filter import EndpointFilter from utils import config, const, motd, settings, status database = users.Database() @@ -19,6 +20,10 @@ app = FastAPI() app.mount('/assets', StaticFiles(directory='../public/assets'), name='static') +# Ignore /ping logs +uvicorn_logger = logging.getLogger('uvicorn.access') +uvicorn_logger.addFilter(EndpointFilter(path='/ping')) + @app.get('/favicon.png', include_in_schema=False) @app.get('/favicon.ico', include_in_schema=False)