Skip to content

Commit 8f0eb9b

Browse files
committed
Merge pull request GoogleCloudPlatform#29 from sharbison3/logging
Logging
2 parents b717cd3 + c0cf966 commit 8f0eb9b

File tree

4 files changed

+238
-0
lines changed

4 files changed

+238
-0
lines changed

logging/pom.xml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.google.cloud.logging.samples</groupId>
5+
<artifactId>cloud-logging-samples</artifactId>
6+
<packaging>jar</packaging>
7+
8+
<parent>
9+
<artifactId>doc-samples</artifactId>
10+
<groupId>com.google.cloud</groupId>
11+
<version>1.0.0</version>
12+
<relativePath>..</relativePath>
13+
</parent>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>com.google.apis</groupId>
18+
<artifactId>google-api-services-logging</artifactId>
19+
<version>v1beta3-rev4-1.20.0</version>
20+
</dependency>
21+
<dependency>
22+
<groupId>com.google.oauth-client</groupId>
23+
<artifactId>google-oauth-client</artifactId>
24+
<version>${project.oauth.version}</version>
25+
</dependency>
26+
<dependency>
27+
<groupId>com.google.http-client</groupId>
28+
<artifactId>google-http-client-jackson2</artifactId>
29+
<version>${project.http.version}</version>
30+
</dependency>
31+
<dependency>
32+
<groupId>com.google.oauth-client</groupId>
33+
<artifactId>google-oauth-client-jetty</artifactId>
34+
<version>${project.oauth.version}</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>junit</groupId>
38+
<artifactId>junit</artifactId>
39+
</dependency>
40+
<dependency>
41+
<groupId>com.jcabi</groupId>
42+
<artifactId>jcabi-matchers</artifactId>
43+
</dependency>
44+
</dependencies>
45+
46+
<build>
47+
<sourceDirectory>src/main/java</sourceDirectory>
48+
<plugins>
49+
<plugin>
50+
<groupId>org.apache.maven.plugins</groupId>
51+
<artifactId>maven-compiler-plugin</artifactId>
52+
<version>3.2</version>
53+
<configuration>
54+
<source>5</source>
55+
<target>5</target>
56+
</configuration>
57+
</plugin>
58+
</plugins>
59+
</build>
60+
61+
</project>

