Pao*_*177 4 logging google-app-engine python-3.7
我在python 3.7中有一个Google App Engine项目,我想在其中写一些日志。我习惯在App Engine python 2.7中编程,而我使用的是简单代码:
logging.info('hi there!')
Run Code Online (Sandbox Code Playgroud)
将任何日志写入Google云日志控制台。上面的命令现在不再起作用,它说:
logging has no attribute 'info'
Run Code Online (Sandbox Code Playgroud)
我搜索了,发现了这个可能的新代码
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
Run Code Online (Sandbox Code Playgroud)
上面的这段代码在堆栈驱动程序报告页面中没有给出任何错误,但是在日志页面中什么都没有显示。
那么,如何在python3.7中为我的App Engine项目编写日志?
恕我直言,第二代标准环境(包括python 3.7)比第一代标准环境(包括python 2.7)更接近于灵活环境。
如果已经(或至少尚未)在第二代中移植了许多第一代自定义版本的API(由GAE团队维护),但各自的功能或多或少已被替代的,更通用的方法所涵盖在灵活的环境中使用(大多数基于GAE以外的团队开发和维护的服务)。
您会在这两个迁移指南中注意到许多服务部分之间的相似之处(这使我得出了以上的总结结论):
日志记录是两个指南中列出的服务之一。第一代使用标准python logging库的自定义版本(在Stackdriver成为独立服务之前)。对于第二代,日志记录只是被委派给使用现在普遍可用的Stackdriver日志记录服务(这是您显示的摘录的来源)。从日志记录(在第一个指南中):
请求日志不再自动关联,但仍会显示在Stackdriver Logging中。使用Stackdriver Logging客户端库来实现所需的日志记录行为。
您显示的代码片段确实对应于Stackdriver Logging。但是您似乎正在直接使用客户端库。我不知道这是否有问题(GAE通常有所不同),但是也许您也可以尝试使用标准的Python日志记录:
要通过将Stackdriver Logging处理程序附加到Python根记录器,将所有日志条目发送到Stackdriver,请使用
setup_logginghelper方法:Run Code Online (Sandbox Code Playgroud)# 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()
附加处理程序后,默认情况下,在应用程序中发出的任何INFO级别或更高级别的日志都将发送到Stackdriver Logging:
Run Code Online (Sandbox Code Playgroud)# 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特定说明(但我不确定它们是否也涵盖第二代标准env):
默认情况下,Google App Engine授予Logs Writer角色。
可以使用Python的Stackdriver Logging库,而无需显式提供凭据。
自动为App Engine应用程序启用了Stackdriver Logging。无需其他设置。
注意:写入stdout和stderr的日志会自动为您发送到Stackdriver Logging,而无需使用Python的Stackdriver Logging库。
也许值得注意的是,在第一代标准环境(应用日志将与请求日志整齐相关)之外,查看日志的方式可能也会有所不同。
此外,还有《App Engine应用程序中的使用Stackdriver Logging》指南。它没有特别提到第二代标准环境(因此可能需要更新),但是对于灵活的环境有很好的提示,这可能会有用。例如,如果缺少的请求日志关联与此有关,则可能需要链接应用程序日志和请求部分。
尽管日志记录在 Python 2.7 和 3.7 中的工作方式不同,但在 Python 2.7 中读取和写入应用程序日志中提供的相同日志记录方法也应该适用于 Python 3.7,因为写入 stdout 和 stderr 的日志仍会出现在 Stackdriver Logging 中。
Import logging
logging.getLogger().setLevel(logging.DEBUG)
logging.debug('This is a debug message')
logging.getLogger().setLevel(logging.INFO)
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')
#logging.warn is deprecated
logging.warn('This is a warning' message)
=======================================
Import logging
app.logger.setLevel(logging.ERROR)
app.logger.error('This is an error message')
Run Code Online (Sandbox Code Playgroud)
但是,日志条目不再像 Python 2.7 中那样自动与请求关联,这就是您以纯文本形式看到它们的原因。我创建了一个功能请求来解决这个问题,您可以在此处关注。
| 归档时间: |
|
| 查看次数: |
2099 次 |
| 最近记录: |