I am working on a simple Flask-RESTful API, which I am very new at, and have run into some problems with logging.
With export FLASK_ENV=development, the Werkzeug logger does not log any HTTP requests such as GET, POST, etc.
When export FLASK_ENV=production everything is normal, and a couple of simple logs can look like this:
2020-12-30 14:01:03,723 | INFO | werkzeug | 127.0.0.1 - - [30/Dec/2020 14:01:03] "GET /list/2 HTTP/1.1" 200 -
2020-12-30 14:01:03,742 | INFO | werkzeug | 127.0.0.1 - - [30/Dec/2020 14:01:03] "PUT /list/2 HTTP/1.1" 201 -
2020-12-30 14:01:03,749 | INFO | werkzeug | 127.0.0.1 - - [30/Dec/2020 14:01:03] "DELETE /list/2 HTTP/1.1" 204 -
The main problem probably comes from my logging.dictConfig in my logger.py, since using a simple logging.basicConfig the logs are created as normal. My logger.py looks like this:
import logging as log
from logger.config import dictConfig
dictConfig(
{
"version": 1,
"formatters": {
"precise": {
"format": "%(asctime)s | %(levelname)s | %(name)s | %(module)s | %(message)s"
},
"brief": {"format": "%(asctime)s | %(levelname)s | %(message)s"},
},
"handlers": {
"console": {
"class": "logging.StreamHandler",
"stream": "ext://sys.stdout",
"formatter": "brief",
"level": "WARNING",
},
"file": {
"class": "logging.handlers.RotatingFileHandler",
"formatter": "precise",
"filename": "logs/api.log",
"maxBytes": 1024 ** 2,
"backupCount": 1,
},
},
"root": {"level": "DEBUG", "handlers": ["console", "file"]},
}
)
In my __init__.py I import log, which means that I can create logs by using log.info() etc. in create_app() or other modules.
However, according to the Flask documentation(https://flask.palletsprojects.com/en/1.1.x/logging/), it might also be caused by the timing of my configuration:
Basic Configuration
When you want to configure logging for your project, you should do it as soon as possible when the program starts. Ifapp.loggeris accessed before logging is configured, it will add a default handler. If possible, configure logging before creating the application object.
And I did notice that Werkzeug logs something before any of my logs when export FLASK_ENV=development. I am just surprised this could be the case, since using logging.basicConfig to configure it, it works as a charm. And the first thing I do in __init__.py, is to import the logger; meaning I can't really do it any earlier..