@Override public void run(Logging logging, String filter) { Page<LogEntry> entryPage; if (filter == null) { entryPage = logging.listLogEntries(); } else { entryPage = logging.listLogEntries(EntryListOption.filter(filter)); } for (LogEntry entry : entryPage.iterateAll()) { System.out.println(entry); } }
/** Example of listing log entries for a specific log. */ // [TARGET listLogEntries(EntryListOption...)] // [VARIABLE "logName=projects/my_project_id/logs/my_log_name"] public Page<LogEntry> listLogEntries(String filter) { // [START logging_list_log_entries] Page<LogEntry> entries = logging.listLogEntries(EntryListOption.filter(filter)); for (LogEntry entry : entries.iterateAll()) { // do something with the entry } // [END logging_list_log_entries] return entries; }
@Test public void testListLogEntries() { String cursor = "cursor"; EasyMock.replay(rpcFactoryMock); logging = options.getService(); ListLogEntriesRequest request = ListLogEntriesRequest.newBuilder().addProjectIds(PROJECT).build(); List<LogEntry> entriesList = ImmutableList.of(LOG_ENTRY1, LOG_ENTRY2); ListLogEntriesResponse response = ListLogEntriesResponse.newBuilder() .setNextPageToken(cursor) .addAllEntries(Lists.transform(entriesList, LogEntry.toPbFunction(PROJECT))) .build(); ApiFuture<ListLogEntriesResponse> futureResponse = ApiFutures.immediateFuture(response); EasyMock.expect(loggingRpcMock.list(request)).andReturn(futureResponse); EasyMock.replay(loggingRpcMock); Page<LogEntry> page = logging.listLogEntries(); assertEquals(cursor, page.getNextPageToken()); assertArrayEquals(entriesList.toArray(), Iterables.toArray(page.getValues(), LogEntry.class)); }
public static void main(String... args) throws Exception { // Create a service object // Credentials are inferred from the environment LoggingOptions options = LoggingOptions.getDefaultInstance(); try (Logging logging = options.getService()) { // Create a log entry LogEntry firstEntry = LogEntry.newBuilder(StringPayload.of("message")) .setLogName("test-log") .setResource( MonitoredResource.newBuilder("global") .addLabel("project_id", options.getProjectId()) .build()) .build(); logging.write(Collections.singleton(firstEntry)); // List log entries Page<LogEntry> entries = logging.listLogEntries( EntryListOption.filter( "logName=projects/" + options.getProjectId() + "/logs/test-log")); for (LogEntry logEntry : entries.iterateAll()) { System.out.println(logEntry); } } } }
@Test public void testListLogEntriesEmpty() { String cursor = "cursor"; EasyMock.replay(rpcFactoryMock); logging = options.getService(); ListLogEntriesRequest request = ListLogEntriesRequest.newBuilder().addProjectIds(PROJECT).build(); List<LogEntry> entriesList = ImmutableList.of(); ListLogEntriesResponse response = ListLogEntriesResponse.newBuilder() .setNextPageToken(cursor) .addAllEntries(Lists.transform(entriesList, LogEntry.toPbFunction(PROJECT))) .build(); ApiFuture<ListLogEntriesResponse> futureResponse = ApiFutures.immediateFuture(response); EasyMock.expect(loggingRpcMock.list(request)).andReturn(futureResponse); EasyMock.replay(loggingRpcMock); Page<LogEntry> page = logging.listLogEntries(); assertEquals(cursor, page.getNextPageToken()); assertArrayEquals(entriesList.toArray(), Iterables.toArray(page.getValues(), LogEntry.class)); }
String filter = createEqualityFilter("logName", logName); Iterator<LogEntry> iterator = logging().listLogEntries(EntryListOption.filter(filter)).iterateAll().iterator(); while (!iterator.hasNext()) { Thread.sleep(500L); iterator = logging().listLogEntries(EntryListOption.filter(filter)).iterateAll().iterator();
@Test public void testListLogEntriesWithOptions() { String cursor = "cursor"; EasyMock.replay(rpcFactoryMock); logging = options.getService(); ListLogEntriesRequest request = ListLogEntriesRequest.newBuilder() .addProjectIds(PROJECT) .setOrderBy("timestamp desc") .setFilter("logName:syslog") .build(); List<LogEntry> entriesList = ImmutableList.of(LOG_ENTRY1, LOG_ENTRY2); ListLogEntriesResponse response = ListLogEntriesResponse.newBuilder() .setNextPageToken(cursor) .addAllEntries(Lists.transform(entriesList, LogEntry.toPbFunction(PROJECT))) .build(); ApiFuture<ListLogEntriesResponse> futureResponse = ApiFutures.immediateFuture(response); EasyMock.expect(loggingRpcMock.list(request)).andReturn(futureResponse); EasyMock.replay(loggingRpcMock); Page<LogEntry> page = logging.listLogEntries( EntryListOption.filter("logName:syslog"), EntryListOption.sortOrder(SortingField.TIMESTAMP, Logging.SortingOrder.DESCENDING)); assertEquals(cursor, page.getNextPageToken()); assertArrayEquals(entriesList.toArray(), Iterables.toArray(page.getValues(), LogEntry.class)); }
EasyMock.expect(loggingRpcMock.list(request2)).andReturn(futureResponse2); EasyMock.replay(loggingRpcMock); Page<LogEntry> page = logging.listLogEntries(); assertEquals(cursor1, page.getNextPageToken()); assertArrayEquals(
String filter = createEqualityFilter("logName", logName); Iterator<LogEntry> iterator = logging().listLogEntries(EntryListOption.filter(filter)).iterateAll().iterator(); while (!iterator.hasNext()) { Thread.sleep(500L); iterator = logging().listLogEntries(EntryListOption.filter(filter)).iterateAll().iterator();
String filter = createEqualityFilter("logName", logName); EntryListOption[] options = {EntryListOption.filter(filter), EntryListOption.pageSize(1)}; Page<LogEntry> page = logging().listLogEntries(options); while (Iterators.size(page.iterateAll().iterator()) < 2) { Thread.sleep(500); page = logging().listLogEntries(options); EntryListOption.sortOrder(SortingField.TIMESTAMP, SortingOrder.DESCENDING) }; page = logging().listLogEntries(options); while (Iterators.size(page.iterateAll().iterator()) < 2) { Thread.sleep(500); page = logging().listLogEntries(options);
/** Expects an existing Stackdriver log name as an argument. */ public static void main(String... args) throws Exception { // [START logging_list_log_entries] // Instantiates a client LoggingOptions options = LoggingOptions.getDefaultInstance(); String logName = args[0]; try (Logging logging = options.getService()) { String logFilter = "logName=projects/" + options.getProjectId() + "/logs/" + logName; // List all log entries Page<LogEntry> entries = logging.listLogEntries( EntryListOption.filter(logFilter)); do { for (LogEntry logEntry : entries.iterateAll()) { System.out.println(logEntry); } entries = entries.getNextPage(); } while (entries != null); } // [END logging_list_log_entries] } }