title: How to use the logging module in Python with gunicorn
body: <p>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.</p>
  
  <p>I want to use the <code>logging</code> module to log to a file. The docs I've found for this are <a href="https://docs.python.org/3/library/logging.html" rel="noreferrer">https://docs.python.org/3/library/logging.html</a> and <a href="https://docs.python.org/3/howto/logging-cookbook.html" rel="noreferrer">https://docs.python.org/3/howto/logging-cookbook.html</a> .</p>
  
  <p>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:</p>
  
  <p>Will <code>logger = logging.getLogger('myapp')</code> return the same logger object in different gunicorn worker threads?</p>
  
  <p>If I am attaching a logging <code>FileHandler</code> to my logger in order to log to a file, how can I avoid doing this multiple times in the different workers?</p>
  
  <p>My understanding - which may be wrong - is that if I just call <code>logger.setLevel(logging.DEBUG)</code>, this will send messages via the root logger which may have a higher default logging level and may ignore debug messages, and so I <em>also</em> need to call <code>logging.basicConfig(logging.DEBUG)</code> in order for my debug messages to get through. But the docs say not to call <code>logging.basicConfig()</code> from a thread. How can I  correctly set the root logging level when using gunicorn? Or do I not need to?</p>