diff --git a/logging/cloud-client/src/main/java/com/example/logging/ListLogs.java b/logging/cloud-client/src/main/java/com/example/logging/ListLogs.java index 267efce5d76..9599f46d786 100644 --- a/logging/cloud-client/src/main/java/com/example/logging/ListLogs.java +++ b/logging/cloud-client/src/main/java/com/example/logging/ListLogs.java @@ -39,13 +39,16 @@ public static void main(String... args) throws Exception { String logFilter = "logName=projects/" + options.getProjectId() + "/logs/" + logName; - // List log entries + // List all log entries Page entries = logging.listLogEntries( EntryListOption.filter(logFilter)); - for (LogEntry logEntry : entries.iterateAll()) { - System.out.println(logEntry); - } - // Use entries.getNextPage() to paginate + do { + for (LogEntry logEntry : entries.iterateAll()) { + System.out.println(logEntry); + } + entries = entries.getNextPage(); + } while(entries != null); + } // [END listlogs] } diff --git a/logging/cloud-client/src/test/java/com/example/logging/LoggingIT.java b/logging/cloud-client/src/test/java/com/example/logging/LoggingIT.java index 1abbca0906b..598822c869f 100644 --- a/logging/cloud-client/src/test/java/com/example/logging/LoggingIT.java +++ b/logging/cloud-client/src/test/java/com/example/logging/LoggingIT.java @@ -39,6 +39,9 @@ @SuppressWarnings("checkstyle:abbreviationaswordinname") public class LoggingIT { + private final String QUICKSTART_LOG = "my-log"; + private final String TEST_WRITE_LOG = "test-log"; + private ByteArrayOutputStream bout; private PrintStream out; private Logging logging = LoggingOptions.getDefaultInstance().getService(); @@ -56,37 +59,36 @@ public void setUp() { @After public void tearDown() { + // Clean up created logs + deleteLog(QUICKSTART_LOG); + deleteLog(TEST_WRITE_LOG); + System.setOut(null); } @Test public void testQuickstart() throws Exception { - String logName = "my-log"; - deleteLog(logName); - QuickstartSample.main(logName); + QuickstartSample.main(QUICKSTART_LOG); String got = bout.toString(); assertThat(got).contains("Logged: Hello, world!"); - deleteLog(logName); } - @Test(timeout = 10000) + @Test(timeout = 20000) public void testWriteAndListLogs() throws Exception { - String logName = "test-log"; - deleteLog(logName); // write a log entry LogEntry entry = LogEntry.newBuilder(StringPayload.of("Hello world again")) - .setLogName(logName) + .setLogName(TEST_WRITE_LOG) .setResource(MonitoredResource.newBuilder("global").build()) .build(); logging.write(Collections.singleton(entry)); // flush out log immediately logging.flush(); bout.reset(); + // Check if the log is listed yet while (bout.toString().isEmpty()) { - ListLogs.main(logName); + ListLogs.main(TEST_WRITE_LOG); Thread.sleep(1000); } assertThat(bout.toString().contains("Hello world again")).isTrue(); - deleteLog(logName); } }