Tweak logging

This commit is contained in:
Thomas Sileo 2022-07-15 13:05:08 +02:00
parent 6fe51ecff2
commit 12f09743fd
2 changed files with 9 additions and 3 deletions

View file

@ -70,6 +70,7 @@ def _body_digest(body: bytes) -> str:
async def _get_public_key(db_session: AsyncSession, key_id: str) -> Key: async def _get_public_key(db_session: AsyncSession, key_id: str) -> Key:
if cached_key := _KEY_CACHE.get(key_id): if cached_key := _KEY_CACHE.get(key_id):
logger.info(f"Key {key_id} found in cache")
return cached_key return cached_key
# Check if the key belongs to an actor already in DB # Check if the key belongs to an actor already in DB

View file

@ -32,9 +32,11 @@ from sqlalchemy import func
from sqlalchemy import select from sqlalchemy import select
from sqlalchemy.orm import joinedload from sqlalchemy.orm import joinedload
from starlette.background import BackgroundTask from starlette.background import BackgroundTask
from starlette.datastructures import Headers
from starlette.datastructures import MutableHeaders from starlette.datastructures import MutableHeaders
from starlette.responses import JSONResponse from starlette.responses import JSONResponse
from starlette.types import Message from starlette.types import Message
from uvicorn.middleware.proxy_headers import ProxyHeadersMiddleware # type: ignore
from app import activitypub as ap from app import activitypub as ap
from app import admin from app import admin
@ -143,9 +145,13 @@ class CustomMiddleware:
server_host, server_port = scope["server"] # type: ignore server_host, server_port = scope["server"] # type: ignore
request_method = scope["method"] request_method = scope["method"]
request_path = scope["path"] request_path = scope["path"]
headers = Headers(raw=scope["headers"]) # type: ignore
user_agent = headers.get("user-agent")
logger.info( logger.info(
f"{client_host}:{client_port} - " f"{client_host}:{client_port} - "
f"{request_method} {scheme}://{server_host}:{server_port}{request_path}" f"{request_method} "
f"{scheme}://{server_host}:{server_port}{request_path} - "
f'"{user_agent}"'
) )
try: try:
await self.app(scope, receive, send_wrapper) # type: ignore await self.app(scope, receive, send_wrapper) # type: ignore
@ -165,6 +171,7 @@ app.include_router(admin.router, prefix="/admin")
app.include_router(admin.unauthenticated_router, prefix="/admin") app.include_router(admin.unauthenticated_router, prefix="/admin")
app.include_router(indieauth.router) app.include_router(indieauth.router)
app.include_router(webmentions.router) app.include_router(webmentions.router)
app.add_middleware(ProxyHeadersMiddleware)
app.add_middleware(CustomMiddleware) app.add_middleware(CustomMiddleware)
logger.configure(extra={"request_id": "no_req_id"}) logger.configure(extra={"request_id": "no_req_id"})
@ -728,8 +735,6 @@ async def post_remote_follow(
@app.get("/.well-known/webfinger") @app.get("/.well-known/webfinger")
async def wellknown_webfinger(resource: str) -> JSONResponse: async def wellknown_webfinger(resource: str) -> JSONResponse:
"""Exposes/servers WebFinger data.""" """Exposes/servers WebFinger data."""
omg = f"acct:{USERNAME}@{DOMAIN}"
logger.info(f"{resource == omg}/{resource}/{omg}/{len(resource)}/{len(omg)}")
if resource not in [f"acct:{USERNAME}@{DOMAIN}", ID]: if resource not in [f"acct:{USERNAME}@{DOMAIN}", ID]:
logger.info(f"Got invalid req for {resource}") logger.info(f"Got invalid req for {resource}")
raise HTTPException(status_code=404) raise HTTPException(status_code=404)