New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Design Question] How do gunicorn workers log correctly? #1272
Comments
Gunicorn master(arbiter) create workers use os.fork, os.fork will share one file write offset, so this is work ok. |
I believe there is no guarantee it won't be mangled. It's not likely to be a problem on most platform unless the log message is smaller than a block, I think, if my memory of kernel I/O serves me correctly. So, likely log messages < 4KB in length can never me mangled. If you have a need for something more and need these guarantees, you can use syslog or other logging support. Please open any PR if you have a good way to explain more in the documentation, otherwise I think we can close this issue if my understanding is correct. |
Sorry, I have some follow-up questions:
|
@mingzhaodotname I don't know if Maybe you can test it and/or provide a branch that enable it? If you do please open a new ticket/PR so anyone can track it :) |
Two interesting boundary conditions:
We have today seen that when the web application in a single gunicorn worker process uses a normal StreamHandler for emitting log messages then those can interleave with the gunicorn's worker process log message emission, at precisely the interface between two write() call invocations. That is, for a web application log message it can happen that it's not actually newline-terminated in stdout, but directly followed by a log message emitted by the gunicorn worker process. In this situation where there is just a single worker process and gunicorn and the web application use the same logging facility, seemingly even both use a StreamHandler -- there should be a straightforward way to prevent this from happening. How can we make both use the same StreamHandler object? |
@jgehrcke Did you solve the issue? I saw the same problem with my web application. Sometimes the log was output like this:
|
I am using a rotating file handler (python 2) like the code below, in a flask app: |
@aloknayak29 you shouldn't do that inside your application. To rotate logs you should send a USR1 signal to the arbiter. As for the log level it's advised to set it for now in the configuration and reload it when needed. If you want to discuss more about this topic please open a new ticket anyway as this one has been closed. |
@aloknayak29 that may be separate from the question for this thread. You may want to open another issue to discuss. |
@jgehrcke very good catch with the separate I am not sure there is a solution today other than to configure a logging handler other than I don't know whether Gunicorn should have its own network logging to the master process. That would be a big change, but if it can be done very simply it could be okay. |
Hello,
I have a simple question about how logging works with gunicorn, and I can't find an explicit answer documented anywhere online (nor is it obvious to me reading through gunicorn the source code).
How does gunicorn ensure that the output from its worker processes is not intermingled? How does it guarantee that log entries generated from its workers are written to gunicorn's access/error log stream atomically? Python's standard library logging module is thread safe but not process safe, so how can we have a guarantee that gunicorn's workers' log entries will not be mangled (e.g. worker process A starts writing a log entry to stdout, OS switches to worker process B halfway through and worker process B starts writing a log entry to stdout).
Taking this question a bit further, if I log to stdout from my WSGI app using a StreamHandler, is this completely disjoint from any logging that gunicorn does? Will this have the consequence of potentially clashing with gunicorn's logging, if I'm sending that to stdout as well? If so, what is the correct way to do "application logging" from the WSGI app (several instances of which may be running at once from the gunicorn workers)?
Please forgive me if I am missing something obvious or fundamental here; this question may not be gunicorn-specific but these questions arose when I was considering how to properly set up logging with my Flask app deployed using gunicorn.
The text was updated successfully, but these errors were encountered: