Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion logging/samples/snippets/requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
backoff==2.2.1
pytest==9.0.3; python_version >= "3.10"
pytest==9.0.3
44 changes: 24 additions & 20 deletions logging/samples/snippets/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment thread
amcolin marked this conversation as resolved.

# 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})
Comment thread
amcolin marked this conversation as resolved.

print("Wrote logs to {}.".format(logger.name))
# wait for threads to finish working on the background.
time.sleep(5)


# [END logging_write_log_entry]
Expand Down Expand Up @@ -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)
23 changes: 18 additions & 5 deletions logging/samples/snippets/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import uuid

import backoff
Expand All @@ -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
Expand All @@ -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()

Expand Down