Saturday, 26 December 2020

What is the advantage of using Flasks app.logger?

I'm typically using the standard logging library or structlog. Recently, when I tried to add a test to a function, I've seen that it uses current_app.logger - which means that this function needed an application context. Otherwise, it didn't need the application context.

Is there an advantage of using the current_app.logger instead of logger = logging.getLogger(__name__)? Is there maybe data available that is not available to the standard logger?

MVCE

import logging
from flask import Flask, current_app

app = Flask(__name__)

# Normal Python logging
logger = logging.getLogger(__name__)
ch = logging.StreamHandler()
logger.addHandler(ch)
logger.setLevel(logging.DEBUG)

# Flasks app logger
app.logger.setLevel(logging.DEBUG)


@app.route("/")
def index():
    current_app.logger.info("flask-app logger info-msg")
    logger.info("base-logger infomsg")
    return "foo"


if __name__ == "__main__":
    app.run()


from What is the advantage of using Flasks app.logger?

No comments:

Post a Comment