docs(logging): add write log entry sample#16906
docs(logging): add write log entry sample#16906MukundaKatta wants to merge 1 commit intogoogleapis:mainfrom
Conversation
|
Here is the summary of changes. You are about to add 1 region tag.
This comment is generated by snippet-bot.
|
There was a problem hiding this comment.
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.
| 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 |
There was a problem hiding this comment.
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.
| 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" |
There was a problem hiding this comment.
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.
| log_name = f"projects/{project_id}/logs/python-example-log" | |
| log_name = client.log_path(project_id, "python-example-log") |
| if __name__ == "__main__": | ||
| write_log_entry(project_id=sys.argv[1]) |
There was a problem hiding this comment.
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.
| 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]) |
|
Hi @MukundaKatta, Thanks for opening a PR. Regrettably, samples for |
Fixes #15426.
Adds a handwritten Cloud Logging sample for the logging_write_log_entry region tag that demonstrates:
Tested: