docs(logging): add write log entry sample#17147
Conversation
There was a problem hiding this comment.
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.
| 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.
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.
| 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]) |
There was a problem hiding this comment.
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.
| 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]) |
|
Here is the summary of changes. You are about to add 1 region tag.
This comment is generated by snippet-bot.
|
Summary
Changed files
packages/google-cloud-logging/samples/snippets/write_log_entry.pypackages/google-cloud-logging/samples/snippets/write_log_entry_test.pyTests