Search in sources :

Example 1 with StringPayload

use of com.google.cloud.logging.Payload.StringPayload in project google-cloud-java by GoogleCloudPlatform.

the class BaseSystemTest method testLoggingHandler.

@Test
public void testLoggingHandler() throws InterruptedException {
    String logName = formatForTest("test-logging-handler");
    LoggingOptions options = logging().getOptions();
    LoggingHandler handler = new LoggingHandler(logName, options);
    handler.setLevel(Level.INFO);
    Logger logger = Logger.getLogger(getClass().getName());
    logger.addHandler(handler);
    logger.setLevel(Level.INFO);
    logger.info("Message");
    Iterator<LogEntry> iterator = logging().listLogEntries(EntryListOption.filter("logName:" + logName)).iterateAll().iterator();
    while (!iterator.hasNext()) {
        Thread.sleep(500L);
        iterator = logging().listLogEntries(EntryListOption.filter("logName:" + logName)).iterateAll().iterator();
    }
    assertTrue(iterator.hasNext());
    LogEntry entry = iterator.next();
    assertTrue(entry.getPayload() instanceof StringPayload);
    assertTrue(entry.<StringPayload>getPayload().getData().contains("Message"));
    assertEquals(logName, entry.getLogName());
    assertEquals(ImmutableMap.of("levelName", "INFO", "levelValue", String.valueOf(Level.INFO.intValue())), entry.getLabels());
    assertEquals("global", entry.getResource().getType());
    assertEquals(ImmutableMap.of("project_id", options.getProjectId()), entry.getResource().getLabels());
    assertNull(entry.getHttpRequest());
    assertEquals(Severity.INFO, entry.getSeverity());
    assertNull(entry.getOperation());
    assertNotNull(entry.getInsertId());
    assertNotNull(entry.getTimestamp());
    assertFalse(iterator.hasNext());
    logger.removeHandler(handler);
    logging().deleteLog(logName);
}
Also used : StringPayload(com.google.cloud.logging.Payload.StringPayload) Logger(java.util.logging.Logger) Test(org.junit.Test)

Example 2 with StringPayload

use of com.google.cloud.logging.Payload.StringPayload in project google-cloud-java by GoogleCloudPlatform.

the class BaseSystemTest method testWriteAndListLogEntriesAsync.

@Test
public void testWriteAndListLogEntriesAsync() throws InterruptedException, ExecutionException {
    String logName = formatForTest("test-write-log-entries-async-log");
    String filter = "logName = projects/" + logging().getOptions().getProjectId() + "/logs/" + logName;
    StringPayload firstPayload = StringPayload.of("stringPayload");
    LogEntry firstEntry = LogEntry.newBuilder(firstPayload).setSeverity(Severity.ALERT).build();
    ProtoPayload secondPayload = ProtoPayload.of(Any.pack(StringValue.newBuilder().setValue("protoPayload").build()));
    LogEntry secondEntry = LogEntry.newBuilder(secondPayload).setSeverity(Severity.DEBUG).build();
    logging().write(ImmutableList.of(firstEntry, secondEntry), WriteOption.labels(ImmutableMap.of("key1", "value1")), WriteOption.resource(MonitoredResource.newBuilder("global").build()), WriteOption.logName(logName));
    logging().flush();
    EntryListOption[] options = { EntryListOption.filter(filter), EntryListOption.pageSize(1) };
    AsyncPage<LogEntry> page = logging().listLogEntriesAsync(options).get();
    while (Iterators.size(page.iterateAll().iterator()) < 2) {
        Thread.sleep(500);
        page = logging().listLogEntriesAsync(options).get();
    }
    Iterator<LogEntry> iterator = page.iterateAll().iterator();
    assertTrue(iterator.hasNext());
    LogEntry entry = iterator.next();
    assertEquals(firstPayload, entry.getPayload());
    assertEquals(logName, entry.getLogName());
    assertEquals(ImmutableMap.of("key1", "value1"), entry.getLabels());
    assertEquals("global", entry.getResource().getType());
    assertNull(entry.getHttpRequest());
    assertEquals(Severity.ALERT, entry.getSeverity());
    assertNull(entry.getOperation());
    assertNotNull(entry.getInsertId());
    assertNotNull(entry.getTimestamp());
    assertTrue(iterator.hasNext());
    entry = iterator.next();
    assertEquals(secondPayload, entry.getPayload());
    assertEquals(logName, entry.getLogName());
    assertEquals(ImmutableMap.of("key1", "value1"), entry.getLabels());
    assertEquals("global", entry.getResource().getType());
    assertNull(entry.getOperation());
    assertEquals(Severity.DEBUG, entry.getSeverity());
    assertNull(entry.getHttpRequest());
    assertNotNull(entry.getInsertId());
    assertNotNull(entry.getTimestamp());
    assertTrue(logging().deleteLogAsync(logName).get());
}
Also used : ProtoPayload(com.google.cloud.logging.Payload.ProtoPayload) StringPayload(com.google.cloud.logging.Payload.StringPayload) EntryListOption(com.google.cloud.logging.Logging.EntryListOption) Test(org.junit.Test)

Example 3 with StringPayload

use of com.google.cloud.logging.Payload.StringPayload in project google-cloud-java by GoogleCloudPlatform.

the class BaseSystemTest method testWriteAndListLogEntries.

