如何在Google App Engine中打印?

时间:2017-08-02 15:14:45

标签: python google-app-engine

我在标准环境Google App Engine中运行一个非常简单的Flask python应用程序。我试图通过以下命令查看日志信息:gcloud app logs tail -s default

我尝试使用3种不同的方法进行打印:

  1. logging.info("Printing via Google App Engine Log")
  2. print("Printing via python print")
  3. app.logger.info("Printing via Flask Log")
  4. 可悲的是,他们都没有工作。

    那么如何在实时日志流中打印出行?

    由于

    修改:添加了我的代码部分

    from flask import Flask
    from flask import request
    
    import requests
    import json
    import logging
    
    app = Flask(__name__)
    
    @app.route("/", methods=["GET"])
    def webhook():
    
        app.logger.info("Printing via Flask Log")
        print("Printing via python print")
        logging.info("Printing via Google App Engine Log")
    
        if request.args.get("hub.mode") == "subscribe" and request.args.get("hub.challenge"):
            if not request.args.get("hub.verify_token") == VERIFICATION_TOKEN:
                return "Verification token mismatch", 403
            return request.args["hub.challenge"], 200
    
        return "Hello world", 200
    

    如果您认为显示.yaml文件等其他文件有帮助,请告诉我们。谢谢!

1 个答案:

答案 0 :(得分:3)

根据此Google Cloud Platform doc,以下是如何使用不同日志记录级别的示例:

import logging

import webapp2


class MainPage(webapp2.RequestHandler):
    def get(self):
        logging.debug('This is a debug message')
        logging.info('This is an info message')
        logging.warning('This is a warning message')
        logging.error('This is an error message')
        logging.critical('This is a critical message')

        try:
            raise ValueError('This is a sample value error.')
        except ValueError:
            logging.exception('A example exception log.')

        self.response.out.write('Logging example.')


app = webapp2.WSGIApplication([
    ('/', MainPage)
], debug=True)

1-在部署中,使用--verbosity标志:

gcloud app deploy --verbosity debug

2-运行gcloud app logs tail -s default

3-触发任何端点。在上面的例子中,任何GET调用都可以。