Skip to content
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

Logging #94

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added logging/__init__.py
Empty file.
Empty file added logging/samples/__init__.py
Empty file.
80 changes: 80 additions & 0 deletions logging/samples/listlogs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Copyright 2015 Google Inc. All rights reserved.
#
# 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.

"""Command-line program to list the logs in a Google Cloud Platform project.

Simple command-line program to demonstrate connecting to the Google Cloud
Logging API to retrieve logs data, using application default credentials[1] to
authenticate.

This application will run--without modification--on 1) Google App Engine,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We recommend users running this locally to use gcloud auth. Also, no need to have the entire ADC description here, just give a brief summary and link them for more information.

2) Google Compute Engine, 3) a local Linux workstation with the Google Cloud
SDK, or 4) a Linux computer without the SDK.

In cases 1) and 2), authorization comes from a built-in service account on the
instance running this application. In case 3) you must run "gcloud auth login"
your workstation to authorize the application. In case 4) you must create a
service account[2] in the Google Cloud project whose log data you will fetch,
copy the account's JSON private key file to the computer running this
application, and on that computer set the environment variable
GOOGLE_APPLICATION_CREDENTIALS to point to the JSON private key file. For
example:

$ export GOOGLE_APPLICATION_CREDENTIALS=/path/to/json-key.json

[1] https://developers.google.com/identity/protocols/application-default-credentials
[2] https://console.developers.google.com/project/_/apiui/credential
""" # NOQA
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# NOQA can be removed, it doesn't do anything in this repository.


# [START all]
import json
import sys

from googleapiclient.discovery import build

from oauth2client.client import GoogleCredentials

# [START auth]
def GetLoggingService():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably okay to just put this in main, like the bigquery samples.

credentials = GoogleCredentials.get_application_default()
service = build('logging', 'v1beta3', credentials=credentials)
return service
# [END auth]


# [START listlogs]
def ListLogs(project_id, service):
next_page_token = None
while True:
response = service.projects().logs().list(
projectsId=project_id, pageToken=next_page_token).execute()
for log in response["logs"]:
print log["name"]
next_page_token = response.get("nextPageToken")
if not next_page_token:
break
# [END listlogs]


def main(project_id):
service = GetLoggingService()
ListLogs(project_id, service)


if __name__ == '__main__':
if len(sys.argv) != 2:
print "Usage: %s <project-name>" % sys.argv[0]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use argparse.

sys.exit(1)
main(sys.argv[1])
# [END all]
Empty file.
38 changes: 38 additions & 0 deletions logging/samples/tests/test_listlogs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright 2015, Google, Inc.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you for writing tests.

# 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 re
import unittest

from logging.samples import listlogs

import tests


class TestListLogs(tests.CloudBaseTest):

@classmethod
def setUpClass(cls):
cls.test_project_id = os.environ.get(tests.PROJECT_ID_ENV)

def test_main(self):
with tests.capture_stdout() as stdout:
listlogs.main(self.test_project_id)
output = stdout.getvalue().strip()
self.assertRegexpMatches(
output, re.compile(r'.*', re.S))


if __name__ == '__main__':
unittest.main()