|
| 1 | +/* |
| 2 | + Copyright 2017 Google Inc. |
| 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 | + |
| 17 | +package com.example.logging; |
| 18 | + |
| 19 | +import com.google.api.gax.paging.Page; |
| 20 | +import com.google.cloud.logging.LogEntry; |
| 21 | +import com.google.cloud.logging.Logging; |
| 22 | +import com.google.cloud.logging.Logging.EntryListOption; |
| 23 | +import com.google.cloud.logging.LoggingOptions; |
| 24 | + |
| 25 | +/** |
| 26 | + * List logs programmatically using the StackDriver Logging API. |
| 27 | + */ |
| 28 | +public class ListLogs { |
| 29 | + |
| 30 | + /** Expects an existing Stackdriver log name as an argument. */ |
| 31 | + public static void main(String... args) throws Exception { |
| 32 | + // [START listlogs] |
| 33 | + // Instantiates a client |
| 34 | + LoggingOptions options = LoggingOptions.getDefaultInstance(); |
| 35 | + |
| 36 | + String logName = args[0]; |
| 37 | + |
| 38 | + try (Logging logging = options.getService()) { |
| 39 | + |
| 40 | + String logFilter = "logName=projects/" + options.getProjectId() + "/logs/" + logName; |
| 41 | + |
| 42 | + // List log entries |
| 43 | + Page<LogEntry> entries = logging.listLogEntries( |
| 44 | + EntryListOption.filter(logFilter)); |
| 45 | + for (LogEntry logEntry : entries.iterateAll()) { |
| 46 | + System.out.println(logEntry); |
| 47 | + } |
| 48 | + // Use entries.getNextPage() to paginate |
| 49 | + } |
| 50 | + // [END listlogs] |
| 51 | + } |
| 52 | +} |
0 commit comments