【问题标题】:Log requests to a flask server using gunicorn as wsgi server ... to AWS cloudwatch使用 gunicorn 作为 wsgi 服务器将请求记录到烧瓶服务器......到 AWS cloudwatch
【发布时间】:2017-11-13 19:09:59
【问题描述】:

我正在使用带有 gunicorn 的烧瓶服务器作为 wsgi 服务器。

我想将所有请求详细信息记录到 cloudwatch。

from flask import Flask, jsonify, request
app = Flask(__name__)

@app.route('/')
def index():
    return jsonify({
            'logging': "I want to log this request to cloudwatch", 
            "request": request
        })

if __name__=='__main__':
    app.run()

【问题讨论】:

    标签: python amazon-web-services gunicorn


    【解决方案1】:

    一种设置记录器和使用瞭望塔的方法

    https://watchtower.readthedocs.io/en/latest/#example-flask-logging-with-watchtower

    import watchtower, flask, logging
    
    logging.basicConfig(level=logging.INFO)
    app = flask.Flask("loggable")
    handler = watchtower.CloudWatchLogHandler()
    app.logger.addHandler(handler)
    logging.getLogger("werkzeug").addHandler(handler)
    
    @app.route('/')
    def index():
        logging.info("I want to log this request to cloudwatch")
        return jsonify({
            "request": request
        })
    
    if __name__ == '__main__':
        app.run()
    

    【讨论】:

    • logging.info("I want to log this request to cloudwatch") 当我尝试时,此行没有出现在 cloudwatch 中
    猜你喜欢
    • 2020-04-16
    • 1970-01-01
    • 1970-01-01
    • 2020-10-29
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多