Google Cloud Logging将所有Python logging.info调用分配为ERROR严重性。

huangapple go评论59阅读模式
英文:

Google Cloud Logging assigns ERROR severity to all Python logging.info calls

问题

It's the first time I use Google Cloud Platform, so please be understanding!

I've built a scheduled workflow that simply runs a Batch job. The job runs Python code and uses the standard logging library for logging. When the job is executed, I can correctly see all the entries in Cloud Logging, but all the entries have severity ERROR although they're all INFO.

Google Cloud Logging将所有Python logging.info调用分配为ERROR严重性。

One possible reason I've been thinking about is that I haven't used the setup_logging function as described in the documentation here. The thing is, I didn't want to run the Cloud Logging setup when I run the code locally.

The questions I have are:

  • why does logging "work" (in the sense that logs end up in Cloud Logging) even if I did not use the setup_logging function? What is its real role?

  • why do my INFO entries show up with ERROR severity?

  • if I include that snippet and that snippet solves this issue, should I include an if statement in my code that detects if I am running the code locally and skips that Cloud Logging setup step?

英文:

It's the first time I use Google Cloud Platform, so please be understanding!

I've built a scheduled workflow that simply runs a Batch job. The job runs Python code and uses the standard logging library for logging. When the job is executed, I can correctly see all the entries in Cloud Logging, but all the entries have severity ERROR although they're all INFO.

Google Cloud Logging将所有Python logging.info调用分配为ERROR严重性。

One possible reason I've been thinking about is that I haven't used the setup_logging function as described in the documentation here. The thing is, I didn't want to run the Cloud Logging setup when I run the code locally.

The questions I have are:

  • why does logging "work" (in the sense that logs end up in Cloud Logging) even if I did not use the setup_logging function? What is it's real role?

  • why do my INFO entries show up with ERROR severity?

  • if I include that snippet and that snippet solves this issue, should I include an if statement in my code that detects if I am running the code locally and skips that Cloud Logging setup step?

答案1

得分: 3

根据文档,您需要进行设置以将日志正确发送到Cloud Logging

这个设置允许使用Python标准库的日志记录。

> 安装后,此库包括日志处理程序,用于连接Python的标准日志模块到Logging,以及一个API客户端库,用于手动访问Cloud Logging。

# 导入Cloud Logging客户端库
import google.cloud.logging

# 实例化客户端
client = google.cloud.logging.Client()

# 根据运行环境检索Cloud Logging处理程序,然后将处理程序与Python日志模块集成。
# 默认情况下,这会捕获INFO级别及更高级别的所有日志。
client.setup_logging()

然后,您可以使用Python标准库将日志添加到Cloud Logging

# 导入Python标准库日志
import logging

# 要记录的数据
text = "Hello, world!"

# 使用标准日志模块发出数据
logging.warning(text)

> - 即使没有使用setup_logging函数,为什么日志“起作用”(指的是日志最终会出现在Cloud Logging中)?它的真正作用是什么?

没有设置时,日志将会被添加到Cloud Logging,但不会以正确的类型和预期方式出现。最好使用设置。

> - 为什么我的INFO条目显示为ERROR严重性?

与上面解释的原因相同。

> - 如果我包含那段代码并且该代码段解决了此问题,是否应在我的代码中包含一个if语句,以检测是否正在本地运行代码并跳过Cloud Logging设置步骤?

我认为在本地运行代码时无需添加if语句。在这种情况下,即使设置存在,日志应该会打印在控制台上。

英文:

According to the documentation, you have to use a setup to send correctly logs to Cloud Logging.

This setup allows then to use the Python logging standard library.

> Once installed, this library includes logging handlers to connect
> Python's standard logging module to Logging, as well as an API client
> library to access Cloud Logging manually.

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

Then you can use the Python standard library to add logs to 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)

> - why does logging "work" (in the sense that logs end up in Cloud Logging) even if I did not use the setup_logging function? What is
> it's real role?

Without the setup, the log will be added to Cloud Logging but not with the correct type and as expected. It's better to use the setup.

> - why do my INFO entries show up with ERROR severity?

The same reason explained above

> - if I include that snippet and that snippet solves this issue, should I include an if statement in my code that detects if I am running the
> code locally and skips that Cloud Logging setup step?

I think no need to add a if statement you run the code locally. In this case, the logs should be printed in the console even if the setup is present.

huangapple
  • 本文由 发表于 2023年2月6日 20:44:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75361466.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定