In order to be able to access the Flask API logs inside AWS CloudWatch Logs, I added the following configuration to Flask App:
from logging.config import dictConfig
# loggings
dictConfig( {
'version': 1,
'formatters': {'default': {
'format': '[%(asctime)s] %(levelname)s in %(module)s: %(message)s',
}},
'handlers': {
'wsgi': {
'class': 'logging.StreamHandler',
'stream': 'ext://sys.stdout',
'formatter': 'default'
}
},
'root': {
'level': 'INFO',
'handlers': ['wsgi']
}
} )
Flask App is deployed on an Nginx server. That worked fin on nginx until I added Gunicorn behind it because I couldn't make multiple requests at the same time:
gunicorn -b 0.0.0.0:5000 --workers=5 api:app
But then after adding Gunicorn, I no longer get the logs of the application in CloudWatch Logs and This is all I get:
[2018-10-04 12:48:25 +0000] [7] [INFO] Starting gunicorn 19.9.0
12:48:25
[2018-10-04 12:48:25 +0000] [7] [INFO] Listening at: http://0.0.0.0:5000 (7)
12:48:25
[2018-10-04 12:48:25 +0000] [7] [INFO] Using worker: sync
12:48:25
[2018-10-04 12:48:25 +0000] [10] [INFO] Booting worker with pid: 10
12:48:25
[2018-10-04 12:48:25 +0000] [11] [INFO] Booting worker with pid: 11
12:48:25
[2018-10-04 12:48:25 +0000] [12] [INFO] Booting worker with pid: 12
12:48:25
[2018-10-04 12:48:25 +0000] [13] [INFO] Booting worker with pid: 13
12:48:25
[2018-10-04 12:48:25 +0000] [14] [INFO] Booting worker with pid: 14
My question is: how to redirect the application logs to Gunicorn ?
https://medium.com/@trstringer/logging-flask-and-gunicorn-the-manageable-way-2e6f0b8beb2f
By Default, gunicorn logs are set at warning. So, you need to check and update the gunicorn level.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With