hypercorn icon indicating copy to clipboard operation
hypercorn copied to clipboard

Duplicate logs with "--access-log -" option

Open Sakoes opened this issue 2 years ago • 1 comments

When running fastapi with hypercorn and the --access-log - option, requests get logged twice. I can't seem to pinpoint the exact issue.

Setup

app.py

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
    return {"Hello": "World"}

Dockerfile

FROM python:3.9-slim-buster

WORKDIR /app

COPY ./app /app

RUN pip install fastapi uvicorn hypercorn

EXPOSE 8000

CMD ["hypercorn", "main:app", "--bind", "0.0.0.0:8000", "--access-log", "-"]

Output

$ curl http://localhost:8000/
{"Hello":"World"}

Logs

The logs are almost identical. Notice the "200" status code missing from the second log entry.

fastapi_1  | [2024-02-09 15:44:41 +0000] [8] [INFO] Running on http://0.0.0.0:8000 (CTRL + C to quit)
fastapi_1  | [2024-02-09 15:44:43 +0000] [8] [INFO] 172.29.0.1:59956 - - [09/Feb/2024:15:44:43 +0000] "GET / 1.1" 200 17 "-" "curl/7.81.0"
fastapi_1  | [2024-02-09 15:44:43 +0000] [8] [INFO] 172.29.0.1:59956 - - [09/Feb/2024:15:44:43 +0000] "GET / 1.1" - - "-" "curl/7.81.0"

Notes

  • At first I thought it had to do with multiple loggers being configured incorrectly thus resulting in two loggers logging the same entry. After verifying with the setup above, this seems unlikely because there are no other loggers defined. Except maybe different loggers from hypercorn vs fastapi?
  • I read something about http being redirected to https, which could also result in duplicate logs. However I can't seem to find a lot of information about this in the docs.

Anybody any ideas on why requests are being logged twice? I am not sure if I am missing something in my setup or whether this is a bug.

Sakoes avatar Feb 09 '24 15:02 Sakoes

maybe its bug

see #158

araki-yzrh avatar Feb 15 '24 04:02 araki-yzrh

For anyone wondering: there is a fix waiting to be merged https://github.com/pgjones/hypercorn/pull/190 There is a manual fix in the issue @araki-yzrh pointed to https://github.com/pgjones/hypercorn/issues/158#issuecomment-1833873395

I'll close this issue since there is a fix ready to go

Sakoes avatar Mar 27 '24 09:03 Sakoes