diff --git a/logging/samples/snippets/requirements-test.txt b/logging/samples/snippets/requirements-test.txt index 79932f8353..a5a3777fcb 100644 --- a/logging/samples/snippets/requirements-test.txt +++ b/logging/samples/snippets/requirements-test.txt @@ -1,2 +1,2 @@ backoff==2.2.1 -pytest==9.0.3; python_version >= "3.10" +pytest==9.0.3 diff --git a/logging/samples/snippets/snippets.py b/logging/samples/snippets/snippets.py index f6c16d17e3..71a1e385e5 100644 --- a/logging/samples/snippets/snippets.py +++ b/logging/samples/snippets/snippets.py @@ -22,35 +22,39 @@ """ import argparse +import time -from google.cloud import logging +import google.cloud.logging + +import logging # [START logging_write_log_entry] -def write_entry(logger_name): - """Writes log entries to the given logger.""" - logging_client = logging.Client() +def write_entry(): + """Demonstrates how to write log entries to Google Cloud using Python's standard logging library.""" + logging_client = google.cloud.logging.Client() - # This log can be found in the Cloud Logging console under 'Custom Logs'. - logger = logging_client.logger(logger_name) + # By default, logs route to projects/[PROJECT_ID]/logs/python + # unless overridden by GCP or custom handlers. + logging_client.setup_logging(log_level=logging.INFO) # Make a simple text log - logger.log_text("Hello, world!") + logging.info("Hello, world!") # 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", - ) + logging.warning("Goodbye, world!") + + # Prepare your structured data as a dictionary. + json_log = { + "name": "King Arthur", + "quest": "Find the Holy Grail", + "favorite_color": "Blue", + } + + logging.info("This is a JSON log.", extra={"json_fields": json_log}) - print("Wrote logs to {}.".format(logger.name)) + # wait for threads to finish working on the background. + time.sleep(5) # [END logging_write_log_entry] @@ -105,6 +109,6 @@ def delete_logger(logger_name): if args.command == "list": list_entries(args.logger_name) elif args.command == "write": - write_entry(args.logger_name) + write_entry() elif args.command == "delete": delete_logger(args.logger_name) diff --git a/logging/samples/snippets/snippets_test.py b/logging/samples/snippets/snippets_test.py index 5cddc92d31..616ce424d4 100644 --- a/logging/samples/snippets/snippets_test.py +++ b/logging/samples/snippets/snippets_test.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import os import uuid import backoff @@ -21,9 +22,10 @@ import snippets - TEST_LOGGER_NAME = "example_log_{}".format(uuid.uuid4().hex) TEST_TEXT = "Hello, world." +GOOGLE_CLOUD_PROJECT = os.getenv("GOOGLE_CLOUD_PROJECT") +DEFAULT_LOGGER = f"projects/{GOOGLE_CLOUD_PROJECT}/logs/python" @pytest.fixture @@ -47,13 +49,24 @@ def eventually_consistent_test(): def test_write(capsys): - snippets.write_entry(TEST_LOGGER_NAME) + snippets.write_entry() @backoff.on_exception(backoff.expo, AssertionError, max_time=120) def eventually_consistent_test(): - snippets.list_entries(TEST_LOGGER_NAME) - out, _ = capsys.readouterr() - assert TEST_TEXT in out + # retrieve logs + client = logging.Client() + + log_filter = DEFAULT_LOGGER + + entries = client.list_entries( + filter_=log_filter, order_by=logging.DESCENDING, max_results=3 + ) + + retrieved_entries = list(entries) + + assert retrieved_entries[0].payload["message"] == "This is a JSON log." + assert retrieved_entries[1].payload == "Goodbye, world!" + assert retrieved_entries[2].payload == "Hello, world!" eventually_consistent_test()