Open irisk29 opened 8 months ago
Hey @irisk29.
It's likely an issue with the different process workers invoked by FastAPI. Each time a worker is spawned, the logger
is re-configured (because spawning a new process cause the whole Python script to execute again). That implies two processes try to write and rotate the same file in parallel, which cause problems such as the one you faced.
There is no built-in solution for that issue yet. Ideally, we would need to use enqueue=True
to synchronize logs across processes, but FastAPI does not provide a way to make worker processes inherit objects such as the logger
from the main process.
The safe solution for now it to use one different file per worker.
Hello, I'm trying to implement a simple use case where I log into the stdout and into a file in a FastApi application. The following code:
creates a log file, but after 30 seconds, when it is time to rotate the file and rename the old one with a date in its name, I encounter with
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process out-1.log -> out-1.2024-03-14_21-49-52_733426.log
but if I define the file name like this:
the access and rotation works just fine (although it creates 2 files on startup instead of one)
What am I doing wrong?