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

1

9

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.

enter image description here

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?
Toratorah answered 6/2, 2023 at 12:44 Comment(0)
V
6

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.

Vickivickie answered 6/2, 2023 at 18:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.