@Test
public void testWriteAndListLogEntries() throws InterruptedException {
    String logName = formatForTest("test-write-log-entries-log");
    String filter = "logName = projects/" + logging().getOptions().getProjectId() + "/logs/" + logName;
    StringPayload firstPayload = StringPayload.of("stringPayload");
    LogEntry firstEntry = LogEntry.newBuilder(firstPayload).addLabel("key1", "value1").setLogName(logName).setHttpRequest(HttpRequest.newBuilder().setStatus(500).build()).setResource(MonitoredResource.newBuilder("global").build()).build();
    JsonPayload secondPayload = JsonPayload.of(ImmutableMap.<String, Object>of("jsonKey", "jsonValue"));
    LogEntry secondEntry = LogEntry.newBuilder(secondPayload).addLabel("key2", "value2").setLogName(logName).setOperation(Operation.of("operationId", "operationProducer")).setResource(MonitoredResource.newBuilder("cloudsql_database").build()).build();
    logging().write(ImmutableList.of(firstEntry));
    logging().write(ImmutableList.of(secondEntry));
    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);
    }
    Iterator<LogEntry> iterator = page.iterateAll().iterator();
    assertTrue(iterator.hasNext());
    LogEntry entry = iterator.next();
    assertEquals(firstPayload, entry.getPayload());
    assertEquals(logName, entry.getLogName());
    assertEquals(ImmutableMap.of("key1", "value1"), entry.getLabels());
    assertEquals("global", entry.getResource().getType());
    assertEquals(HttpRequest.newBuilder().setStatus(500).build(), entry.getHttpRequest());
    assertEquals(Severity.DEFAULT, entry.getSeverity());
    assertNull(entry.getOperation());
    assertNotNull(entry.getInsertId());
    assertNotNull(entry.getTimestamp());
    assertTrue(iterator.hasNext());
    entry = iterator.next();
    assertEquals(secondPayload, entry.getPayload());
    assertEquals(logName, entry.getLogName());
    assertEquals(ImmutableMap.of("key2", "value2"), entry.getLabels());
    assertEquals("cloudsql_database", entry.getResource().getType());
    assertEquals(Operation.of("operationId", "operationProducer"), entry.getOperation());
    assertEquals(Severity.DEFAULT, entry.getSeverity());
    assertNull(entry.getHttpRequest());
    assertNotNull(entry.getInsertId());
    assertNotNull(entry.getTimestamp());
    options = new EntryListOption[] { EntryListOption.filter(filter), EntryListOption.sortOrder(SortingField.TIMESTAMP, SortingOrder.DESCENDING) };
    page = logging().listLogEntries(options);
    while (Iterators.size(page.iterateAll().iterator()) < 2) {
        Thread.sleep(500);
        page = logging().listLogEntries(options);
    }
    iterator = page.iterateAll().iterator();
    Long lastTimestamp = iterator.next().getTimestamp();
    while (iterator.hasNext()) {
        assertTrue(iterator.next().getTimestamp() <= lastTimestamp);
    }
    assertTrue(logging().deleteLog(logName));
}
Also used : StringPayload(com.google.cloud.logging.Payload.StringPayload) JsonPayload(com.google.cloud.logging.Payload.JsonPayload) EntryListOption(com.google.cloud.logging.Logging.EntryListOption) Test(org.junit.Test)

Example 4 with StringPayload

use of com.google.cloud.logging.Payload.StringPayload in project google-cloud-java by GoogleCloudPlatform.

the class BaseSystemTest method testSyncLoggingHandler.

@Test
public void testSyncLoggingHandler() throws InterruptedException {
    String logName = formatForTest("test-sync-logging-handler");
    LoggingOptions options = logging().getOptions();
    MonitoredResource resource = MonitoredResource.of("gce_instance", ImmutableMap.of("project_id", options.getProjectId(), "instance_id", "instance", "zone", "us-central1-a"));
    LoggingHandler handler = new LoggingHandler(logName, options, resource);
    handler.setLevel(Level.WARNING);
    handler.setSynchronicity(Synchronicity.SYNC);
    Logger logger = Logger.getLogger(getClass().getName());
    logger.addHandler(handler);
    logger.setLevel(Level.WARNING);
    logger.warning("Message");
    Iterator<LogEntry> iterator = logging().listLogEntries(EntryListOption.filter("logName:" + logName)).iterateAll().iterator();
    while (!iterator.hasNext()) {
        Thread.sleep(500L);
        iterator = logging().listLogEntries(EntryListOption.filter("logName:" + logName)).iterateAll().iterator();
    }
    assertTrue(iterator.hasNext());
    LogEntry entry = iterator.next();
    assertTrue(entry.getPayload() instanceof StringPayload);
    assertTrue(entry.<StringPayload>getPayload().getData().contains("Message"));
    assertEquals(logName, entry.getLogName());
    assertEquals(ImmutableMap.of("levelName", "WARNING", "levelValue", String.valueOf(Level.WARNING.intValue())), entry.getLabels());
    assertEquals(resource, entry.getResource());
    assertNull(entry.getHttpRequest());
    assertEquals(Severity.WARNING, entry.getSeverity());
    assertNull(entry.getOperation());
    assertNotNull(entry.getInsertId());
    assertNotNull(entry.getTimestamp());
    assertFalse(iterator.hasNext());
    logger.removeHandler(handler);
    logging().deleteLog(logName);
}
Also used : MonitoredResource(com.google.cloud.MonitoredResource) StringPayload(com.google.cloud.logging.Payload.StringPayload) Logger(java.util.logging.Logger) Test(org.junit.Test)

Aggregations

StringPayload (com.google.cloud.logging.Payload.StringPayload)4 Test (org.junit.Test)4 EntryListOption (com.google.cloud.logging.Logging.EntryListOption)2 Logger (java.util.logging.Logger)2 MonitoredResource (com.google.cloud.MonitoredResource)1 JsonPayload (com.google.cloud.logging.Payload.JsonPayload)1 ProtoPayload (com.google.cloud.logging.Payload.ProtoPayload)1