diff --git a/lambda/handler.py b/lambda/handler.py index 50e56d7ac..a7b262edf 100644 --- a/lambda/handler.py +++ b/lambda/handler.py @@ -1,7 +1,12 @@ """AWS Lambda handler.""" +import logging + from mangum import Mangum from titiler.main import app +logging.getLogger("mangum.lifespan").setLevel(logging.ERROR) +logging.getLogger("mangum.http").setLevel(logging.ERROR) + handler = Mangum(app, lifespan="auto", log_level="error") diff --git a/titiler/main.py b/titiler/main.py index f46dc3741..727c2e5fd 100644 --- a/titiler/main.py +++ b/titiler/main.py @@ -1,17 +1,23 @@ """titiler app.""" +import logging + from brotli_asgi import BrotliMiddleware from . import __version__ as titiler_version from . import settings from .endpoints import cog, mosaic, stac, tms from .errors import DEFAULT_STATUS_CODES, add_exception_handlers -from .middleware import CacheControlMiddleware, TotalTimeMiddleware +from .middleware import CacheControlMiddleware, LoggerMiddleware, TotalTimeMiddleware from fastapi import FastAPI from starlette.middleware.cors import CORSMiddleware +logging.getLogger("botocore.credentials").disabled = True +logging.getLogger("botocore.utils").disabled = True +logging.getLogger("rio-tiler").setLevel(logging.ERROR) + api_settings = settings.ApiSettings() app = FastAPI( @@ -40,6 +46,8 @@ app.add_middleware(BrotliMiddleware, minimum_size=0, gzip_fallback=True) app.add_middleware(CacheControlMiddleware, cachecontrol=api_settings.cachecontrol) app.add_middleware(TotalTimeMiddleware) +if api_settings.debug: + app.add_middleware(LoggerMiddleware) @app.get("/ping", description="Health Check", tags=["Health Check"]) diff --git a/titiler/middleware.py b/titiler/middleware.py index 78ad90836..d3c58416f 100644 --- a/titiler/middleware.py +++ b/titiler/middleware.py @@ -1,8 +1,11 @@ """Titiler middlewares.""" +import logging import time from typing import Optional +from fastapi.logger import logger + from starlette.middleware.base import BaseHTTPMiddleware from starlette.requests import Request from starlette.types import ASGIApp @@ -43,3 +46,30 @@ async def dispatch(self, request: Request, call_next): f"{timings}, {app_time}" if timings else app_time ) return response + + +class LoggerMiddleware(BaseHTTPMiddleware): + """MiddleWare to add logging.""" + + def __init__( + self, app: ASGIApp, querystrings: bool = False, headers: bool = False, + ) -> None: + """Init Middleware.""" + super().__init__(app) + self.logger = logger + logger.setLevel(logging.DEBUG) + + self.querystrings = querystrings + self.headers = headers + + async def dispatch(self, request: Request, call_next): + """Add logs.""" + 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)) + + response = await call_next(request) + return response diff --git a/titiler/settings.py b/titiler/settings.py index 1759990e0..65d35d514 100644 --- a/titiler/settings.py +++ b/titiler/settings.py @@ -9,6 +9,7 @@ class ApiSettings(pydantic.BaseSettings): name: str = "titiler" cors_origins: str = "*" cachecontrol: str = "public, max-age=3600" + debug: bool = False @pydantic.validator("cors_origins") def parse_cors_origin(cls, v):