-
-
Save phatnguyenuit/f7abdf8d14cbe4e50d791a69e4dbb6fd to your computer and use it in GitHub Desktop.
Example of flask blueprint and register logging to root logger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
from flask import Flask | |
from werkzeug.utils import find_modules, import_string | |
def configure_logging(): | |
# register root logging | |
logging.basicConfig(level=logging.DEBUG) | |
logging.getLogger('werkzeug').setLevel(logging.INFO) | |
def register_blueprints(app): | |
"""Automagically register all blueprint packages | |
Just take a look in the blueprints directory. | |
""" | |
for name in find_modules('blueprints', recursive=True): | |
mod = import_string(name) | |
if hasattr(mod, 'bp'): | |
app.register_blueprint(mod.bp) | |
return None | |
def create_app(): | |
app = Flask(__name__) | |
configure_logging() | |
register_blueprints(app) | |
return app | |
if __name__ == '__main__': | |
app = create_app() | |
app.run() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
from flask.blueprints import Blueprint | |
LOG = logging.getLogger(__name__) | |
bp = Blueprint("blog", __name__, url_prefix="/blog") | |
@bp.route("/") | |
def health(): | |
LOG.debug("Call health ok") | |
return "Ok" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment