Skip to content

[Design Question] How do gunicorn workers log correctly? #1272

@bal2ag

Description

@bal2ag

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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions