From 1c0b91e4597357f63dfcb4b271b939ceefa0ad55 Mon Sep 17 00:00:00 2001 From: l-krrish Date: Thu, 29 Jan 2026 11:54:15 -0500 Subject: [PATCH 1/3] feat(logging): add sample to list available logs Adds Python sample demonstrating how to list all available logs in a Google Cloud project, similar to the existing Node.js implementation. - Includes list_logs.py sample with region tag logging_list_logs - Includes list_logs_test.py with test coverage - Follows existing sample patterns in the repository Fixes #15427 --- samples/list_logs.py | 57 +++++++++++++++++++++++++++++++++++++++ samples/list_logs_test.py | 24 +++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 samples/list_logs.py create mode 100644 samples/list_logs_test.py diff --git a/samples/list_logs.py b/samples/list_logs.py new file mode 100644 index 00000000..3fcb932e --- /dev/null +++ b/samples/list_logs.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python + +# Copyright 2025 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. + +"""Sample showing how to list available logs in a Google Cloud project.""" + +import argparse + + +def list_logs(project_id): + """Lists all available logs in the specified project. + + Args: + project_id: The Google Cloud project ID. + """ + # [START logging_list_logs] + from google.cloud import logging_v2 + + # Creates a client + client = logging_v2.services.logging_service_v2.LoggingServiceV2Client() + + # Creates the request + request = logging_v2.types.ListLogsRequest( + parent=f"projects/{project_id}", + ) + + # Lists all logs in the project + logs = client.list_logs(request=request) + + print("Logs:") + for log in logs: + print(log) + # [END logging_list_logs] + + +if __name__ == "__main__": + parser = argparse.ArgumentParser( + description=__doc__, + formatter_class=argparse.RawDescriptionHelpFormatter, + ) + parser.add_argument("project_id", help="Your Google Cloud project ID") + + args = parser.parse_args() + + list_logs(args.project_id) \ No newline at end of file diff --git a/samples/list_logs_test.py b/samples/list_logs_test.py new file mode 100644 index 00000000..77d1c699 --- /dev/null +++ b/samples/list_logs_test.py @@ -0,0 +1,24 @@ +# Copyright 2025 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 os +import list_logs +def test_list_logs(capsys): + + project_id = os.environ["GOOGLE_CLOUD_PROJECT"] + + list_logs.list_logs(project_id) + + out, _ = capsys.readouterr() + assert "Logs:" in out \ No newline at end of file From c989f8c7671ea89c84af4c13a2bf2f7911e760b4 Mon Sep 17 00:00:00 2001 From: l-krrish Date: Thu, 29 Jan 2026 12:00:48 -0500 Subject: [PATCH 2/3] fix: address code review feedback - Simplify list_logs API call by passing parent directly - Remove extra blank lines in test file - Add newline at end of files per PEP 8 --- samples/list_logs.py | 10 +++------- samples/list_logs_test.py | 4 ++-- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/samples/list_logs.py b/samples/list_logs.py index 3fcb932e..0e2af8a5 100644 --- a/samples/list_logs.py +++ b/samples/list_logs.py @@ -31,13 +31,8 @@ def list_logs(project_id): # Creates a client client = logging_v2.services.logging_service_v2.LoggingServiceV2Client() - # Creates the request - request = logging_v2.types.ListLogsRequest( - parent=f"projects/{project_id}", - ) - # Lists all logs in the project - logs = client.list_logs(request=request) + logs = client.list_logs(parent=f"projects/{project_id}") print("Logs:") for log in logs: @@ -54,4 +49,5 @@ def list_logs(project_id): args = parser.parse_args() - list_logs(args.project_id) \ No newline at end of file + list_logs(args.project_id) + \ No newline at end of file diff --git a/samples/list_logs_test.py b/samples/list_logs_test.py index 77d1c699..5907d7d0 100644 --- a/samples/list_logs_test.py +++ b/samples/list_logs_test.py @@ -17,8 +17,8 @@ def test_list_logs(capsys): project_id = os.environ["GOOGLE_CLOUD_PROJECT"] - list_logs.list_logs(project_id) out, _ = capsys.readouterr() - assert "Logs:" in out \ No newline at end of file + assert "Logs:" in out + \ No newline at end of file From e9db0413c1fa5c5ba5daf8df100daa6b106f34e9 Mon Sep 17 00:00:00 2001 From: Krrish Lala Date: Fri, 30 Jan 2026 09:16:06 -0500 Subject: [PATCH 3/3] Update samples/list_logs_test.py Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --- samples/list_logs_test.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/samples/list_logs_test.py b/samples/list_logs_test.py index 5907d7d0..f48488b1 100644 --- a/samples/list_logs_test.py +++ b/samples/list_logs_test.py @@ -14,9 +14,8 @@ import os import list_logs -def test_list_logs(capsys): - project_id = os.environ["GOOGLE_CLOUD_PROJECT"] + list_logs.list_logs(project_id) out, _ = capsys.readouterr()