Skip to content

docs(logging): add write log entry sample#17147

Open
MukundaKatta wants to merge 1 commit into
googleapis:mainfrom
MukundaKatta:codex/logging-write-log-entry
Open

docs(logging): add write log entry sample#17147
MukundaKatta wants to merge 1 commit into
googleapis:mainfrom
MukundaKatta:codex/logging-write-log-entry

Conversation

@MukundaKatta
Copy link
Copy Markdown
Contributor

Summary

  • docs(logging): add write log entry sample

Changed files

  • packages/google-cloud-logging/samples/snippets/write_log_entry.py
  • packages/google-cloud-logging/samples/snippets/write_log_entry_test.py

Tests

  • Not run in this publishing pass; local branch was clean before opening the PR.

Copy link
Copy Markdown
Contributor

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

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 introduces a new Python sample and unit test for writing text and structured log entries to Google Cloud Logging. The feedback suggests improving the import structure by using the public namespace and adding a check for command-line arguments to prevent potential runtime errors.

Comment on lines +18 to +20
from google.cloud.logging_v2.services.logging_service_v2 import LoggingServiceV2Client
from google.cloud.logging_v2.types import LogEntry
from google.cloud.logging_v2.types import WriteLogEntriesRequest
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.

medium

It is recommended to import classes from the public google.cloud.logging_v2 namespace rather than from deep internal submodules like services or types. This follows best practices for using Google Cloud client libraries and makes the code more resilient to internal package changes.

Suggested change
from google.cloud.logging_v2.services.logging_service_v2 import LoggingServiceV2Client
from google.cloud.logging_v2.types import LogEntry
from google.cloud.logging_v2.types import WriteLogEntriesRequest
from google.cloud.logging_v2 import LoggingServiceV2Client
from google.cloud.logging_v2 import LogEntry
from google.cloud.logging_v2 import WriteLogEntriesRequest



if __name__ == "__main__":
write_log_entry(project_id=sys.argv[1])
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.

medium

Accessing sys.argv[1] without checking the number of arguments will cause an IndexError if the script is executed without a project ID. Adding a simple check with a usage message improves the robustness and usability of the sample.

Suggested change
write_log_entry(project_id=sys.argv[1])
if len(sys.argv) < 2:
print(f"Usage: python {sys.argv[0]} <project_id>")
sys.exit(1)
write_log_entry(project_id=sys.argv[1])

@MukundaKatta MukundaKatta changed the title [codex] docs(logging): add write log entry sample docs(logging): add write log entry sample May 15, 2026
@MukundaKatta MukundaKatta marked this pull request as ready for review May 15, 2026 02:59
@MukundaKatta MukundaKatta requested a review from a team as a code owner May 15, 2026 02:59
@snippet-bot
Copy link
Copy Markdown

snippet-bot Bot commented May 15, 2026

Here is the summary of changes.

You are about to add 1 region tag.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant