恕我直言,第二代标准环境(包括 python 3.7)比第一代标准环境(包括 python 2.7)更接近灵活环境。
如果相应的功能或多或少被替代的、更通用的方法所覆盖,则许多具有第一代定制版本(由 GAE 团队维护)的 API 尚未(或至少尚未)移植到第二代中。在灵活的环境中使用(其中大多数基于由 GAE 以外的团队开发和维护的服务)。
您会注意到这两个迁移指南中的许多服务部分之间的相似性(这使我得出了上述总结结论):
- 了解 App Engine 标准环境中 Python 2 和 Python 3 之间的差异 https://cloud.google.com/appengine/docs/standard/python3/python-differences
-
将服务从标准环境迁移到灵活环境 https://cloud.google.com/appengine/docs/flexible/python/migrating(即第一代标准到灵活)
日志记录是两个指南中列出的服务之一。第一代使用了标准python的定制版本logging
库(在 Stackdriver 成为独立服务之前)。对于第二代,日志记录被简单地委托使用现在普遍可用的Stackdriver 日志记录 https://cloud.google.com/logging/docs/服务(这就是您显示的代码片段的来源)。来自日志记录(在第一个指南中):
请求日志不再自动关联,但仍会
显示在 Stackdriver Logging 中。使用 Stackdriver Logging 客户端
库来实现您所需的日志记录行为。
您显示的代码片段确实对应于 Stackdriver Logging。但你似乎是使用客户端库 https://cloud.google.com/logging/docs/reference/libraries#using_the_client_library直接地。我不知道这是否是一个问题(GAE 通常有点不同),但也许你也可以尝试使用标准 Python 日志记录 https://cloud.google.com/logging/docs/setup/python反而:
-
将库连接到 Python 日志记录 https://cloud.google.com/logging/docs/setup/python#connecting_the_library_to_python_logging:
通过附加 Stackdriver 将所有日志条目发送到 Stackdriver
将处理程序记录到 Python 根记录器,使用setup_logging
辅助方法:
# Imports the Google Cloud client library
import google.cloud.logging
# Instantiates a client
client = google.cloud.logging.Client()
# Connects the logger to the root logging handler; by default this captures
# all logs at INFO level and higher
client.setup_logging()
-
使用 Python 根记录器 https://cloud.google.com/logging/docs/setup/python#using_the_python_root_logger:
连接处理程序后,默认情况下,任何日志都会处于 INFO 级别或
您的应用程序中发出的更高的将被发送到
Stackdriver 日志记录:
# Imports Python standard library logging
import logging
# The data to log
text = 'Hello, world!'
# Emits the data using the standard logging module
logging.warn(text)
其中还有一些特定于 GAE 的注释(但我不确定它们是否也涵盖第二代标准环境):
谷歌应用引擎 https://cloud.google.com/appengine/docs/授予日志编写者角色 https://cloud.google.com/logging/docs/access-control#permissions_and_roles默认情况下。
无需使用 Python 的 Stackdriver Logging 库即可使用
明确提供凭据。
自动为 App Engine 启用 Stackdriver 日志记录
应用程序。无需额外设置。
Note:日志写入stdout and stderr会自动发送到 Stackdriver Logging,无需使用
适用于 Python 的 Stackdriver 日志库。
也许值得注意的是查看日志 https://cloud.google.com/logging/docs/view/overview在第一代标准环境之外也可能有所不同(其中应用程序日志将与请求日志紧密相关)。
还有在 App Engine 应用中使用 Stackdriver 日志记录 https://cloud.google.com/appengine/articles/logging指导。它没有具体提到第二代标准环境(因此可能需要更新),但对灵活的环境有很好的提示might有用的。例如链接应用程序日志和请求 https://cloud.google.com/appengine/articles/logging#linking_app_logs_and_requests部分might如果丢失的请求日志相关性与其有任何关系,那么我们会感兴趣。