From c451a14bded9b1658924a4a15724f33620b60ad9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0Sam=20Harb?= =?UTF-8?q?ison?= <       sharbison3@gmail.com> Date: Sat, 22 Aug 2015 16:41:48 -0400 Subject: [PATCH 1/6] Initial ListLogs sample code. --- logging/pom.xml | 69 +++++++++++++++++++++++ logging/src/main/java/ListLogs.java | 86 +++++++++++++++++++++++++++++ 2 files changed, 155 insertions(+) create mode 100644 logging/pom.xml create mode 100644 logging/src/main/java/ListLogs.java diff --git a/logging/pom.xml b/logging/pom.xml new file mode 100644 index 00000000000..134d7a1fc5e --- /dev/null +++ b/logging/pom.xml @@ -0,0 +1,69 @@ + + 4.0.0 + com.google.cloud.logging.samples + cloud-logging-samples + jar + + + doc-samples + com.google.cloud + 1.0.0 + .. + + + + + + googleapis + https://google-api-client-libraries.appspot.com/mavenrepo + + + + + + com.google.apis + google-api-services-logging + v3beta1-rev3-1.20.0 + + + com.google.oauth-client + google-oauth-client + ${project.oauth.version} + + + com.google.http-client + google-http-client-jackson2 + ${project.http.version} + + + com.google.oauth-client + google-oauth-client-jetty + ${project.oauth.version} + + + junit + junit + + + com.jcabi + jcabi-matchers + + + + + src/main/java + + + org.apache.maven.plugins + maven-compiler-plugin + 3.2 + + 5 + 5 + + + + + + diff --git a/logging/src/main/java/ListLogs.java b/logging/src/main/java/ListLogs.java new file mode 100644 index 00000000000..d125f6d0f38 --- /dev/null +++ b/logging/src/main/java/ListLogs.java @@ -0,0 +1,86 @@ +/** + * Copyright (c) 2015 Google Inc. + * + * 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. + */ +// [START all] +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.services.logging.Logging; +import com.google.api.services.logging.model.ListLogsResponse; +import com.google.api.services.logging.model.Log; + +/** + * Cloud Logging Java API sample that lists the logs available to a project. + * Uses the v1beta3 Cloud Logging API, version 1.20.0 or later. + * See https://cloud.google.com/logging/docs/api/libraries/. + */ +public class ListLogs { + + private static final List LOGGING_SCOPES = Collections.singletonList( + "https://www.googleapis.com/auth/logging.read"); + + private static final String APPLICATION_NAME = "ListLogs sample"; + + /** Returns an authorized Cloud Logging API service client. */ + public static Logging getLoggingService() throws GeneralSecurityException, + IOException { + GoogleCredential credential = GoogleCredential.getApplicationDefault() + .createScoped(LOGGING_SCOPES); + Logging service = new Logging.Builder( + GoogleNetHttpTransport.newTrustedTransport(), + JacksonFactory.getDefaultInstance(), + credential).setApplicationName(APPLICATION_NAME).build(); + return service; + } + + /** Extract simple log names from URL-encoded resource names. */ + public static List getSimpleLogNames(List logs, + String projectId) throws UnsupportedEncodingException { + final int RESOURCE_PREFIX_LENGTH = ("/projects/" + projectId + "/logs/") + .length(); + List logNames = new ArrayList(); + for (Log log: logs) { + logNames.add(URLDecoder.decode(log.getName(), "utf-8").substring( + RESOURCE_PREFIX_LENGTH)); + } + return logNames; + } + + public static void main(String[] args) throws Exception { + if (args.length != 1) { + System.err.println(String.format("Usage: %s ", + ListLogs.class.getSimpleName())); + return; + } + + String projectId = args[0]; + Logging service = getLoggingService(); + ListLogsResponse response = service.projects().logs().list(projectId) + .execute(); + System.out.println("RAW: " + response.toPrettyString()); + System.out.println("SIMPLE: " + + getSimpleLogNames(response.getLogs(), projectId)); + + } +} +// [END all] From 9273a34f60dd2ac6b061ad2d952356d2494deffe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0Sam=20Harb?= =?UTF-8?q?ison?= <       sharbison3@gmail.com> Date: Sat, 22 Aug 2015 16:50:17 -0400 Subject: [PATCH 2/6] Add settings files. --- logging/.classpath | 26 +++++++++++++++++ logging/.gitignore | 28 +++++++++++++++++++ logging/.project | 23 +++++++++++++++ .../org.eclipse.core.resources.prefs | 4 +++ logging/.settings/org.eclipse.jdt.core.prefs | 5 ++++ logging/.settings/org.eclipse.m2e.core.prefs | 4 +++ 6 files changed, 90 insertions(+) create mode 100644 logging/.classpath create mode 100644 logging/.gitignore create mode 100644 logging/.project create mode 100644 logging/.settings/org.eclipse.core.resources.prefs create mode 100644 logging/.settings/org.eclipse.jdt.core.prefs create mode 100644 logging/.settings/org.eclipse.m2e.core.prefs diff --git a/logging/.classpath b/logging/.classpath new file mode 100644 index 00000000000..0a1daddd3e8 --- /dev/null +++ b/logging/.classpath @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/logging/.gitignore b/logging/.gitignore new file mode 100644 index 00000000000..86dc131d1b2 --- /dev/null +++ b/logging/.gitignore @@ -0,0 +1,28 @@ +# Google App Engine generated folder +appengine-generated/ + +# Java +*.class + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +# maven +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +dependency-reduced-pom.xml +buildNumber.properties + +service-account.json diff --git a/logging/.project b/logging/.project new file mode 100644 index 00000000000..fce5b7aaee3 --- /dev/null +++ b/logging/.project @@ -0,0 +1,23 @@ + + + cloud-logging-samples + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/logging/.settings/org.eclipse.core.resources.prefs b/logging/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 00000000000..f9fe34593fc --- /dev/null +++ b/logging/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,4 @@ +eclipse.preferences.version=1 +encoding//src/main/java=UTF-8 +encoding//src/test/java=UTF-8 +encoding/=UTF-8 diff --git a/logging/.settings/org.eclipse.jdt.core.prefs b/logging/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 00000000000..abec6ca389a --- /dev/null +++ b/logging/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,5 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 +org.eclipse.jdt.core.compiler.compliance=1.5 +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.source=1.5 diff --git a/logging/.settings/org.eclipse.m2e.core.prefs b/logging/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 00000000000..f897a7f1cb2 --- /dev/null +++ b/logging/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 From 77c33dfb0538600c1ae9a3b2c1f65b2a40aa45a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0Sam=20Harb?= =?UTF-8?q?ison?= <       sharbison3@gmail.com> Date: Sat, 22 Aug 2015 18:04:57 -0400 Subject: [PATCH 3/6] Fix pom.xml files. --- logging/pom.xml | 10 +--------- pom.xml | 1 + 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/logging/pom.xml b/logging/pom.xml index 134d7a1fc5e..eb803b986bb 100644 --- a/logging/pom.xml +++ b/logging/pom.xml @@ -12,19 +12,11 @@ .. - - - - googleapis - https://google-api-client-libraries.appspot.com/mavenrepo - - - com.google.apis google-api-services-logging - v3beta1-rev3-1.20.0 + v1beta3-rev4-1.20.0 com.google.oauth-client diff --git a/pom.xml b/pom.xml index 2982562f6c2..f70a3a60a69 100644 --- a/pom.xml +++ b/pom.xml @@ -28,6 +28,7 @@ cloud-storage/xml-api/serviceaccount-appengine-sample cloud-storage/storage-transfer monitoring + logging From 7c31b3a3b3923fa52b6ffb362dce68e1785136e4 Mon Sep 17 00:00:00 2001 From: Sam Harbison Date: Wed, 2 Sep 2015 13:25:59 -0400 Subject: [PATCH 4/6] Added a test and improved the main sample to handle paged API responses. --- logging/src/main/java/ListLogs.java | 107 +++++++++++++++--------- logging/src/test/java/ListLogsTest.java | 61 ++++++++++++++ 2 files changed, 129 insertions(+), 39 deletions(-) create mode 100644 logging/src/test/java/ListLogsTest.java diff --git a/logging/src/main/java/ListLogs.java b/logging/src/main/java/ListLogs.java index d125f6d0f38..b49d3c4dbf8 100644 --- a/logging/src/main/java/ListLogs.java +++ b/logging/src/main/java/ListLogs.java @@ -13,22 +13,24 @@ * License for the specific language governing permissions and limitations under * the License. */ -// [START all] -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.net.URLDecoder; -import java.security.GeneralSecurityException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - +// [START imports] import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.http.HttpTransport; +import com.google.api.client.http.javanet.NetHttpTransport; +import com.google.api.client.json.JsonFactory; import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.client.util.Strings; import com.google.api.services.logging.Logging; +import com.google.api.services.logging.LoggingScopes; import com.google.api.services.logging.model.ListLogsResponse; import com.google.api.services.logging.model.Log; +import java.io.IOException; +import java.net.URLDecoder; +import java.util.Collections; +import java.util.List; +// [END imports] + /** * Cloud Logging Java API sample that lists the logs available to a project. * Uses the v1beta3 Cloud Logging API, version 1.20.0 or later. @@ -37,36 +39,68 @@ public class ListLogs { private static final List LOGGING_SCOPES = Collections.singletonList( - "https://www.googleapis.com/auth/logging.read"); - + LoggingScopes.LOGGING_READ); + private static final String APPLICATION_NAME = "ListLogs sample"; - /** Returns an authorized Cloud Logging API service client. */ - public static Logging getLoggingService() throws GeneralSecurityException, - IOException { - GoogleCredential credential = GoogleCredential.getApplicationDefault() - .createScoped(LOGGING_SCOPES); - Logging service = new Logging.Builder( - GoogleNetHttpTransport.newTrustedTransport(), - JacksonFactory.getDefaultInstance(), - credential).setApplicationName(APPLICATION_NAME).build(); + /** + * Returns an authorized Cloud Logging API service client that is usable + * on Google App Engine, Google Compute Engine, workstations with the Google Cloud SDK, + * and other computers if you install service account private credentials. + * See https://cloud.google.com/logging/docs/api/tasks. + */ + // [START auth] + public static Logging getLoggingService() throws IOException { + HttpTransport transport = new NetHttpTransport(); + JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); + GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory); + if (credential.createScopedRequired()) { + credential = credential.createScoped(LOGGING_SCOPES); + } + Logging service = new Logging.Builder(transport, jsonFactory, credential) + .setApplicationName(APPLICATION_NAME).build(); return service; } + // [END auth] - /** Extract simple log names from URL-encoded resource names. */ - public static List getSimpleLogNames(List logs, - String projectId) throws UnsupportedEncodingException { - final int RESOURCE_PREFIX_LENGTH = ("/projects/" + projectId + "/logs/") - .length(); - List logNames = new ArrayList(); - for (Log log: logs) { - logNames.add(URLDecoder.decode(log.getName(), "utf-8").substring( - RESOURCE_PREFIX_LENGTH)); - } - return logNames; + /** + * Lists the names of the logs visible to a project, which may require fetching multiple + * pages of results from the Cloud Logging API. This method converts log resource names + * ("/projects/PROJECTID/logs/SERVICENAME%2FLOGNAME") to simple log names ("SERVICENAME/LOGNAME"). + * + * @param service The logging service client returned by getLoggingService. + * @param projectId The project whose logs are to be listed. + * @throws IOException If the Cloud Logging API fails because, for example, the project ID + * doesn't exist or authorization fails. + * See https://cloud.google.com//logging/docs/api/tasks/#java_sample_code. + */ + // [START listlogs] + private static void listLogs(Logging service, String projectId) throws IOException { + final int pageSize = 3; + final int resourcePrefixLength = ("/projects/" + projectId + "/logs/").length(); + String nextPageToken = ""; + + do { + ListLogsResponse response = service.projects().logs().list(projectId) + .setPageToken(nextPageToken).setPageSize(pageSize).execute(); + if (response.isEmpty()) break; + for (Log log: response.getLogs()) { + System.out.println(URLDecoder.decode( + log.getName().substring(resourcePrefixLength), "utf-8")); + } + nextPageToken = response.getNextPageToken(); + } while (!Strings.isNullOrEmpty(nextPageToken)); + System.out.println("Done."); } + // [END listlogs] - public static void main(String[] args) throws Exception { + /** + * Demonstrates the Cloud Logging API by listing the logs in a project. + * @param args The project ID. + * @throws IOException if a Cloud Logging API call fails because, say, the project ID is wrong + * or authorization fails. + */ + public static void main(String[] args) throws IOException { if (args.length != 1) { System.err.println(String.format("Usage: %s ", ListLogs.class.getSimpleName())); @@ -75,12 +109,7 @@ public static void main(String[] args) throws Exception { String projectId = args[0]; Logging service = getLoggingService(); - ListLogsResponse response = service.projects().logs().list(projectId) - .execute(); - System.out.println("RAW: " + response.toPrettyString()); - System.out.println("SIMPLE: " + - getSimpleLogNames(response.getLogs(), projectId)); - + listLogs(service, projectId); } } // [END all] diff --git a/logging/src/test/java/ListLogsTest.java b/logging/src/test/java/ListLogsTest.java new file mode 100644 index 00000000000..5f1bd3e26c1 --- /dev/null +++ b/logging/src/test/java/ListLogsTest.java @@ -0,0 +1,61 @@ +/** + * 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. + */ +import static com.jcabi.matchers.RegexMatchers.containsPattern; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +/** + * Tests the Cloud Logging sample. + */ +public class ListLogsTest { + private final ByteArrayOutputStream stdout = new ByteArrayOutputStream(); + private final ByteArrayOutputStream stderr = new ByteArrayOutputStream(); + private static final PrintStream REAL_OUT = System.out; + private static final PrintStream REAL_ERR = System.err; + + @Before + public void setUp() { + System.setOut(new PrintStream(stdout)); + System.setErr(new PrintStream(stderr)); + } + + @After + public void tearDown() { + System.setOut(ListLogsTest.REAL_OUT); + System.setErr(ListLogsTest.REAL_ERR); + } + + @Test + public void testUsage() throws Exception { + ListLogs.main(new String[] {}); + assertEquals("Usage: ListLogs \n", stderr.toString()); + } + + @Test + public void testListLogs() throws Exception { + ListLogs.main(new String[] {"cloud-samples-tests"}); + String out = stdout.toString(); + // Don't know what logs the test project will have. + assertThat(out, containsPattern("Done\\.")); + } +} From 8205819ca018259a4a8ee67b7f2b4d01ff8004af Mon Sep 17 00:00:00 2001 From: Sam Harbison Date: Thu, 3 Sep 2015 15:35:51 -0400 Subject: [PATCH 5/6] Remove .settings directory and add to .gitignore. --- logging/.gitignore | 3 +++ logging/.settings/org.eclipse.core.resources.prefs | 4 ---- logging/.settings/org.eclipse.jdt.core.prefs | 5 ----- logging/.settings/org.eclipse.m2e.core.prefs | 4 ---- 4 files changed, 3 insertions(+), 13 deletions(-) delete mode 100644 logging/.settings/org.eclipse.core.resources.prefs delete mode 100644 logging/.settings/org.eclipse.jdt.core.prefs delete mode 100644 logging/.settings/org.eclipse.m2e.core.prefs diff --git a/logging/.gitignore b/logging/.gitignore index 86dc131d1b2..95631ba702a 100644 --- a/logging/.gitignore +++ b/logging/.gitignore @@ -12,6 +12,9 @@ appengine-generated/ *.war *.ear +# Eclipse +.settings/ + # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* diff --git a/logging/.settings/org.eclipse.core.resources.prefs b/logging/.settings/org.eclipse.core.resources.prefs deleted file mode 100644 index f9fe34593fc..00000000000 --- a/logging/.settings/org.eclipse.core.resources.prefs +++ /dev/null @@ -1,4 +0,0 @@ -eclipse.preferences.version=1 -encoding//src/main/java=UTF-8 -encoding//src/test/java=UTF-8 -encoding/=UTF-8 diff --git a/logging/.settings/org.eclipse.jdt.core.prefs b/logging/.settings/org.eclipse.jdt.core.prefs deleted file mode 100644 index abec6ca389a..00000000000 --- a/logging/.settings/org.eclipse.jdt.core.prefs +++ /dev/null @@ -1,5 +0,0 @@ -eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 -org.eclipse.jdt.core.compiler.compliance=1.5 -org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning -org.eclipse.jdt.core.compiler.source=1.5 diff --git a/logging/.settings/org.eclipse.m2e.core.prefs b/logging/.settings/org.eclipse.m2e.core.prefs deleted file mode 100644 index f897a7f1cb2..00000000000 --- a/logging/.settings/org.eclipse.m2e.core.prefs +++ /dev/null @@ -1,4 +0,0 @@ -activeProfiles= -eclipse.preferences.version=1 -resolveWorkspaceProjects=true -version=1 From c0cf9669dbd1c34b2201822794d836974545c000 Mon Sep 17 00:00:00 2001 From: Sam Harbison Date: Fri, 4 Sep 2015 12:48:53 -0400 Subject: [PATCH 6/6] Remove .classpath, .gitignore, .project. --- logging/.classpath | 26 -------------------------- logging/.gitignore | 31 ------------------------------- logging/.project | 23 ----------------------- 3 files changed, 80 deletions(-) delete mode 100644 logging/.classpath delete mode 100644 logging/.gitignore delete mode 100644 logging/.project diff --git a/logging/.classpath b/logging/.classpath deleted file mode 100644 index 0a1daddd3e8..00000000000 --- a/logging/.classpath +++ /dev/null @@ -1,26 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/logging/.gitignore b/logging/.gitignore deleted file mode 100644 index 95631ba702a..00000000000 --- a/logging/.gitignore +++ /dev/null @@ -1,31 +0,0 @@ -# Google App Engine generated folder -appengine-generated/ - -# Java -*.class - -# Mobile Tools for Java (J2ME) -.mtj.tmp/ - -# Package Files # -*.jar -*.war -*.ear - -# Eclipse -.settings/ - -# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml -hs_err_pid* - -# maven -target/ -pom.xml.tag -pom.xml.releaseBackup -pom.xml.versionsBackup -pom.xml.next -release.properties -dependency-reduced-pom.xml -buildNumber.properties - -service-account.json diff --git a/logging/.project b/logging/.project deleted file mode 100644 index fce5b7aaee3..00000000000 --- a/logging/.project +++ /dev/null @@ -1,23 +0,0 @@ - - - cloud-logging-samples - - - - - - org.eclipse.jdt.core.javabuilder - - - - - org.eclipse.m2e.core.maven2Builder - - - - - - org.eclipse.jdt.core.javanature - org.eclipse.m2e.core.maven2Nature - -