Skip to content

Commit

Permalink
quiet logs and add loggerMiddleware (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentsarago authored Oct 24, 2020
1 parent d0e8f95 commit 898bcaf
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lambda/handler.py
Original file line number Diff line number Diff line change
@@ -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")
10 changes: 9 additions & 1 deletion titiler/main.py
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -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"])
Expand Down
30 changes: 30 additions & 0 deletions titiler/middleware.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions titiler/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 898bcaf

Please sign in to comment.