diff --git a/logging/pom.xml b/logging/pom.xml new file mode 100644 index 00000000000..eb803b986bb --- /dev/null +++ b/logging/pom.xml @@ -0,0 +1,61 @@ + + 4.0.0 + com.google.cloud.logging.samples + cloud-logging-samples + jar + + + doc-samples + com.google.cloud + 1.0.0 + .. + + + + + com.google.apis + google-api-services-logging + v1beta3-rev4-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..b49d3c4dbf8 --- /dev/null +++ b/logging/src/main/java/ListLogs.java @@ -0,0 +1,115 @@ +/** + * 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 imports] +import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; +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. + * See https://cloud.google.com/logging/docs/api/libraries/. + */ +public class ListLogs { + + private static final List LOGGING_SCOPES = Collections.singletonList( + LoggingScopes.LOGGING_READ); + + private static final String APPLICATION_NAME = "ListLogs sample"; + + /** + * 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] + + /** + * 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] + + /** + * 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())); + return; + } + + String projectId = args[0]; + Logging service = getLoggingService(); + 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\\.")); + } +} 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