Search in sources :

Example 1 with Logging

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

the class LoggingExample method main.

@SuppressWarnings("unchecked")
public static void main(String... args) throws Exception {
    if (args.length < 1) {
        System.out.println("Missing required project id and action");
        printUsage();
        return;
    }
    LoggingOptions.Builder optionsBuilder = LoggingOptions.newBuilder();
    LoggingAction action;
    String actionName;
    if (args.length >= 2 && !ACTIONS.containsKey(args[0])) {
        actionName = args[1];
        optionsBuilder.setProjectId(args[0]);
        action = ACTIONS.get(args[1]);
        args = Arrays.copyOfRange(args, 2, args.length);
    } else {
        actionName = args[0];
        action = ACTIONS.get(args[0]);
        args = Arrays.copyOfRange(args, 1, args.length);
    }
    if (action == null) {
        System.out.println("Unrecognized action.");
        printUsage();
        return;
    }
    try (Logging logging = optionsBuilder.build().getService()) {
        Object arg;
        try {
            arg = action.parse(args);
        } catch (IllegalArgumentException ex) {
            System.out.printf("Invalid input for action '%s'. %s%n", actionName, ex.getMessage());
            System.out.printf("Expected: %s%n", action.params());
            return;
        } catch (Exception ex) {
            System.out.println("Failed to parse arguments.");
            ex.printStackTrace();
            return;
        }
        action.run(logging, arg);
    }
}
Also used : Logging(com.google.cloud.logging.Logging) LoggingOptions(com.google.cloud.logging.LoggingOptions)

Example 2 with Logging

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

the class CreateAndListSinks method main.

public static void main(String... args) throws Exception {
    // Credentials are inferred from the environment
    try (Logging logging = LoggingOptions.getDefaultInstance().getService()) {
        // Create a sink to back log entries to a BigQuery dataset
        SinkInfo sinkInfo = SinkInfo.newBuilder("test-sink", DatasetDestination.of("test-dataset")).setFilter("severity >= ERROR").build();
        logging.create(sinkInfo);
        // List sinks
        Page<Sink> sinks = logging.listSinks();
        for (Sink sink : sinks.iterateAll()) {
            System.out.println(sink);
        }
    }
}
Also used : Logging(com.google.cloud.logging.Logging) Sink(com.google.cloud.logging.Sink) SinkInfo(com.google.cloud.logging.SinkInfo)

Example 3 with Logging

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

the class WriteAndListLogEntries method main.

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);
        }
    }
}
Also used : Logging(com.google.cloud.logging.Logging) LoggingOptions(com.google.cloud.logging.LoggingOptions) LogEntry(com.google.cloud.logging.LogEntry)

Example 4 with Logging

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

the class CreateAndListMetrics method main.

public static void main(String... args) throws Exception {
    // Credentials are inferred from the environment
    try (Logging logging = LoggingOptions.getDefaultInstance().getService()) {
        // Create a metric
        MetricInfo metricInfo = MetricInfo.newBuilder("test-metric", "severity >= ERROR").setDescription("Log entries with severity higher or equal to ERROR").build();
        logging.create(metricInfo);
        // List metrics
        Page<Metric> metrics = logging.listMetrics();
        for (Metric metric : metrics.iterateAll()) {
            System.out.println(metric);
        }
    }
}
Also used : Logging(com.google.cloud.logging.Logging) MetricInfo(com.google.cloud.logging.MetricInfo) Metric(com.google.cloud.logging.Metric)

Example 5 with Logging

use of com.google.cloud.logging.Logging 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

Logging (com.google.cloud.logging.Logging)5 LoggingOptions (com.google.cloud.logging.LoggingOptions)3 LogEntry (com.google.cloud.logging.LogEntry)2 Page (com.google.api.gax.paging.Page)1 EntryListOption (com.google.cloud.logging.Logging.EntryListOption)1 Metric (com.google.cloud.logging.Metric)1 MetricInfo (com.google.cloud.logging.MetricInfo)1 Severity (com.google.cloud.logging.Severity)1 Sink (com.google.cloud.logging.Sink)1 SinkInfo (com.google.cloud.logging.SinkInfo)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 HttpURLConnection (java.net.HttpURLConnection)1 URI (java.net.URI)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1