Skip to content

Commit c451a14

Browse files
author
        Sam Harbison
committed
Initial ListLogs sample code.
1 parent da52291 commit c451a14

2 files changed

Lines changed: 155 additions & 0 deletions

File tree

logging/pom.xml

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
16+
<repositories>
17+
<repository>
18+
<id>googleapis</id>
19+
<url>https://google-api-client-libraries.appspot.com/mavenrepo</url>
20+
</repository>
21+
</repositories>
22+
23+
<dependencies>
24+
<dependency>
25+
<groupId>com.google.apis</groupId>
26+
<artifactId>google-api-services-logging</artifactId>
27+
<version>v3beta1-rev3-1.20.0</version>
28+
</dependency>
29+
<dependency>
30+
<groupId>com.google.oauth-client</groupId>
31+
<artifactId>google-oauth-client</artifactId>
32+
<version>${project.oauth.version}</version>
33+
</dependency>
34+
<dependency>
35+
<groupId>com.google.http-client</groupId>
36+
<artifactId>google-http-client-jackson2</artifactId>
37+
<version>${project.http.version}</version>
38+
</dependency>
39+
<dependency>
40+
<groupId>com.google.oauth-client</groupId>
41+
<artifactId>google-oauth-client-jetty</artifactId>
42+
<version>${project.oauth.version}</version>
43+
</dependency>
44+
<dependency>
45+
<groupId>junit</groupId>
46+
<artifactId>junit</artifactId>
47+
</dependency>
48+
<dependency>
49+
<groupId>com.jcabi</groupId>
50+
<artifactId>jcabi-matchers</artifactId>
51+
</dependency>
52+
</dependencies>
53+
54+
<build>
55+
<sourceDirectory>src/main/java</sourceDirectory>
56+
<plugins>
57+
<plugin>
58+
<groupId>org.apache.maven.plugins</groupId>
59+
<artifactId>maven-compiler-plugin</artifactId>
60+
<version>3.2</version>
61+
<configuration>
62+
<source>5</source>
63+
<target>5</target>
64+
</configuration>
65+
</plugin>
66+
</plugins>
67+
</build>
68+
69+
</project>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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 all]
17+
import java.io.IOException;
18+
import java.io.UnsupportedEncodingException;
19+
import java.net.URLDecoder;
20+
import java.security.GeneralSecurityException;
21+
import java.util.ArrayList;
22+
import java.util.Collections;
23+
import java.util.List;
24+
25+
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
26+
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
27+
import com.google.api.client.json.jackson2.JacksonFactory;
28+
import com.google.api.services.logging.Logging;
29+
import com.google.api.services.logging.model.ListLogsResponse;
30+
import com.google.api.services.logging.model.Log;
31+
32+
/**
33+
* Cloud Logging Java API sample that lists the logs available to a project.
34+
* Uses the v1beta3 Cloud Logging API, version 1.20.0 or later.
35+
* See https://cloud.google.com/logging/docs/api/libraries/.
36+
*/
37+
public class ListLogs {
38+
39+
private static final List<String> LOGGING_SCOPES = Collections.singletonList(
40+
"https://www.googleapis.com/auth/logging.read");
41+
42+
private static final String APPLICATION_NAME = "ListLogs sample";
43+
44+
/** Returns an authorized Cloud Logging API service client. */
45+
public static Logging getLoggingService() throws GeneralSecurityException,
46+
IOException {
47+
GoogleCredential credential = GoogleCredential.getApplicationDefault()
48+
.createScoped(LOGGING_SCOPES);
49+
Logging service = new Logging.Builder(
50+
GoogleNetHttpTransport.newTrustedTransport(),
51+
JacksonFactory.getDefaultInstance(),
52+
credential).setApplicationName(APPLICATION_NAME).build();
53+
return service;
54+
}
55+
56+
/** Extract simple log names from URL-encoded resource names. */
57+
public static List<String> getSimpleLogNames(List<Log> logs,
58+
String projectId) throws UnsupportedEncodingException {
59+
final int RESOURCE_PREFIX_LENGTH = ("/projects/" + projectId + "/logs/")
60+
.length();
61+
List<String> logNames = new ArrayList<String>();
62+
for (Log log: logs) {
63+
logNames.add(URLDecoder.decode(log.getName(), "utf-8").substring(
64+
RESOURCE_PREFIX_LENGTH));
65+
}
66+
return logNames;
67+
}
68+
69+
public static void main(String[] args) throws Exception {
70+
if (args.length != 1) {
71+
System.err.println(String.format("Usage: %s <project-name>",
72+
ListLogs.class.getSimpleName()));
73+
return;
74+
}
75+
76+
String projectId = args[0];
77+
Logging service = getLoggingService();
78+
ListLogsResponse response = service.projects().logs().list(projectId)
79+
.execute();
80+
System.out.println("RAW: " + response.toPrettyString());
81+
System.out.println("SIMPLE: " +
82+
getSimpleLogNames(response.getLogs(), projectId));
83+
84+
}
85+
}
86+
// [END all]

0 commit comments

Comments
 (0)