-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from sharbison3/logging
Logging
- Loading branch information
Showing
4 changed files
with
238 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.google.cloud.logging.samples</groupId> | ||
<artifactId>cloud-logging-samples</artifactId> | ||
<packaging>jar</packaging> | ||
|
||
<parent> | ||
<artifactId>doc-samples</artifactId> | ||
<groupId>com.google.cloud</groupId> | ||
<version>1.0.0</version> | ||
<relativePath>..</relativePath> | ||
</parent> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.google.apis</groupId> | ||
<artifactId>google-api-services-logging</artifactId> | ||
<version>v1beta3-rev4-1.20.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.oauth-client</groupId> | ||
<artifactId>google-oauth-client</artifactId> | ||
<version>${project.oauth.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.http-client</groupId> | ||
<artifactId>google-http-client-jackson2</artifactId> | ||
<version>${project.http.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.oauth-client</groupId> | ||
<artifactId>google-oauth-client-jetty</artifactId> | ||
<version>${project.oauth.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.jcabi</groupId> | ||
<artifactId>jcabi-matchers</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<sourceDirectory>src/main/java</sourceDirectory> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.2</version> | ||
<configuration> | ||
<source>5</source> | ||
<target>5</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String> 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 <project-name>", | ||
ListLogs.class.getSimpleName())); | ||
return; | ||
} | ||
|
||
String projectId = args[0]; | ||
Logging service = getLoggingService(); | ||
listLogs(service, projectId); | ||
} | ||
} | ||
// [END all] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <project-name>\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\\.")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters