I was addicted to trying logging.getLogger in Flask 1.1.x

Conclusion

When outputting the log without touching the app instance, After referring to ʻapp.loggereven once, Get the logger withlogging.getLogger ("app ")`.

Note

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

I was addicted to trying logging.getLogger in Flask 1.1.x
I was addicted to Flask on dotCloud
I was addicted to scraping with Selenium (+ Python) in 2020
I was addicted to multiprocessing + psycopg2
After implementing Watson IoT Platform application in Flask, I was addicted to MQTT connection
I was addicted to pip install mysqlclient
I was addicted to confusing class variables and instance variables in Python
What I was addicted to Python autorun
A story I was addicted to trying to install LightFM on Amazon Linux
The file name was bad in Python and I was addicted to import
I was addicted to trying Cython with PyCharm, so make a note
[Introduction to json] No, I was addicted to it. .. .. ♬
What I was addicted to when creating a web application in a windows environment
Docker x visualization didn't work and I was addicted to it, so I summarized it!
I want to transition with a button in flask
A story that I was addicted to at np.where
What I was addicted to when using Python tornado
Note that I was addicted to npm script not passing in the verification environment
[IOS] GIF animation with Pythonista3. I was addicted to it.
What I was addicted to when migrating Processing users to Python
[Fixed] I was addicted to alphanumeric judgment of Python strings
When I tried to install PIL and matplotlib in a virtualenv environment, I was addicted to it.
What I was addicted to when dealing with huge files in a Linux 32bit environment
The story I was addicted to when I specified nil as a function argument in Go
A story that I was addicted to calling Lambda from AWS Lambda.
When I tried to scrape using requests in python, I was addicted to SSLError, so a workaround memo
The record I was addicted to when putting MeCab on Heroku
I was addicted to deploying GoogleCloudFunctions ('ascii' codec can't encode character u'\ u281b' in position 58 appears)
What I was addicted to when introducing ALE to Vim for Python
A note I was addicted to when making a beep on Linux
Note that I was addicted to sklearn's missing value interpolation (Imputer)
A note I was addicted to when creating a table with SQLAlchemy
When I put Django in my home directory, I was addicted to static files with permission errors
Numpy's intellisense (input completion) is incomplete in VS Code and I was lightly addicted to the solution
Two things I was addicted to building Django + Apache + Nginx on Windows
I was addicted to creating a Python venv environment with VS Code
Use Python from Java with Jython. I was also addicted to it.
I was addicted to not being able to use Markdown on pypi's long_description
[Python] I was hooked for an hour trying to use list comprehensions
[Python] I was addicted to not saving internal variables of lambda expressions
A memorandum because I stumbled on trying to use MeCab in Python
I tried to implement PLSA in Python
I tried to implement permutation in Python
I tried to implement PLSA in Python 2
Minimum knowledge to use Form in Flask
I wanted to solve ABC159 in Python
I tried to implement PPO in Python
I want to embed Matplotlib in PySimpleGUI
Implemented DQN in TensorFlow (I wanted to ...)
I got an error when trying to run Hello World in Go language
Three things I was addicted to when using Python and MySQL with Docker
AtCoder AGC 041 C --I was addicted to the full search of Domino Quality
I'm addicted to the difference in how Flask and Django receive JSON data
Summary of points I was addicted to running Selenium on AWS Lambda (python)
A note I was addicted to when running Python with Visual Studio Code
A story that I was addicted to when I made SFTP communication with python
I set up TensowFlow and was addicted to it, so make a note
I want to do Dunnett's test in Python
I want to pin Datetime.now in Django tests
Try to separate Controllers using Blueprint in Flask
I want to create a window in Python