From ffdb12e80397e296d88ce7b5aad0c5534afbc439 Mon Sep 17 00:00:00 2001 From: Camie Kim Date: Tue, 16 Jun 2026 10:58:45 -0400 Subject: [PATCH 1/2] docs: Refactor logging to use standard library integration Per b/234303347 --- logging/samples/snippets/snippets.py | 53 ++++++++++++++++------------ 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/logging/samples/snippets/snippets.py b/logging/samples/snippets/snippets.py index f6c16d17e38..cc84f4c89fe 100644 --- a/logging/samples/snippets/snippets.py +++ b/logging/samples/snippets/snippets.py @@ -27,30 +27,37 @@ # [START logging_write_log_entry] -def write_entry(logger_name): - """Writes log entries to the given logger.""" - logging_client = logging.Client() - - # This log can be found in the Cloud Logging console under 'Custom Logs'. - logger = logging_client.logger(logger_name) - - # Make a simple text log - logger.log_text("Hello, world!") +import logging +import google.cloud.logging - # Simple text log with severity. - logger.log_text("Goodbye, world!", severity="WARNING") - - # Struct log. The struct can be any JSON-serializable dictionary. - logger.log_struct( - { - "name": "King Arthur", - "quest": "Find the Holy Grail", - "favorite_color": "Blue", - }, - severity="INFO", - ) - - print("Wrote logs to {}.".format(logger.name)) +def write_entry(logger_name): + """Writes log entries using the Python standard library integration.""" + # Instantiate Cloud Logging client + client = google.cloud.logging.Client() + + # Set up standard library integration + # Attaches the CloudLoggingHandler to the root logger. + # The 'name' parameter sets the destination log ID in Cloud Logging. + client.setup_logging(name=logger_name) + + # Simple text log + logging.info("Hello, world!") + + # Simple text log with severity + # Standard Python logging levels map to Cloud Logging severities. + logging.warning("Goodbye, world!") + + # Structured log (JSON payload) + # Use the 'json_fields' key in the 'extra' argument to log a dictionary. + # Data will appear as the jsonPayload in the Cloud Logging entry. + data_dict = { + "name": "King Arthur", + "quest": "Find the Holy Grail", + "favorite_color": "Blue", + } + logging.info("Quest details", extra={"json_fields": data_dict}) + + print(f"Wrote logs to {logger_name}.") # [END logging_write_log_entry] From c692707aace086feb4a4b2a78a8a504db1613d97 Mon Sep 17 00:00:00 2001 From: Camie Kim Date: Tue, 16 Jun 2026 11:23:46 -0400 Subject: [PATCH 2/2] docs: Update logging imports to use google.cloud.logging --- logging/samples/snippets/snippets.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/logging/samples/snippets/snippets.py b/logging/samples/snippets/snippets.py index cc84f4c89fe..d0e4f8dcd67 100644 --- a/logging/samples/snippets/snippets.py +++ b/logging/samples/snippets/snippets.py @@ -23,13 +23,11 @@ import argparse -from google.cloud import logging - +import google.cloud.logging -# [START logging_write_log_entry] import logging -import google.cloud.logging +# [START logging_write_log_entry] def write_entry(logger_name): """Writes log entries using the Python standard library integration.""" # Instantiate Cloud Logging client @@ -66,7 +64,7 @@ def write_entry(logger_name): # [START logging_list_log_entries] def list_entries(logger_name): """Lists the most recent entries for a given logger.""" - logging_client = logging.Client() + logging_client = google.cloud.logging.Client() logger = logging_client.logger(logger_name) print("Listing entries for logger {}:".format(logger.name)) @@ -85,7 +83,7 @@ def delete_logger(logger_name): Note that a deletion can take several minutes to take effect. """ - logging_client = logging.Client() + logging_client = google.cloud.logging.Client() logger = logging_client.logger(logger_name) logger.delete()