Delgan / loguru

Python logging made (stupidly) simple
MIT License
18.99k stars 681 forks source link

gunicorn fastapi use loguru #1111

Open fanmaoyu0871 opened 3 months ago

fanmaoyu0871 commented 3 months ago

I used this command to deploy the fastapi gunicorn main:app -b 0.0.0.0:12000 -w 4 -k uvicorn.workers.UvicornWorker --daemon ,Logoru was used to record HTTP requests and responses, but I found that some logs were not recorded,my loguru config:logger.add("logs/api.log", encoding="utf-8", catch=True, backtrace=True, diagnose=True, enqueue=True, rotation="00:00", retention="20 days", compression="zip"),is -w 4 or -k uvicorn.workers.UvicornWorker has some problems? use -w 1,it's likely work fine。

Delgan commented 3 months ago

Yes, this is because of the multiple processes started by FastAPI. They won't inherit the logger, therefore the enqueue=True parameter is unfortunately ineffective.

Right now, it's advised to use one unique file per worker, until a new workaround is implemented in Loguru.

fanmaoyu0871 commented 3 months ago

How to do it? Can you give me some ideas? Thank you

Delgan commented 3 months ago

Maybe these snippets can help you:

Lizidong01 commented 4 weeks ago

Are there any new developments on this issue?