How can I log outside of main Flask module?
<p>I have a Python Flask application, the entry file configures a logger on the app, like so:</p>

<pre><code>app = Flask(__name__)
handler = logging.StreamHandler(sys.stdout)
app.logger.addHandler(handler)
app.logger.setLevel(logging.DEBUG)
</code></pre>

<p>I then do a bunch of logging using </p>

<p><code>app.logger.debug("Log Message")</code></p>

<p>which works fine. However, I have a few API functions like:</p>

<pre><code>@app.route('/api/my-stuff', methods=['GET'])
def get_my_stuff():
    db_manager = get_manager()
    query = create_query(request.args)

    service = Service(db_manager, query)
    app.logger.debug("Req: {}".format(request.url))
</code></pre>

<p>What I would like to know is how can I do logging within that <code>Service</code> module/python class. Do I have to pass the app to it? That seems like a bad practice, but I don't know how to get a handle to the app.logger from outside of the main Flask file...</p>

<p>Even though this is a possible duplicate I want to write out a tiny bit of python logging knowledge. </p>

<p>DON'T pass loggers around. You can always access any given logger by <code>logging.getLogger(&lt;log name as string&gt;)</code>. By default it looks like* flask uses the name you provide to the <code>Flask</code> class. </p>

<p>So if your main module is called 'my_tool', you would want to do <code>logger = logging.getLogger('my_tool')</code>in the <code>Service</code> module. </p>

<p>To add onto that, I like to be explicit about naming my loggers and packages, so I would do <code>Flask('my_tool')</code>** and in other modules, have sub level loggers like. <code>logger = logging.getLogger('my_tool.services')</code> that all use the same root logger (and handlers).</p>

<p>* No experience, based off other answer. </p>

<p>** Again, don't use flask, dk if that is good practice</p>

<p><strong>Edit: Super simple stupid example</strong></p>

<p><strong>Main Flask app</strong></p>

<pre><code>import sys
import logging

import flask

from module2 import hi

app = flask.Flask('tester')

handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(logging.Formatter(
    '%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
app.logger.addHandler(handler)
app.logger.setLevel(logging.DEBUG)

@app.route("/index")
def index():
    app.logger.debug("TESTING!")
    hi()
    return "hi"

if __name__ == '__main__':
    app.run()
</code></pre>

<p><strong>module2</strong></p>

<pre><code>import logging

log = logging.getLogger('tester.sub')


def hi():
    log.warning('warning test')
</code></pre>

<p><strong>Outputs</strong></p>

<pre><code>127.0.0.1 - - [04/Oct/2016 20:08:29] "GET /index HTTP/1.1" 200 -
2016-10-04 20:08:29,098 - tester - DEBUG - TESTING!
2016-10-04 20:08:29,098 - tester.sub - WARNING - warning test
</code></pre>

<p><strong>Edit 2: Messing with subloggers</strong>  </p>

<p>Totally unneeded, just for general knowledge. </p>

<p>By defining a child logger, done by adding a <code>.something</code> after the root logger name in <code>logging.getLogger('root.something')</code> it gives you basiclly a different namespace to work with. </p>

<p>I personally like using it to group functionality in logging. So have some <code>.tool</code> or <code>.db</code> to know what type of code is logging. But it also allows so that those child loggers can have their own handlers. So if you only want some of your code to print to <code>stderr</code>, or to a log you can do so. Here is an example with a modified <code>module2</code>.</p>

<p><strong>module2</strong> </p>

<pre><code>import logging
import sys

log = logging.getLogger('tester.sub')
handler = logging.StreamHandler(sys.stderr)
handler.setFormatter(logging.Formatter('%(name)s - %(levelname)s - %(message)s'))
log.addHandler(handler)
log.setLevel(logging.INFO)


def hi():
    log.warning("test")
</code></pre>

<p>Output</p>

<pre><code>127.0.0.1 - - [04/Oct/2016 20:23:18] "GET /index HTTP/1.1" 200 -
2016-10-04 20:23:18,354 - tester - DEBUG - TESTING!
tester.sub - WARNING - test
2016-10-04 20:23:18,354 - tester.sub - WARNING - test
</code></pre>

python logging flask