title: Where to put logging setup code in a flask app?
body: <p>I'm writing my first Flask application. The application itself runs fine. I just have a newbie question about logging in production mode. </p>
  
  <p>The basic structure:</p>
  
  <pre><code>app/
  app/templates/
  app/static
  config.py
  flask/... &lt;- virtual env with flask + extensions
  run.py
  </code></pre>
  
  <p>The application is started by <code>run.py</code> script:</p>
  
  <pre><code>#!flask/bin/python
  
  import os.path
  import sys
  
  appdir = os.path.dirname(os.path.abspath(__file__))
  if appdir not in sys.path:
      sys.path.insert(1, appdir)
  
  from app import app as application
  
  if __name__ == '__main__':
      application.run(debug=True)
  </code></pre>
  
  <p>and is started either directly or from an Apache 2.4 web server. I have these lines in the apache config:</p>
  
  <pre><code>WSGIPythonHome /usr/local/opt/app1/flask
  WSGIScriptAlias /app1 /usr/local/opt/app1/run.py
  </code></pre>
  
  <p>In the former case, the <code>debug=True</code> is all I need for the development.</p>
  
  <p>I'd like to have some logging also for the latter case, i.e. when running under Apache on a production server. Following is a recommendation from the Flask docs:</p>
  
  <pre><code>if not app.debug:
      import logging
      from themodule import TheHandlerYouWant
      file_handler = TheHandlerYouWant(...)
      file_handler.setLevel(logging.WARNING)
      app.logger.addHandler(file_handler)
  </code></pre>
  
  <p>It needs some customization, but that's what I want - instructions for the case when <code>app.debug</code> flag is not set. Similar recommendation was given also here:
  <a href="https://stackoverflow.com/questions/14037975/how-do-i-write-flasks-excellent-debug-log-message-to-a-file-in-production">How do I write Flask&#39;s excellent debug log message to a file in production?</a></p>
  
  <p>Please help: where do I have to put this code?</p>
  
  <hr>
  
  <p>UPDATE: based on the comments by davidism and the first answer I've got I think the app in the current simple form is not suitable for what I was asking for. I will modify it to use different sets of configuration data as recommended here: <a href="http://flask.pocoo.org/docs/0.10/config/#development-production" rel="nofollow noreferrer">http://flask.pocoo.org/docs/0.10/config/#development-production</a> . If my application were larger, I would follow the pech0rin's answer.</p>
  
  <p>UPDATE2: I think the key here is that the environment variables should control how the application is to be configured.</p>