Member-only story
Python Web Development with Flask — Logging and Config
3 min readJan 27, 2021
Flask is a simple web framework written in Python.
In this article, we’ll look at how to develop simple Python web apps with Flask.
Logging
We can add logging into our Flask app.
For example, we can write:
from flask import Flask, abort
from logging.config import dictConfigdictConfig({
'version': 1,
'formatters': {'default': {
'format': '[%(asctime)s] %(levelname)s in %(module)s: %(message)s',
}},
'handlers': {'wsgi': {
'class': 'logging.StreamHandler',
'stream': 'ext://flask.logging.wsgi_errors_stream',
'formatter': 'default'
}},
'root': {
'level': 'INFO',
'handlers': ['wsgi']
}
})
app = Flask(__name__)@app.route('/')
def hello_world():
app.logger.info('success')
return 'hello'
to configure our logger and use it.
The dictConfig
function lets us configure our logger.
version
is an integer value representing the schema version.
formatters
has the dictionary top construct a Formatter
instance.
filters
is a dict which each key is a filter id and each value is a dict describing how to…