logging/src/main/java/ListLogs.java

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/**
2+
* Copyright (c) 2015 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
// [START imports]
17+
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
18+
import com.google.api.client.http.HttpTransport;
19+
import com.google.api.client.http.javanet.NetHttpTransport;
20+
import com.google.api.client.json.JsonFactory;
21+
import com.google.api.client.json.jackson2.JacksonFactory;
22+
import com.google.api.client.util.Strings;
23+
import com.google.api.services.logging.Logging;
24+
import com.google.api.services.logging.LoggingScopes;
25+
import com.google.api.services.logging.model.ListLogsResponse;
26+
import com.google.api.services.logging.model.Log;
27+
28+
import java.io.IOException;
29+
import java.net.URLDecoder;
30+
import java.util.Collections;
31+
import java.util.List;
32+
// [END imports]
33+
34+
/**
35+
* Cloud Logging Java API sample that lists the logs available to a project.
36+
* Uses the v1beta3 Cloud Logging API, version 1.20.0 or later.
37+
* See https://cloud.google.com/logging/docs/api/libraries/.
38+
*/
39+
public class ListLogs {
40+
41+
private static final List<String> LOGGING_SCOPES = Collections.singletonList(
42+
LoggingScopes.LOGGING_READ);
43+
44+
private static final String APPLICATION_NAME = "ListLogs sample";
45+
46+
/**
47+
* Returns an authorized Cloud Logging API service client that is usable
48+
* on Google App Engine, Google Compute Engine, workstations with the Google Cloud SDK,
49+
* and other computers if you install service account private credentials.
50+
* See https://cloud.google.com/logging/docs/api/tasks.
51+
*/
52+
// [START auth]
53+
public static Logging getLoggingService() throws IOException {
54+
HttpTransport transport = new NetHttpTransport();
55+
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
56+
GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory);
57+
if (credential.createScopedRequired()) {
58+
credential = credential.createScoped(LOGGING_SCOPES);
59+
}
60+
Logging service = new Logging.Builder(transport, jsonFactory, credential)
61+
.setApplicationName(APPLICATION_NAME).build();
62+
return service;
63+
}
64+
// [END auth]
65+
66+
/**
67+
* Lists the names of the logs visible to a project, which may require fetching multiple
68+
* pages of results from the Cloud Logging API. This method converts log resource names
69+
* ("/projects/PROJECTID/logs/SERVICENAME%2FLOGNAME") to simple log names ("SERVICENAME/LOGNAME").
70+
*
71+
* @param service The logging service client returned by getLoggingService.
72+
* @param projectId The project whose logs are to be listed.
73+
* @throws IOException If the Cloud Logging API fails because, for example, the project ID
74+
* doesn't exist or authorization fails.
75+
* See https://cloud.google.com//logging/docs/api/tasks/#java_sample_code.
76+
*/
77+
// [START listlogs]
78+
private static void listLogs(Logging service, String projectId) throws IOException {
79+
final int pageSize = 3;
80+
final int resourcePrefixLength = ("/projects/" + projectId + "/logs/").length();
81+
String nextPageToken = "";
82+
83+
do {
84+
ListLogsResponse response = service.projects().logs().list(projectId)
85+
.setPageToken(nextPageToken).setPageSize(pageSize).execute();
86+
if (response.isEmpty()) break;
87+
for (Log log: response.getLogs()) {
88+
System.out.println(URLDecoder.decode(
89+
log.getName().substring(resourcePrefixLength), "utf-8"));
90+
}
91+
nextPageToken = response.getNextPageToken();
92+
} while (!Strings.isNullOrEmpty(nextPageToken));
93+
System.out.println("Done.");
94+
}
95+
// [END listlogs]
96+
97+
/**
98+
* Demonstrates the Cloud Logging API by listing the logs in a project.
99+
* @param args The project ID.
100+
* @throws IOException if a Cloud Logging API call fails because, say, the project ID is wrong
101+
* or authorization fails.
102+
*/
103+
public static void main(String[] args) throws IOException {
104+
if (args.length != 1) {
105+
System.err.println(String.format("Usage: %s <project-name>",
106+
ListLogs.class.getSimpleName()));
107+
return;
108+
}
109+
110+
String projectId = args[0];
111+
Logging service = getLoggingService();
112+
listLogs(service, projectId);
113+
}
114+
}
115+
// [END all]
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* Copyright 2015 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
import static com.jcabi.matchers.RegexMatchers.containsPattern;
17+
import static org.junit.Assert.assertEquals;
18+
import static org.junit.Assert.assertThat;
19+
20+
import org.junit.After;
21+
import org.junit.Before;
22+
import org.junit.Test;
23+
24+
import java.io.ByteArrayOutputStream;
25+
import java.io.PrintStream;
26+
27+
/**
28+
* Tests the Cloud Logging sample.
29+
*/
30+
public class ListLogsTest {
31+
private final ByteArrayOutputStream stdout = new ByteArrayOutputStream();
32+
private final ByteArrayOutputStream stderr = new ByteArrayOutputStream();
33+
private static final PrintStream REAL_OUT = System.out;
34+
private static final PrintStream REAL_ERR = System.err;
35+
36+
@Before
37+
public void setUp() {
38+
System.setOut(new PrintStream(stdout));
39+
System.setErr(new PrintStream(stderr));
40+
}
41+
42+
@After
43+
public void tearDown() {
44+
System.setOut(ListLogsTest.REAL_OUT);
45+
System.setErr(ListLogsTest.REAL_ERR);
46+
}
47+
48+
@Test
49+
public void testUsage() throws Exception {
50+
ListLogs.main(new String[] {});
51+
assertEquals("Usage: ListLogs <project-name>\n", stderr.toString());
52+
}
53+
54+
@Test
55+
public void testListLogs() throws Exception {
56+
ListLogs.main(new String[] {"cloud-samples-tests"});
57+
String out = stdout.toString();
58+
// Don't know what logs the test project will have.
59+
assertThat(out, containsPattern("Done\\."));
60+
}
61+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<module>storage/xml-api/serviceaccount-appengine-sample</module>
2929
<module>storage/storage-transfer</module>
3030
<module>monitoring</module>
31+
<module>logging</module>
3132
</modules>
3233

3334
<build>

0 commit comments

Comments
 (0)