Skip to content

update logging middleware with more request details #1107

@hrodmn

Description

@hrodmn

We could include more info about the route, path parameters, query parameters, etc like we recently added to the maap-eoapi deployment in MAAP-Project/maap-eoapi#49

class LoggerMiddleware:
"""MiddleWare to add logging."""
def __init__(
self,
app: ASGIApp,
querystrings: bool = False,
headers: bool = False,
) -> None:
"""Init Middleware.
Args:
app (ASGIApp): starlette/FastAPI application.
"""
self.app = app
self.querystrings = querystrings
self.headers = headers
self.logger = logger
logger.setLevel(logging.DEBUG)
async def __call__(self, scope: Scope, receive: Receive, send: Send):
"""Handle call."""
if scope["type"] == "http":
request = Request(scope)
self.logger.debug(str(request.url))
qs = dict(request.query_params)
if qs and self.querystrings:
self.logger.debug(qs)
if self.headers:
self.logger.debug(dict(request.headers))
await self.app(scope, receive, send)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions