Uvicorn workers logging Unable to write to their own file #2615
Unanswered
Jmandarino
asked this question in
Potential Issue
Replies: 1 comment
-
Played around and disappointed this process seems to contrived. TLDR: inside your application code if you don't set the logger manually uvicorn will have all the loggers log to the same file. def log_config():
from uvicorn.config import LOGGING_CONFIG
from copy import deepcopy
import os
config = deepcopy(LOGGING_CONFIG)
pid = os.getpid()
log_path = f"logs/app-{pid}.log"
os.makedirs("logs", exist_ok=True) # Ensure the logs folder exists
config["handlers"]["file"] = {
"level": "INFO",
"class": "logging.FileHandler",
"filename": log_path,
"formatter": "default",
}
config["loggers"]["uvicorn"] = {
"handlers": ["default", "file"],
"level": "INFO",
"propagate": False,
}
config["loggers"]["uvicorn.access"] = {
"handlers": ["access", "file"],
"level": "INFO",
"propagate": False,
}
return config
if __name__ == "__main__":
import uvicorn
uvicorn.run("app.main:app", workers=2, log_config=log_config()) this does not work -- if you do this you are left with one log file based off the main process. The only way I got this to work was using a startup event for FastAPI (which seems pretty odd to have to do given Gunicorn has support for each worker to have their own log) @app.on_event("startup")
def set_handlers():
import os
import logging
pid = os.getpid()
log_path = f"logs/app-{pid}.log"
os.makedirs("logs", exist_ok=True)
# Remove existing handlers (like console)
for handler in logging.root.handlers[:]:
logging.root.removeHandler(handler)
# Add new handler that writes to file
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
handlers=[
logging.FileHandler(log_path),
],
)
logging.getLogger().info(f"Worker PID {pid} logging to {log_path}") ADDing this (and the above code) creates a log per file but what a mess. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Doesn't seem to be discussed anywhere in the docs but I had a question on the potential log interweaving or corruption when using logfiles instead of stdout/stderr and having mulitple workers (2+)
Is it possible to have an issue with log file corruption while using mulitple workers in uvicorn? I know
logging
is thread safe in python but I didn't see any queue based handlers in the uvicorn code for logging.If this is a potential issue or logging is a blocking operation on a given file. I also don't see anywhere in the docs how I can set each child process to have its PID or process name as part of the log file to allow for separate files
Long story short is logging in uvicorn process safe and if its not how can I best make separate logs for each uvicorn worker
Kinda realizing this might not have enough details:
Implementation:
setup specifically Running uvicorn programmically (Docs.
Attempts at logging into mulitple files via uvicorn:
I have also tried to use root loggers, sending "uvicorn" both "uvicorn.access" to a file with a PID suffix. This nets all logs going to one of the files created but not the other.
Beta Was this translation helpful? Give feedback.
All reactions