Skip to content

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

Closed
MukundaKatta wants to merge 1 commit intogoogleapis:mainfrom
MukundaKatta:codex/logging-write-log-entry
Closed

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

Conversation

@MukundaKatta
Copy link
Copy Markdown
Contributor

Fixes #15426.

Adds a handwritten Cloud Logging sample for the logging_write_log_entry region tag that demonstrates:

  • writing a textPayload entry
  • writing a jsonPayload entry
  • severity, labels, and global monitored resource metadata
  • single-entry writes and batched writes

Tested:

  • /private/tmp/google-cloud-python-logging-venv/bin/python -m pytest packages/google-cloud-logging/samples/snippets/write_log_entry_test.py

@MukundaKatta MukundaKatta requested a review from a team as a code owner April 30, 2026 21:59
@snippet-bot
Copy link
Copy Markdown

snippet-bot Bot commented Apr 30, 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

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 corresponding unit tests for writing text and structured log entries to Google Cloud Logging. The review feedback focuses on improving the code's robustness and adherence to library best practices, specifically by importing from the public API surface, using the client's helper method for log path construction, and adding basic command-line argument validation to prevent 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

Avoid importing from deep internal paths like services.logging_service_v2 or types. It is more idiomatic and robust to import from the public surface of the library, which is less likely to change in future versions.

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

"""Writes text and structured log entries to Cloud Logging."""

client = LoggingServiceV2Client()
log_name = f"projects/{project_id}/logs/python-example-log"
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

Instead of manually constructing the log resource name string, use the log_path helper method provided by the client. This ensures the name is correctly formatted and follows the library's best practices.

Suggested change
log_name = f"projects/{project_id}/logs/python-example-log"
log_name = client.log_path(project_id, "python-example-log")

Comment on lines +70 to +71
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

The script will raise an IndexError if no command-line argument is provided. Adding a simple check for sys.argv improves the usability of this sample by providing a clear usage message.

Suggested change
if __name__ == "__main__":
write_log_entry(project_id=sys.argv[1])
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python write_log_entry.py <PROJECT_ID>")
sys.exit(1)
write_log_entry(project_id=sys.argv[1])

@parthea
Copy link
Copy Markdown
Contributor

parthea commented May 1, 2026

Hi @MukundaKatta,

Thanks for opening a PR. Regrettably, samples for google-cloud-logging have moved to https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/logging/samples/snippets . I'm going to close this PR but please feel free to open a new PR in https://github.com/GoogleCloudPlatform/python-docs-samples.

@parthea parthea closed this May 1, 2026
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.

Improve the Write logEntry sample

2 participants