Where to put logging setup code in a flask app?
Question: I'm writing my first Flask application. The application itself runs fine. I
just have a newbie question about logging in production mode.

The basic structure:

    
    
    app/
    app/templates/
    app/static
    config.py
    flask/... <- virtual env with flask + extensions
    run.py
    

The application is started by `run.py` script:

    
    
    #!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)
    

and is started either directly or from an Apache 2.4 web server. I have these
lines in the apache config:

    
    
    WSGIPythonHome /usr/local/opt/app1/flask
    WSGIScriptAlias /app1 /usr/local/opt/app1/run.py
    

In the former case, the `debug=True` is all I need for the development.

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:

    
    
    if not app.debug:
        import logging
        from themodule import TheHandlerYouWant
        file_handler = TheHandlerYouWant(...)
        file_handler.setLevel(logging.WARNING)
        app.logger.addHandler(file_handler)
    

It needs some customization, but that's what I want - instructions for the
case when `app.debug` flag is not set. Similar recommendation was given also
here: [How do I write Flask's excellent debug log message to a file in
production?](http://stackoverflow.com/questions/14037975/how-do-i-write-
flasks-excellent-debug-log-message-to-a-file-in-production)

Please help: where do I have to put this code?

* * *

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: <http://flask.pocoo.org/docs/0.10/config/#development-
production> . If my application were larger, I would follow the pech0rin's
answer.

UPDATE2: I think the key here is that the environment variables should control
how the application is to be configured.


Answer: I have had a lot of success with setting up my logging configurations inside a
`create_app` function. This uses the [application factory
pattern](http://flask.pocoo.org/docs/0.10/patterns/appfactories/). This allows
you to pass in some arguments or a configuration class. The application is
then specifically created using your parameters.

This allows you initialize the application, setup logging, and do whatever
else you want to do, before the application is sent back to be run.

For example:

    
    
    def create_app(dev=False):
       app = Flask(__name__)
       if dev:
          app.config['DEBUG'] = True
       else:
          ...
          app.logger.addHandler(file_handler)
       return app
    

This has worked very well for me in production environments. YMMV

