gpt4 book ai didi

google-app-engine - GAE - Python 3.7 - 如何记录?

转载 作者:太空宇宙 更新时间:2023-11-03 15:26:46 25 4
gpt4 key购买 nike

我在 python 3.7 中有一个谷歌应用程序引擎项目,我想在其中编写一些日志。我习惯在 app engine python 2.7 中编程,我使用的是简单的代码:

 logging.info('hi there!')

将任何日志写入谷歌云日志控制台。上面的命令现在不再起作用,它说:

logging has no attribute 'info'

我搜索并找到了这个可能的新代码

from flask import Flask
from google.cloud import logging

app = Flask(__name__)

@app.route('/l')
def hello():
logging_client = logging.Client()
log_name = LOG_NAME
logger = logging_client.logger(LOG_NAME)
text = 'Hello, world!'
logger.log_text(text, severity='CRITICAL')
return text

上面的代码不会在堆栈驱动程序报告页面中给出任何错误,但它在日志页面中什么也不会显示。

那么如何在 python3.7 中为我的应用引擎项目编写日志?

最佳答案

第二代标准环境(包括 python 3.7)恕我直言,比第一代标准环境(包括 python 2.7)更接近柔性环境。

许多具有第一代定制版本(由 GAE 团队维护)的 API 没有(或至少还没有)移植到第二代,如果相应的功能或多或少被替代的、更通用的覆盖的话方法,已经在灵活的环境中使用(其中大部分基于由 GAE 以外的团队开发和维护的服务)。

您会注意到这 2 个迁移指南中许多服务部分之间的相似性(这使我得出了上述总结结论):

日志记录是两个指南中列出的服务之一。第一代使用标准 python logging 库的自定义版本(在 Stackdriver 成为独立服务之前)。对于第二代日志记录,只需委托(delegate)使用现在普遍可用的 Stackdriver logging服务(这是您显示的代码段的来源)。来自日志记录(在第一指南中):

Request logs are no longer automatically correlated but will still appear in Stackdriver Logging. Use the Stackdriver Logging client libraries to implement your desired logging behavior.

您显示的代码片段确实对应于 Stackdriver Logging。但是你好像是Using the client library直接地。我不知道这是否是一个问题(GAE 通常有点不同),但也许你也可以尝试 using the standard Python logging相反:

To send all log entries to Stackdriver by attaching the Stackdriver Logging handler to the Python root logger, use the setup_logging helper method:

# 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()

Once the handler is attached, any logs at, by default, INFO level or higher which are emitted in your application will be sent to Stackdriver Logging:

# 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 的注释(但我不确定它们是否也涵盖了第二代标准环境):

Google App Engine grants the Logs Writer role by default.

The Stackdriver Logging library for Python can be used without needing to explicitly provide credentials.

Stackdriver Logging is automatically enabled for App Engine applications. No additional setup is required.

Note: Logs written to stdout and stderr are automatically sent to Stackdriver Logging for you, without needing to use Stackdriver Logging library for Python.

也许值得注意的是 viewing the logs在第一代标准 env 之外也可能会有所不同(其中应用程序日志将与请求日志完全相关)。

还有 Using Stackdriver Logging in App Engine apps指导。它没有特别提到第二代标准 env(因此它可能需要更新)但对灵活的环境有很好的提示,可能很有用。例如 Linking app logs and requests如果丢失的请求日志相关性与它有任何关系,部分可能会感兴趣。

关于google-app-engine - GAE - Python 3.7 - 如何记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53064007/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com