Skip to content

Is there a way not to log body if it's binary data? #2968

Answered by peterschutt
vkuznetsovgn asked this question in Q&A
Discussion options

You must be logged in to vote

This is not tested, but should work - subclass LoggingMiddleware and inspect/manipulate the data to be logged from there, e.g.:

from typing import Any

from litestar.middleware import LoggingMiddleware, LoggingMiddlewareConfig
from litestar.types import Scope


class MyLoggingMiddleware(LoggingMiddleware):

    def should_remove_body(body: Any) -> bool:
        # custom logic here

    def extract_response_data(scope: Scope) -> dict[str, Any]:
        data = super().response_data(scope)
        if not self.should_remove_body(data.get("body", None)):
            del data["body"]
        return data


config = LoggingMiddlewareConfig(..., middleware_class=MyLoggingMiddleware)

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by vkuznetsovgn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants