如何在 Google App Engine 中打印?

Bos*_*sen 2 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")

可悲的是,他们都没有工作。

那么如何打印实时日志流中的行呢?

谢谢


编辑:添加了我的代码的一部分

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
Run Code Online (Sandbox Code Playgroud)

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

小智 5

根据此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)
Run Code Online (Sandbox Code Playgroud)

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

gcloud app deploy --verbosity debug
Run Code Online (Sandbox Code Playgroud)

2-运行 gcloud app logs tail -s default

3- 触发任何端点。在上述情况下,任何 GET 调用都可以。