Search code examples
google-cloud-platformloggingcloud

Cloud Logging in GCP - basic example


I tried to log my message to google clog logs using the standard gcloud logs library code. It is throwoing an error message - "ImportError: cannot import name 'logging' from 'google.cloud' (unknown location)". Please advise where I am doing wrong?

# Imports the Google Cloud client library
from google.cloud import logging

# Instantiates a client
logging_client = logging.Client()

# The name of the log to write to
log_name = "EN-LOG"
# Selects the log to write to
logger = logging_client.logger(log_name)

# The data to log
text = "My test message"

# Writes the log entry
logger.log_text(text)

I am expecting the log to be listed in the google clod logs explorer under the log name specified in the script "EN-LOG". But it throwing an error when I try to run the script. "ImportError: cannot import name 'logging' from 'google.cloud' (unknown location)". Please advise where I am doing wrong?


Solution

  • maybe try "import google.cloud.logging"

    https://cloud.google.com/logging/docs/setup/python#write_logs_with_the_standard_python_logging_handler