Search in sources :

Example 6 with LogEntry

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

the class LoggingSnippets method listLogEntriesAsync.

/**
   * Example of asynchronously listing log entries for a specific log.
   */
// [TARGET listLogEntriesAsync(EntryListOption...)]
// [VARIABLE "logName=projects/my_project_id/logs/my_log_name"]
public Page<LogEntry> listLogEntriesAsync(String filter) throws ExecutionException, InterruptedException {
    // [START listLogEntriesAsync]
    Future<AsyncPage<LogEntry>> future = logging.listLogEntriesAsync(EntryListOption.filter(filter));
    // ...
    AsyncPage<LogEntry> entries = future.get();
    for (LogEntry entry : entries.iterateAll()) {
    // do something with the entry
    }
    // [END listLogEntriesAsync]
    return entries;
}
Also used : AsyncPage(com.google.api.gax.paging.AsyncPage) LogEntry(com.google.cloud.logging.LogEntry)

Example 7 with LogEntry

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

the class LoggingAppender method append.

@Override
protected void append(ILoggingEvent e) {
    LogEntry logEntry = logEntryFor(e);
    getLogging().write(Collections.singleton(logEntry), defaultWriteOptions);
}
Also used : LogEntry(com.google.cloud.logging.LogEntry)

Example 8 with LogEntry

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

the class LoggingAppender method logEntryFor.

private LogEntry logEntryFor(ILoggingEvent e) {
    String payload = e.getFormattedMessage();
    Level level = e.getLevel();
    LogEntry.Builder builder = LogEntry.newBuilder(Payload.StringPayload.of(payload)).setTimestamp(e.getTimeStamp()).setSeverity(severityFor(level));
    builder.addLabel(LEVEL_NAME_KEY, level.toString()).addLabel(LEVEL_VALUE_KEY, String.valueOf(level.toInt()));
    if (loggingEnhancers != null) {
        for (LoggingEnhancer enhancer : loggingEnhancers) {
            enhancer.enhanceLogEntry(builder);
        }
    }
    return builder.build();
}
Also used : LoggingEnhancer(com.google.cloud.logging.LoggingEnhancer) Level(ch.qos.logback.classic.Level) LogEntry(com.google.cloud.logging.LogEntry)

Example 9 with LogEntry

use of com.google.cloud.logging.LogEntry in project jetty-runtime by GoogleCloudPlatform.

the class LoggingIntegrationTest method testLogging.

@Test
@RemoteOnly
public void testLogging() throws Exception {
    // Create unique ID to relate request with log entries
    String id = Long.toHexString(System.nanoTime());
    // Hit the DumpServlet that will log the request path
    URI target = getUri().resolve("/dump/info/" + id);
    HttpURLConnection http = HttpUrlUtil.openTo(target);
    assertThat(http.getResponseCode(), is(200));
    String responseBody = HttpUrlUtil.getResponseBody(http);
    List<String> lines = new BufferedReader(new StringReader(responseBody)).lines().collect(Collectors.toList());
    assertThat(lines.stream().filter(s -> s.startsWith("requestURI=")).findFirst().get(), containsString(id));
    String traceId = lines.stream().filter(s -> s.startsWith("X-Cloud-Trace-Context: ")).findFirst().get().split("[ /]")[1];
    assertThat(traceId, Matchers.notNullValue());
    LoggingOptions options = LoggingOptions.getDefaultInstance();
    String filter = "resource.type=gae_app AND resource.labels.module_id=smoke" + " AND textPayload:" + id;
    int expected = 2;
    try (Logging logging = options.getService()) {
        Page<LogEntry> entries = logging.listLogEntries(EntryListOption.filter(filter));
        for (LogEntry entry : entries.iterateAll()) {
            if (entry.getSeverity() == Severity.INFO) {
                assertThat(entry.getLogName(), is("gae_app.log"));
                assertThat(entry.getResource().getType(), is("gae_app"));
                assertThat(entry.getResource().getLabels().get("module_id"), is("smoke"));
                assertThat(entry.getResource().getLabels().get("zone"), Matchers.notNullValue());
                assertThat(entry.getLabels().get("appengine.googleapis.com/trace_id"), is(traceId));
                assertThat(entry.getLabels().get("appengine.googleapis.com/instance_name"), Matchers.notNullValue());
                if (entry.getPayload().toString().contains("JUL.info:/dump/info/")) {
                    expected--;
                    assertThat(entry.getPayload().toString(), Matchers.containsString(id));
                }
                if (entry.getPayload().toString().contains("ServletContext.log:/dump/info")) {
                    expected--;
                    assertThat(entry.getPayload().toString(), Matchers.containsString(id));
                }
            }
        }
        assertThat(expected, is(0));
    }
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) HttpURLConnection(java.net.HttpURLConnection) Page(com.google.api.gax.paging.Page) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) LogEntry(com.google.cloud.logging.LogEntry) HttpUrlUtil(com.google.cloud.runtime.jetty.util.HttpUrlUtil) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) Severity(com.google.cloud.logging.Severity) Collectors(java.util.stream.Collectors) List(java.util.List) StringReader(java.io.StringReader) LoggingOptions(com.google.cloud.logging.LoggingOptions) EntryListOption(com.google.cloud.logging.Logging.EntryListOption) AbstractIntegrationTest(com.google.cloud.runtime.jetty.test.AbstractIntegrationTest) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Logging(com.google.cloud.logging.Logging) BufferedReader(java.io.BufferedReader) URI(java.net.URI) Assert(org.junit.Assert) RemoteOnly(com.google.cloud.runtime.jetty.test.annotation.RemoteOnly) Logging(com.google.cloud.logging.Logging) HttpURLConnection(java.net.HttpURLConnection) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) LoggingOptions(com.google.cloud.logging.LoggingOptions) URI(java.net.URI) LogEntry(com.google.cloud.logging.LogEntry) RemoteOnly(com.google.cloud.runtime.jetty.test.annotation.RemoteOnly) Test(org.junit.Test) AbstractIntegrationTest(com.google.cloud.runtime.jetty.test.AbstractIntegrationTest)

Aggregations

LogEntry (com.google.cloud.logging.LogEntry)9 Test (org.junit.Test)5 LoggingEvent (ch.qos.logback.classic.spi.LoggingEvent)4 Timestamp (com.google.cloud.Timestamp)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 Logging (com.google.cloud.logging.Logging)2 LoggingOptions (com.google.cloud.logging.LoggingOptions)2 Level (ch.qos.logback.classic.Level)1 ThresholdFilter (ch.qos.logback.classic.filter.ThresholdFilter)1 AsyncPage (com.google.api.gax.paging.AsyncPage)1 Page (com.google.api.gax.paging.Page)1 EntryListOption (com.google.cloud.logging.Logging.EntryListOption)1 WriteOption (com.google.cloud.logging.Logging.WriteOption)1 LoggingEnhancer (com.google.cloud.logging.LoggingEnhancer)1 Severity (com.google.cloud.logging.Severity)1 AbstractIntegrationTest (com.google.cloud.runtime.jetty.test.AbstractIntegrationTest)1 RemoteOnly (com.google.cloud.runtime.jetty.test.annotation.RemoteOnly)1 HttpUrlUtil (com.google.cloud.runtime.jetty.util.HttpUrlUtil)1 BufferedReader (java.io.BufferedReader)1 StringReader (java.io.StringReader)1