简体   繁体   中英

redirect flask logs to Gunicorn output

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM