Skip to content

Commit 124374a

Browse files
authored
feat(logging): add a sample for list logs (#9999)
1 parent f39e919 commit 124374a

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

logging/cloud-client/list_logs.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright 2023 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START logging_list_logs]
16+
from typing import List
17+
from google.cloud import logging_v2
18+
19+
20+
def list_logs(project_id: str) -> List[str]:
21+
"""Lists all logs in a project.
22+
23+
Args:
24+
project_id: the ID of the project
25+
26+
Returns:
27+
A list of log names.
28+
"""
29+
client = logging_v2.services.logging_service_v2.LoggingServiceV2Client()
30+
request = logging_v2.types.ListLogsRequest(
31+
parent=f"projects/{project_id}",
32+
)
33+
34+
logs = client.list_logs(request=request)
35+
for log in logs:
36+
print(log)
37+
38+
return logs
39+
# [END logging_list_logs]
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2023 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import list_logs
16+
import os
17+
18+
PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"]
19+
20+
21+
def test_list_logs(capsys):
22+
logs = list_logs.list_logs(PROJECT)
23+
assert "logs" in str(logs)

0 commit comments

Comments
 (0)