Skip to content

Commit d2712e7

Browse files
committed
feat: log static asset requests at debug level
- Introduced a new function, `is_static_assets_request`, to check if a request is for static assets like CSS or JS files. - Static asset requests are now logged at a debug level to prevent cluttering the logs with unimportant entries. Generated-by: aiautocommit
1 parent b25b26a commit d2712e7

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

app/routes/middleware/access_log.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,19 @@ def get_client_addr(scope: Scope) -> str:
7676
return f"{ip}:{port}"
7777

7878

79+
# TODO we should look at the static asset logic and pull the prefix path from tha
80+
def is_static_assets_request(scope: Scope) -> bool:
81+
"""Check if the request is for static assets. Pretty naive check.
82+
83+
Args:
84+
scope (Scope): Current context.
85+
86+
Returns:
87+
bool: True if the request is for static assets, False otherwise.
88+
"""
89+
return scope["path"].endswith(".css") or scope["path"].endswith(".js")
90+
91+
7992
def add_middleware(
8093
app: FastAPI,
8194
) -> None:
@@ -96,7 +109,10 @@ async def access_log_middleware(
96109
assert start
97110
elapsed = perf_counter() - start
98111

99-
log.info(
112+
# debug log all asset requests otherwise the logs because unreadable
113+
log_method = log.debug if is_static_assets_request(scope) else log.info
114+
115+
log_method(
100116
f"{response.status_code} {scope['method']} {get_path_with_query_string(scope)}",
101117
time=round(elapsed * 1000),
102118
status=response.status_code,

0 commit comments

Comments
 (0)