Skip to content

Commit b973ca3

Browse files
author
Jon Wayne Parrott
committed
Adding cloud logging api sample.
1 parent 8f17b04 commit b973ca3

File tree

7 files changed

+130
-1
lines changed

7 files changed

+130
-1
lines changed

.coveragerc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ include =
77
datastore/*
88
monitoring/*
99
storage/*
10+
cloud_logging/*
1011
[report]
1112
exclude_lines =
1213
pragma: NO COVER

cloud_logging/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Google Cloud Logging Samples
2+
3+
This section contains samples for [Google Cloud Logging](https://cloud.google.com/logging).
4+
5+
## Running the samples
6+
7+
In order to run it, your environment must be setup with [authentication
8+
information](https://developers.google.com/identity/protocols/application-default-credentials#howtheywork). If you're running it in your local development environment and you have the [Google Cloud SDK](https://cloud.google.com/sdk/) installed, you can do this easily by running:
9+
10+
$ gcloud auth login
11+
12+
## Additional resources
13+
14+
For more information on Cloud Logging you can visit:
15+
16+
> https://developers.google.com/logging
17+
18+
For more information on the Clloud Logging API Python library surface you
19+
can visit:
20+
21+
> https://developers.google.com/resources/api-libraries/documentation/logging/v1beta3/python/latest/
22+
23+
For information on the Python Client Library visit:
24+
25+
> https://developers.google.com/api-client-library/python
26+
27+
## Other Samples

cloud_logging/__init__.py

Whitespace-only changes.

cloud_logging/api/__init__.py

Whitespace-only changes.

cloud_logging/api/list_logs.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2015 Google Inc. All rights reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""Command-line program to list the logs in a Google Cloud Platform project.
18+
19+
This sample is used in this section of the documentation:
20+
21+
https://cloud.google.com/logging/docs
22+
23+
For more information, see the README.md under /cloud_logging.
24+
"""
25+
26+
# [START all]
27+
import argparse
28+
29+
from googleapiclient import discovery
30+
from oauth2client.client import GoogleCredentials
31+
32+
33+
# [START list_logs]
34+
def list_logs(project_id, logging_service):
35+
request = logging_service.projects().logs().list(projectsId=project_id)
36+
37+
while request:
38+
response = request.execute()
39+
for log in response['logs']:
40+
print(log['name'])
41+
42+
request = logging_service.projects().logs().list_next(
43+
request, response)
44+
# [END list_logs]
45+
46+
47+
def main(project_id):
48+
# [START build_service]
49+
credentials = GoogleCredentials.get_application_default()
50+
logging_service = discovery.build(
51+
'logging', 'v1beta3', credentials=credentials)
52+
# [END build_service]
53+
54+
list_logs(project_id, logging_service)
55+
56+
57+
if __name__ == '__main__':
58+
parser = argparse.ArgumentParser(
59+
description=__doc__,
60+
formatter_class=argparse.RawDescriptionHelpFormatter)
61+
parser.add_argument('project_id', help='Your Google Cloud project ID.')
62+
63+
args = parser.parse_args()
64+
65+
main(args.project_id)
66+
# [END all]

cloud_logging/api/list_logs_test.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright 2015, Google, Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
import re
15+
import unittest
16+
17+
import tests
18+
19+
from . import list_logs
20+
21+
22+
class TestListLogs(tests.CloudBaseTest):
23+
24+
def test_main(self):
25+
with tests.capture_stdout() as stdout:
26+
list_logs.main(self.project_id)
27+
28+
output = stdout.getvalue().strip()
29+
30+
self.assertRegexpMatches(
31+
output, re.compile(r'.*', re.S))
32+
33+
34+
if __name__ == '__main__':
35+
unittest.main()

storage/api/list_objects.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def main(argv):
7474

7575
# If you have too many items to list in one request, list_next() will
7676
# automatically handle paging with the pageToken.
77-
while req is not None:
77+
while req:
7878
resp = req.execute()
7979
print(json.dumps(resp, indent=2))
8080
req = service.objects().list_next(req, resp)

0 commit comments

Comments
 (0)