I have a flask-based app. When I run it locally, I run it from the command line, but when I deploy it, I start it with gunicorn with multiple workers.
I want to use the logging
module to log to a file. The docs I've found for this are here and here.
I am confused over the correct way to use logging when my app may be launched with gunicorn. The docs address threading but assume I have control of the master process. Points of confusion:
Will logger = logging.getLogger('myapp')
return the same logger object in different gunicorn worker threads?
If I am attaching a logging FileHandler
to my logger in order to log to a file, how can I avoid doing this multiple times in the different workers?
My understanding - which may be wrong - is that if I just call logger.setLevel(logging.DEBUG)
, this will send messages via the root logger which may have a higher default logging level and may ignore debug messages, and so I also need to call logging.basicConfig(logging.DEBUG)
in order for my debug messages to get through. But the docs say not to call logging.basicConfig()
from a thread. How can I correctly set the root logging level when using gunicorn? Or do I not need to?