-
Notifications
You must be signed in to change notification settings - Fork 1.7k
docs(logging): add write log entry sample #17147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MukundaKatta
wants to merge
1
commit into
googleapis:main
Choose a base branch
from
MukundaKatta:codex/logging-write-log-entry
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+128
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
packages/google-cloud-logging/samples/snippets/write_log_entry.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,71 @@ | ||||||||||||||
| # Copyright 2026 Google LLC | ||||||||||||||
| # | ||||||||||||||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||||
| # you may not use this file except in compliance with the License. | ||||||||||||||
| # You may obtain a copy of the License at | ||||||||||||||
| # | ||||||||||||||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||
| # | ||||||||||||||
| # Unless required by applicable law or agreed to in writing, software | ||||||||||||||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||
| # See the License for the specific language governing permissions and | ||||||||||||||
| # limitations under the License. | ||||||||||||||
|
|
||||||||||||||
| import sys | ||||||||||||||
|
|
||||||||||||||
| from google.api import monitored_resource_pb2 | ||||||||||||||
| 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.logging.type import log_severity_pb2 | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| # [START logging_write_log_entry] | ||||||||||||||
| def write_log_entry(project_id: str) -> None: | ||||||||||||||
| """Writes text and structured log entries to Cloud Logging.""" | ||||||||||||||
|
|
||||||||||||||
| client = LoggingServiceV2Client() | ||||||||||||||
| log_name = f"projects/{project_id}/logs/python-example-log" | ||||||||||||||
| resource = monitored_resource_pb2.MonitoredResource(type="global") | ||||||||||||||
| labels = {"sample": "write-log-entry"} | ||||||||||||||
|
|
||||||||||||||
| text_entry = LogEntry( | ||||||||||||||
| log_name=log_name, | ||||||||||||||
| resource=resource, | ||||||||||||||
| severity=log_severity_pb2.INFO, | ||||||||||||||
| labels=labels, | ||||||||||||||
| text_payload="Text log entry written from Python.", | ||||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
| struct_entry = LogEntry( | ||||||||||||||
| log_name=log_name, | ||||||||||||||
| resource=resource, | ||||||||||||||
| severity=log_severity_pb2.WARNING, | ||||||||||||||
| labels=labels, | ||||||||||||||
| json_payload={ | ||||||||||||||
| "message": "Structured log entry written from Python.", | ||||||||||||||
| "component": "sample", | ||||||||||||||
| }, | ||||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
| client.write_log_entries( | ||||||||||||||
| request=WriteLogEntriesRequest( | ||||||||||||||
| entries=[text_entry], | ||||||||||||||
| ) | ||||||||||||||
| ) | ||||||||||||||
| print("Wrote one text log entry.") | ||||||||||||||
|
|
||||||||||||||
| client.write_log_entries( | ||||||||||||||
| request=WriteLogEntriesRequest( | ||||||||||||||
| entries=[text_entry, struct_entry], | ||||||||||||||
| ) | ||||||||||||||
| ) | ||||||||||||||
| print("Wrote a batch of text and structured log entries.") | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| # [END logging_write_log_entry] | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| if __name__ == "__main__": | ||||||||||||||
| write_log_entry(project_id=sys.argv[1]) | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Accessing
Suggested change
|
||||||||||||||
57 changes: 57 additions & 0 deletions
57
packages/google-cloud-logging/samples/snippets/write_log_entry_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from unittest import mock | ||
|
|
||
| from google.logging.type import log_severity_pb2 | ||
|
|
||
| import write_log_entry | ||
|
|
||
|
|
||
| def test_write_log_entry(capsys): | ||
| project_id = "my-project" | ||
|
|
||
| with mock.patch.object(write_log_entry, "LoggingServiceV2Client") as client: | ||
| write_log_entry.write_log_entry(project_id) | ||
|
|
||
| logging_client = client.return_value | ||
| assert logging_client.write_log_entries.call_count == 2 | ||
|
|
||
| single_request = logging_client.write_log_entries.call_args_list[0].kwargs["request"] | ||
| assert len(single_request.entries) == 1 | ||
|
|
||
| text_entry = single_request.entries[0] | ||
| assert text_entry.log_name == f"projects/{project_id}/logs/python-example-log" | ||
| assert text_entry.resource.type == "global" | ||
| assert text_entry.severity == log_severity_pb2.INFO | ||
| assert text_entry.labels["sample"] == "write-log-entry" | ||
| assert text_entry.text_payload == "Text log entry written from Python." | ||
|
|
||
| batch_request = logging_client.write_log_entries.call_args_list[1].kwargs["request"] | ||
| assert len(batch_request.entries) == 2 | ||
| assert batch_request.entries[0].text_payload == text_entry.text_payload | ||
|
|
||
| struct_entry = batch_request.entries[1] | ||
| assert struct_entry.resource.type == "global" | ||
| assert struct_entry.severity == log_severity_pb2.WARNING | ||
| assert struct_entry.labels["sample"] == "write-log-entry" | ||
| assert ( | ||
| struct_entry.json_payload["message"] | ||
| == "Structured log entry written from Python." | ||
| ) | ||
| assert struct_entry.json_payload["component"] == "sample" | ||
|
|
||
| out, _ = capsys.readouterr() | ||
| assert "Wrote one text log entry." in out | ||
| assert "Wrote a batch of text and structured log entries." in out |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is recommended to import classes from the public
google.cloud.logging_v2namespace rather than from deep internal submodules likeservicesortypes. This follows best practices for using Google Cloud client libraries and makes the code more resilient to internal package changes.