rus*_*ano 5 logging google-cloud-platform google-cloud-logging google-cloud-functions
我是第一次使用Google Cloud Platform,还请大家谅解!
我已经构建了一个仅运行批处理作业的预定工作流程。该作业运行 Python 代码并使用标准logging库进行日志记录。执行作业时,我可以正确地看到 Cloud Logging 中的所有条目,但所有条目都具有严重错误,尽管它们都是 INFO。
我一直在考虑的一个可能原因是我没有使用此处setup_logging文档中描述的函数。问题是,我不想在本地运行代码时运行 Cloud Logging 设置。
我的问题是:
setup_logging?它的真正作用是什么?根据文档,您必须使用设置才能将日志正确发送到Cloud Logging.
此设置允许使用Python日志记录标准库。
安装后,该库包含用于将 Python 的标准日志记录模块连接到 Logging 的日志记录处理程序,以及用于手动访问 Cloud Logging 的 API 客户端库。
# Imports the Cloud Logging client library
import google.cloud.logging
# Instantiates a client
client = google.cloud.logging.Client()
# Retrieves a Cloud Logging handler based on the environment
# you're running in and integrates the handler with the
# Python logging module. By default this captures all logs
# at INFO level and higher
client.setup_logging()
Run Code Online (Sandbox Code Playgroud)
然后您可以使用Python标准库将日志添加到Cloud Logging.
# Imports Python standard library logging
import logging
# The data to log
text = "Hello, world!"
# Emits the data using the standard logging module
logging.warning(text)
Run Code Online (Sandbox Code Playgroud)
- 即使我没有使用 setup_logging 函数,为什么日志记录仍然“有效”(即日志最终出现在 Cloud Logging 中)?它的真正作用是什么?
如果没有设置,日志将被添加,Cloud Logging但不会以正确的类型和预期的方式添加。最好使用该设置。
- 为什么我的信息条目显示错误严重性?
与上面解释的原因相同
- 如果我包含该代码片段并且该代码片段解决了此问题,我是否应该在代码中包含一个 if 语句来检测我是否在本地运行代码并跳过 Cloud Logging 设置步骤?
我认为无需添加if本地运行代码的语句。在这种情况下,即使存在设置,日志也应该打印在控制台中。
| 归档时间: |
|
| 查看次数: |
2203 次 |
| 最近记录: |