|
| 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