com.google.cloud.logging.LogEntry Java Examples
The following examples show how to use
com.google.cloud.logging.LogEntry.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LoggingSnippets.java From google-cloud-java with Apache License 2.0 | 6 votes |
/** 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; }
Example #2
Source File: TraceIdLoggingEnhancer.java From spring-cloud-gcp with Apache License 2.0 | 6 votes |
/** * Set the trace and span ID fields of the log entry to the current one. * <p>The current trace ID is either the trace ID stored in the Mapped Diagnostic Context (MDC) * under the "X-B3-TraceId" key or, if none set, the current trace ID set by * {@link #setCurrentTraceId(String)}. The current span ID is retrieved from the MDC * under the "X-B3-SpanId" key, if set. * <p>The trace ID is set in the log entry in the "projects/[GCP_PROJECT_ID]/traces/[TRACE_ID]" * format, in order to be associated to traces by the Google Cloud Console. * <p>If an application is running on Google App Engine, the trace ID is also stored in the * "appengine.googleapis.com/trace_id" field, in order to enable log correlation on the logs * viewer. * @param builder log entry builder */ @Override public void enhanceLogEntry(LogEntry.Builder builder) { // In order not to duplicate the whole google-cloud-logging-logback LoggingAppender to add // the trace ID from the MDC there, we're doing it here. // This requires a call to the org.slf4j package. String traceId = org.slf4j.MDC.get(StackdriverTraceConstants.MDC_FIELD_TRACE_ID); String spanId = org.slf4j.MDC.get(StackdriverTraceConstants.MDC_FIELD_SPAN_ID); if (traceId == null) { traceId = getCurrentTraceId(); } if (traceId != null) { builder.setTrace(StackdriverTraceConstants.composeFullTraceName( this.projectIdProvider.getProjectId(), traceId)); if (this.runningOnAppEngine) { builder.addLabel(APP_ENGINE_LABEL_NAME, traceId); } } if (spanId != null) { builder.setSpanId(spanId); } }
Example #3
Source File: LoggingSnippets.java From google-cloud-java with Apache License 2.0 | 6 votes |
/** * Example of writing log entries and providing a default log name and monitored resource. Logging * writes are asynchronous by default. {@link Logging#setWriteSynchronicity(Synchronicity)} can be * used to update the synchronicity. */ // [TARGET write(Iterable, WriteOption...)] // [VARIABLE "my_log_name"] public void write(String logName) { // [START logging_write_log_entry] List<LogEntry> entries = new ArrayList<>(); entries.add(LogEntry.of(StringPayload.of("Entry payload"))); Map<String, Object> jsonMap = new HashMap<>(); jsonMap.put("key", "value"); entries.add(LogEntry.of(JsonPayload.of(jsonMap))); logging.write( entries, WriteOption.logName(logName), WriteOption.resource(MonitoredResource.newBuilder("global").build())); // [END logging_write_log_entry] }
Example #4
Source File: OpenCensusTraceLoggingEnhancerTest.java From opencensus-java with Apache License 2.0 | 6 votes |
@Test public void enhanceLogEntry_AddSampledSpanToLogEntry() { LogEntry logEntry = getEnhancedLogEntry( new OpenCensusTraceLoggingEnhancer("my-test-project-3"), new TestSpan( SpanContext.create( TraceId.fromLowerBase16("4c6af40c499951eb7de2777ba1e4fefa"), SpanId.fromLowerBase16("de52e84d13dd232d"), TraceOptions.builder().setIsSampled(true).build(), EMPTY_TRACESTATE))); assertTrue(logEntry.getTraceSampled()); assertThat(logEntry.getTrace()) .isEqualTo("projects/my-test-project-3/traces/4c6af40c499951eb7de2777ba1e4fefa"); assertThat(logEntry.getSpanId()).isEqualTo("de52e84d13dd232d"); }
Example #5
Source File: OpenCensusTraceLoggingEnhancerTest.java From opencensus-java with Apache License 2.0 | 6 votes |
@Test public void enhanceLogEntry_AddNonSampledSpanToLogEntry() { LogEntry logEntry = getEnhancedLogEntry( new OpenCensusTraceLoggingEnhancer("my-test-project-6"), new TestSpan( SpanContext.create( TraceId.fromLowerBase16("72c905c76f99e99974afd84dc053a480"), SpanId.fromLowerBase16("731e102335b7a5a0"), TraceOptions.builder().setIsSampled(false).build(), EMPTY_TRACESTATE))); assertFalse(logEntry.getTraceSampled()); assertThat(logEntry.getTrace()) .isEqualTo("projects/my-test-project-6/traces/72c905c76f99e99974afd84dc053a480"); assertThat(logEntry.getSpanId()).isEqualTo("731e102335b7a5a0"); }
Example #6
Source File: CustomLoggingServlet.java From tomcat-runtime with Apache License 2.0 | 6 votes |
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { JsonNode body = objectMapper.readTree(req.getReader()); String logName = body.path("log_name").asText(); String token = body.path("token").asText(); Severity severity = Severity.valueOf(body.path("level").asText()); LogEntry logEntry = LogEntry.newBuilder(Payload.StringPayload.of(token)) .setLogName(logName) .setSeverity(severity) .setResource(MonitoredResource.newBuilder("global").build()) .build(); logging.write(Collections.singletonList(logEntry)); resp.setContentType("text/plain"); resp.getWriter().println(URLEncoder.encode(logName, "UTF-8")); }
Example #7
Source File: ExampleSystemTest.java From java-docs-samples with Apache License 2.0 | 6 votes |
private static String getLogEntriesAsString(String startTimestamp) { // Construct Stackdriver logging filter // See this page for more info: https://cloud.google.com/logging/docs/view/advanced-queries String filter = "resource.type=\"cloud_function\"" + " AND severity=INFO" + " AND resource.labels.function_name=" + FUNCTION_DEPLOYED_NAME + String.format(" AND timestamp>=\"%s\"", startTimestamp); // Get Stackdriver logging entries Page<LogEntry> logEntries = loggingClient.listLogEntries( Logging.EntryListOption.filter(filter), Logging.EntryListOption.sortOrder( Logging.SortingField.TIMESTAMP, Logging.SortingOrder.DESCENDING) ); // Serialize Stackdriver logging entries + collect them into a single string String logsConcat = StreamSupport.stream(logEntries.getValues().spliterator(), false) .map((x) -> x.toString()) .collect(Collectors.joining("%n")); return logsConcat; }
Example #8
Source File: ExampleSystemTest.java From java-docs-samples with Apache License 2.0 | 6 votes |
private static String getLogEntriesAsString(String startTimestamp) { // Construct Stackdriver logging filter // See this page for more info: https://cloud.google.com/logging/docs/view/advanced-queries String filter = "resource.type=\"cloud_function\"" + " AND severity=INFO" + " AND resource.labels.function_name=" + FUNCTION_DEPLOYED_NAME + String.format(" AND timestamp>=\"%s\"", startTimestamp); // Get Stackdriver logging entries Page<LogEntry> logEntries = loggingClient.listLogEntries( Logging.EntryListOption.filter(filter), Logging.EntryListOption.sortOrder( Logging.SortingField.TIMESTAMP, Logging.SortingOrder.DESCENDING) ); // Serialize Stackdriver logging entries + collect them into a single string String logsConcat = StreamSupport.stream(logEntries.getValues().spliterator(), false) .map((x) -> x.toString()) .collect(Collectors.joining("%n")); return logsConcat; }
Example #9
Source File: ListLogs.java From java-docs-samples with Apache License 2.0 | 6 votes |
/** 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] }
Example #10
Source File: QuickstartSample.java From java-docs-samples with Apache License 2.0 | 6 votes |
/** Expects a new or existing Stackdriver log name as the first argument.*/ public static void main(String... args) throws Exception { // Instantiates a client Logging logging = LoggingOptions.getDefaultInstance().getService(); // The name of the log to write to String logName = args[0]; // "my-log"; // The data to write to the log String text = "Hello, world!"; LogEntry entry = LogEntry.newBuilder(StringPayload.of(text)) .setSeverity(Severity.ERROR) .setLogName(logName) .setResource(MonitoredResource.newBuilder("global").build()) .build(); // Writes the log entry asynchronously logging.write(Collections.singleton(entry)); System.out.printf("Logged: %s%n", text); }
Example #11
Source File: LoggingIT.java From java-docs-samples with Apache License 2.0 | 6 votes |
@Test(timeout = 60000) public void testWriteAndListLogs() throws Exception { // write a log entry LogEntry entry = LogEntry.newBuilder(StringPayload.of("Hello world again")) .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(TEST_WRITE_LOG); Thread.sleep(5000); } assertThat(bout.toString().contains("Hello world again")).isTrue(); }
Example #12
Source File: LoggingExample.java From google-cloud-java with Apache License 2.0 | 6 votes |
@Override LogEntry parse(String... args) throws Exception { if (args.length >= 3) { if ((args.length & 0x1) != 0x1) { throw new IllegalArgumentException("Labels must be a list of key-value pairs."); } String logName = args[0]; Severity severity = Severity.valueOf(args[1].toUpperCase()); String message = args[2]; Map<String, String> labels = Maps.newHashMapWithExpectedSize((args.length - 3) / 2); for (int i = 3; i < args.length; i += 2) { labels.put(args[i], args[i + 1]); } return LogEntry.newBuilder(StringPayload.of(message)) .setLogName(logName) .setSeverity(severity) .setLabels(labels) .build(); } else { throw new IllegalArgumentException("Missing required arguments."); } }
Example #13
Source File: WriteAndListLogEntries.java From google-cloud-java with Apache License 2.0 | 5 votes |
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); } } }
Example #14
Source File: LoggingSnippets.java From google-cloud-java with Apache License 2.0 | 5 votes |
/** 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; }
Example #15
Source File: LoggingExample.java From google-cloud-java with Apache License 2.0 | 5 votes |
@Override public void run(Logging logging, LogEntry entry) { MonitoredResource resource = MonitoredResource.newBuilder("global") .addLabel("project_id", logging.getOptions().getProjectId()) .build(); LogEntry entryWithResource = entry.toBuilder().setResource(resource).build(); logging.write(Collections.singleton(entryWithResource)); System.out.printf("Written entry %s%n", entryWithResource); }
Example #16
Source File: OpenCensusTraceLoggingEnhancerTest.java From opencensus-java with Apache License 2.0 | 5 votes |
private static LogEntry getEnhancedLogEntry(LoggingEnhancer loggingEnhancer, Span span) { Scope scope = tracer.withSpan(span); try { LogEntry.Builder builder = LogEntry.newBuilder(null); loggingEnhancer.enhanceLogEntry(builder); return builder.build(); } finally { scope.close(); } }
Example #17
Source File: OpenCensusTraceLoggingEnhancerTest.java From opencensus-java with Apache License 2.0 | 5 votes |
@Test public void enhanceLogEntry_ConvertNullProjectIdToEmptyString() { LogEntry logEntry = getEnhancedLogEntry( new OpenCensusTraceLoggingEnhancer(null), new TestSpan( SpanContext.create( TraceId.fromLowerBase16("bfb4248a24325a905873a1d43001d9a0"), SpanId.fromLowerBase16("6f23f9afd448e272"), TraceOptions.builder().setIsSampled(true).build(), EMPTY_TRACESTATE))); assertThat(logEntry.getTrace()).isEqualTo("projects//traces/bfb4248a24325a905873a1d43001d9a0"); }
Example #18
Source File: OpenCensusTraceLoggingEnhancerTest.java From opencensus-java with Apache License 2.0 | 5 votes |
@Test public void enhanceLogEntry_AddBlankSpanToLogEntry() { LogEntry logEntry = getEnhancedLogEntry( new OpenCensusTraceLoggingEnhancer("my-test-project-7"), BlankSpan.INSTANCE); assertFalse(logEntry.getTraceSampled()); assertThat(logEntry.getTrace()) .isEqualTo("projects/my-test-project-7/traces/00000000000000000000000000000000"); assertThat(logEntry.getSpanId()).isEqualTo("0000000000000000"); }
Example #19
Source File: LoggingExample.java From google-cloud-java with Apache License 2.0 | 5 votes |
@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 #20
Source File: ITLoggingSnippets.java From google-cloud-java with Apache License 2.0 | 5 votes |
@Test public void testWriteAndListLogEntries() throws InterruptedException { String logName = RemoteLoggingHelper.formatForTest("log_name"); String filter = "logName=projects/" + logging.getOptions().getProjectId() + "/logs/" + logName; loggingSnippets.write(logName); Iterator<LogEntry> iterator = loggingSnippets.listLogEntries(filter).iterateAll().iterator(); while (Iterators.size(iterator) < 2) { Thread.sleep(500); iterator = loggingSnippets.listLogEntries(filter).iterateAll().iterator(); } assertTrue(loggingSnippets.deleteLog(logName)); }
Example #21
Source File: ITLoggingSnippets.java From google-cloud-java with Apache License 2.0 | 5 votes |
@Test public void testWriteAndListLogEntriesAsync() throws ExecutionException, InterruptedException { String logName = RemoteLoggingHelper.formatForTest("log_name"); String filter = "logName=projects/" + logging.getOptions().getProjectId() + "/logs/" + logName; loggingSnippets.write(logName); // flush all pending asynchronous writes logging.flush(); Iterator<LogEntry> iterator = loggingSnippets.listLogEntriesAsync(filter).iterateAll().iterator(); while (Iterators.size(iterator) < 2) { Thread.sleep(500); iterator = loggingSnippets.listLogEntriesAsync(filter).iterateAll().iterator(); } assertTrue(loggingSnippets.deleteLogAsync(logName)); }
Example #22
Source File: ExampleEnhancer.java From java-docs-samples with Apache License 2.0 | 4 votes |
@Override public void enhanceLogEntry(LogEntry.Builder logEntry) { // add additional labels logEntry.addLabel("test-label-1", "test-value-1"); }
Example #23
Source File: ExampleEnhancer.java From java-docs-samples with Apache License 2.0 | 4 votes |
@Override public void enhanceLogEntry(LogEntry.Builder logEntry) { // add additional labels logEntry.addLabel("test-label-1", "test-value-1"); }
Example #24
Source File: StackdriverMonitor.java From data-transfer-project with Apache License 2.0 | 4 votes |
private void log(Severity severity, Supplier<String> supplier, Object... data) { MonitoredResource.Builder resourceBuilder = MonitoredResource.newBuilder("generic_task") .addLabel("project_id", projectId) // This is slightly backwards as in GCP a job can have many tasks // but to line up with the DTP terminology around a job we'll use // GCP's job to line up with DTP's job. .addLabel("task_id", getHostName()); if (null != jobId) { resourceBuilder.addLabel("job", jobId); } StringBuilder logMessage = new StringBuilder(); logMessage.append(supplier.get()); if (data != null) { for (Object datum : data) { if (datum instanceof Throwable) { logMessage.append(format("\n%s", Throwables.getStackTraceAsString(((Throwable) datum)))); } else if (datum instanceof UUID) { logMessage.append(format("\nJobId: %s", ((UUID) datum))); } else if (datum instanceof EventCode) { logMessage.append(format("\nEventCode: %s", (EventCode) datum)); } else if (datum != null) { logMessage.append(format("\n%s", datum)); } } } LogEntry entry = LogEntry.newBuilder(Payload.StringPayload.of(logMessage.toString())) .setSeverity(severity) .setLogName(LOG_NAME) .setResource(resourceBuilder.build()) .build(); try { // Writes the log entry asynchronously logging.write(Collections.singleton(entry)); } catch (Throwable t) { System.out.println("Problem logging: " + t.getMessage()); t.printStackTrace(System.out); } }
Example #25
Source File: OpenCensusTraceLoggingEnhancer.java From opencensus-java with Apache License 2.0 | 4 votes |
private static void addTracingData( String tracePrefix, SpanContext span, LogEntry.Builder builder) { builder.setTrace(formatTraceId(tracePrefix, span.getTraceId())); builder.setSpanId(span.getSpanId().toLowerBase16()); builder.setTraceSampled(span.getTraceOptions().isSampled()); }
Example #26
Source File: OpenCensusTraceLoggingEnhancer.java From opencensus-java with Apache License 2.0 | 4 votes |
@Override public void enhanceLogEntry(LogEntry.Builder builder) { addTracingData(tracePrefix, getCurrentSpanContext(), builder); }
Example #27
Source File: TraceSampleApplicationTests.java From spring-cloud-gcp with Apache License 2.0 | 4 votes |
@Test public void testTracesAreLoggedCorrectly() { HttpHeaders headers = new HttpHeaders(); String uuidString = UUID.randomUUID().toString().replaceAll("-", ""); headers.add("x-cloud-trace-context", uuidString); this.testRestTemplate.exchange(this.url, HttpMethod.GET, new HttpEntity<>(headers), String.class); GetTraceRequest getTraceRequest = GetTraceRequest.newBuilder() .setProjectId(this.projectIdProvider.getProjectId()) .setTraceId(uuidString) .build(); String logFilter = String.format( "trace=projects/%s/traces/%s", this.projectIdProvider.getProjectId(), uuidString); await().atMost(4, TimeUnit.MINUTES) .pollInterval(Duration.TWO_SECONDS) .ignoreExceptions() .untilAsserted(() -> { Trace trace = this.traceServiceStub.getTrace(getTraceRequest); assertThat(trace.getTraceId()).isEqualTo(uuidString); assertThat(trace.getSpansCount()).isEqualTo(8); // verify custom tags assertThat(trace.getSpans(0).getLabelsMap().get("environment")).isEqualTo("QA"); assertThat(trace.getSpans(0).getLabelsMap().get("session-id")).isNotNull(); List<LogEntry> logEntries = new ArrayList<>(); this.logClient.listLogEntries(Logging.EntryListOption.filter(logFilter)).iterateAll() .forEach((le) -> { logEntries.add(le); assertThat(le.getTrace()).matches( "projects/" + this.projectIdProvider.getProjectId() + "/traces/([a-z0-9]){32}"); assertThat(le.getSpanId()).matches("([a-z0-9]){16}"); }); List<String> logContents = logEntries.stream() .map((logEntry) -> (String) ((JsonPayload) logEntry.getPayload()) .getDataAsMap().get("message")) .collect(Collectors.toList()); assertThat(logContents).contains("starting busy work"); assertThat(logContents).contains("finished busy work"); }); }