Skip to content

feat(logging): update write_entry snippet to use Python logging framework#14361

Open
XrossFox wants to merge 3 commits into
GoogleCloudPlatform:mainfrom
XrossFox:logger-snippets-json-update
Open

feat(logging): update write_entry snippet to use Python logging framework#14361
XrossFox wants to merge 3 commits into
GoogleCloudPlatform:mainfrom
XrossFox:logger-snippets-json-update

Conversation

@XrossFox

@XrossFox XrossFox commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

Description

  • Update write_entry in snippets.py to use google.cloud.logging.setup_logging() instead of calling the client logger methods directly.
  • Demonstrate writing a structured JSON log using the standard logging library's extra parameter.
  • Update test_write in snippets_test.py to verify logs written via the default python logging handler.
  • Remove python version constraint for pytest in requirements-test.txt.

Fixes b/234303347

Checklist

Testing

  • I have tested this change on a live environment and verified it works as intended.

Compliance & Style


Post-Approval Actions

  • Please merge this PR for me once it is approved

…ework

 - Update `write_entry` in `snippets.py` to use `google.cloud.logging.setup_logging()` instead of calling the client logger methods directly.
 - Demonstrate writing a structured JSON log using the standard `logging` library's `extra` parameter.
 - Update `test_write` in `snippets_test.py` to verify logs written via the default python logging handler.
 - Remove python version constraint for pytest in `requirements-test.txt`.
@product-auto-label product-auto-label Bot added api: logging Issues related to the Cloud Logging API. samples Issues that are directly related to samples. labels Jun 29, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the logging snippets to use the standard Python logging framework integrated with Google Cloud Logging via setup_logging(), replacing the custom logger implementation. It also updates the corresponding tests to verify the written logs. The reviewer identified two issues: an outdated docstring in snippets.py that still references a logger parameter, and potential test flakiness in snippets_test.py due to an incorrect log filter format and direct calls to next() on the entries iterator which can raise an unhandled StopIteration exception.

Comment thread logging/samples/snippets/snippets_test.py Outdated
Comment thread logging/samples/snippets/snippets.py Outdated
@XrossFox

Copy link
Copy Markdown
Contributor Author

This sample has been tested locally:

image image

@XrossFox XrossFox marked this pull request as ready for review June 29, 2026 23:11
@XrossFox XrossFox requested review from a team as code owners June 29, 2026 23:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: logging Issues related to the Cloud Logging API. samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant