Skip to content

Add more context to the request logs#552

Merged
GianlucaFicarelli merged 2 commits intomainfrom
improve_logging
Mar 9, 2026
Merged

Add more context to the request logs#552
GianlucaFicarelli merged 2 commits intomainfrom
improve_logging

Conversation

@GianlucaFicarelli
Copy link
Collaborator

@GianlucaFicarelli GianlucaFicarelli commented Mar 9, 2026

Log in case of request_completed with success:

{
  "time": "2026-03-09T14:19:35.087574+01:00",
  "level": "INFO",
  "name": "app.middleware",
  "message": "request_completed",
  "extra": {
    "method": "GET",
    "url": "http://127.0.0.1:8000/cell-morphology",
    "route_template": "/cell-morphology",
    "status_code": 200,
    "status_class": 2,
    "process_time_ms": 530,
    "response_size": 80,
    "client": "127.0.0.1",
    "forwarded_for": "",
    "user_agent": "curl/8.7.1",
    "request_id": "9232ffab-e4b9-437c-84c8-109456a623d0",
    "user_id": "dcb8ee63-c8fd-4284-8ec3-a3b6ff3fab88"
  },
  "exception": null
}

Log in case of request_completed with validation error (user_id is missing because not resolved):

{
  "time": "2026-03-09T14:13:21.058340+01:00",
  "level": "INFO",
  "name": "app.middleware",
  "message": "request_completed",
  "extra": {
    "method": "GET",
    "url": "http://127.0.0.1:8000/cell-morphology/a7b7cc69-c5d4-4cc6-b902-9b848623d208?page_size=30&page=1&with_facets=true&order_by=-creation_date&within_brain_region_hierarchy_id=e3e70682-c209-4cac-a29f-6fbed82c07cd&within_brain_region_brain_region_id=5c60bf3e-5335-4971-a8ec-6597292452b2&within_brain_region_direction=ascendants_and_descendants&authorized_public=true",
    "route_template": "/cell-morphology/{id_}",
    "status_code": 422,
    "status_class": 4,
    "process_time_ms": 2,
    "response_size": 297,
    "client": "127.0.0.1",
    "forwarded_for": "",
    "user_agent": "curl/8.7.1",
    "request_id": "5b57df68-22f6-4bde-b550-e865c62ba320"
  },
  "exception": null
}

Log in case of request_failed (uncaught exception):

{
  "time": "2026-03-09T14:15:46.849294+01:00",
  "level": "ERROR",
  "name": "app.middleware",
  "message": "request_failed",
  "extra": {
    "method": "GET",
    "url": "http://127.0.0.1:8000/cell-morphology",
    "status_code": 500,
    "status_class": 5,
    "process_time_ms": 442,
    "client": "127.0.0.1",
    "forwarded_for": "",
    "user_agent": "curl/8.7.1",
    "request_id": "d6729a88-5f7e-4fba-909c-20c76194e9b4",
    "user_id": "dcb8ee63-c8fd-4284-8ec3-a3b6ff3fab88"
  },
  "exception": null
}

In this last case, the uncaught exception is logged by uvicorn in a separate message, with the same request_id:

{
    "time": "2026-03-09T14:15:46.862779+01:00",
    "level": "ERROR",
    "name": "uvicorn.protocols.http.httptools_impl",
    "message": "Exception in ASGI application\n",
    "extra": {
        "request_id": "d6729a88-5f7e-4fba-909c-20c76194e9b4",
        "user_id": "dcb8ee63-c8fd-4284-8ec3-a3b6ff3fab88",
    },
    "exception": {
        "type": "RuntimeError",
        "value": "test error",
        "traceback": "Traceback (most recent call last):..."
    },
}

@codecov
Copy link

codecov bot commented Mar 9, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

Flag Coverage Δ
pytest 97.91% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
app/middleware.py 100.00% <100.00%> (ø)
app/schemas/types.py 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@GianlucaFicarelli GianlucaFicarelli merged commit 6dd3695 into main Mar 9, 2026
2 checks passed
@GianlucaFicarelli GianlucaFicarelli deleted the improve_logging branch March 9, 2026 14:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants