When outputting the log without touching the app instance,
After referring to ʻapp.loggereven once, Get the logger with
logging.getLogger ("app ")`.
Basically, you can output the log with ʻapp.logger` like this.
app.py
from flask import Flask
app = Flask(__name__)
app.logger.warning("This is warning message.")
# => [2020-02-21 14:35:09,642] WARNING in app: This is warning message.
This works normally.
But I don't want to touch the app from sources other than main
Get the logger with logging.getLogger
.
I looked at the article that came out googled and did as follows.
index.py
import logging
log = logging.getLogger("flask.app")
log.warning("This is warning message.")
# => This is warning message.
No good. It is output, but nothing is formatted, just like print
.
Looking at the 1.1.x documentation, the specs have changed.
It seems that the logger will be registered with the same name as ʻapp.name. Since the main source file name is ʻapp.py
and the initialization is Flask (__ name__)
You should be able to get it with getLogger ("app ")
.
index.py
import logging
log = logging.getLogger("app")
log.warning("This is warning message.")
# => This is warning message.
For some reason this is no good either. The output was not formatted.
As a result of various tweaks, it worked this way.
app.py
from flask import Flask
app = Flask(__name__)
app.logger.warning("Log once appropriately")
index.py
import logging
log = logging.getLogger("app")
log.warning("This is warning message.")
# => [2020-02-21 14:35:09,642] WARNING in index: This is warning message.
Recommended Posts