Duplicate logs with "--access-log -" option
Sakoes opened this issue · 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.
For anyone wondering: there is a fix waiting to be merged #190
There is a manual fix in the issue @araki-yzrh pointed to #158 (comment)
I'll close this issue since there is a fix ready to go