Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure module logger to flask app logger

Tags:

python

flask

My flask application uses a module which gets a logger like this:

import logging
logger = logging.getLogger("XYZ")
...
logger.debug("stuff")

Without having to modify anything in the module, can I configure flask such that the module will get flask's app.logger when it makes the getLogger("XYZ") call?

like image 967
dcr Avatar asked Apr 08 '14 17:04

dcr


1 Answers

The Flask logger is accessible during a request or CLI command via current_app.

Example usage:

from flask import current_app, Flask
app = Flask('HelloWorldApp')


@app.route('/')
def hello():
    current_app.logger.info('Hello World!')
    return "Hello World!"


if __name__ == '__main__':
    app.run()
like image 51
smarlowucf Avatar answered Sep 30 '22 09:09

smarlowucf