Refine search
@Test public void testBuilder() { assertEquals(LOG_NAME, STRING_ENTRY.getLogName()); assertEquals(RESOURCE, STRING_ENTRY.getResource()); assertEquals(TIMESTAMP, (long) STRING_ENTRY.getTimestamp()); assertEquals(RECEIVE_TIMESTAMP, (long) STRING_ENTRY.getReceiveTimestamp()); assertEquals(SEVERITY, STRING_ENTRY.getSeverity()); LogEntry logEntry = LogEntry.newBuilder(STRING_PAYLOAD) .setPayload(StringPayload.of("otherPayload")) .setLogName(LOG_NAME) .setResource(RESOURCE) .setTimestamp(TIMESTAMP) .setReceiveTimestamp(RECEIVE_TIMESTAMP) .setSeverity(SEVERITY) .setInsertId(INSERT_ID) .setHttpRequest(HTTP_REQUEST) .addLabel("key1", "value1") .addLabel("key2", "value2") .setOperation(OPERATION) .setTrace(TRACE) .setSpanId(SPAN_ID) .setTraceSampled(TRACE_SAMPLED) .setSourceLocation(SOURCE_LOCATION) .build(); assertEquals(LOG_NAME, logEntry.getLogName()); assertEquals(RESOURCE, logEntry.getResource());
@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."); } }
StringPayload firstPayload = StringPayload.of("stringPayload"); LogEntry firstEntry = LogEntry.newBuilder(firstPayload) .addLabel("key1", "value1") .setLogName(logId) .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(logId) .setOperation(Operation.of("operationId", "operationProducer")) .setResource(MonitoredResource.newBuilder("cloudsql_database").build()) .build(); logging().write(ImmutableList.of(firstEntry)); logging().write(ImmutableList.of(secondEntry)); assertTrue(iterator.hasNext()); LogEntry entry = iterator.next(); assertEquals(firstPayload, entry.getPayload()); assertEquals(logId, entry.getLogName()); assertEquals(ImmutableMap.of("key1", "value1"), entry.getLabels());
static LogEntry fromPb(com.google.logging.v2.LogEntry entryPb) { Builder builder = newBuilder(Payload.fromPb(entryPb)); builder.setLabels(entryPb.getLabelsMap()); builder.setSeverity(Severity.fromPb(entryPb.getSeverity())); if (!entryPb.getLogName().equals("")) { builder.setLogName(ProjectLogName.parse(entryPb.getLogName()).getLog()); builder.setResource(MonitoredResource.fromPb(entryPb.getResource())); Long millis = millisFromTimestamp(entryPb.getTimestamp()); if (millis != 0) { builder.setTimestamp(millis); Long millis = millisFromTimestamp(entryPb.getReceiveTimestamp()); if (millis != 0) { builder.setReceiveTimestamp(millis); builder.setInsertId(entryPb.getInsertId()); builder.setHttpRequest(HttpRequest.fromPb(entryPb.getHttpRequest())); builder.setOperation(Operation.fromPb(entryPb.getOperation())); builder.setTrace(entryPb.getTrace()); builder.setSpanId(entryPb.getSpanId()); builder.setTraceSampled(entryPb.getTraceSampled()); if (!entryPb.getSourceLocation().equals(LogEntrySourceLocation.getDefaultInstance())) { builder.setSourceLocation(SourceLocation.fromPb(entryPb.getSourceLocation()));
@Test public void testWriteLogEntriesDoesnotEnableFlushByDefault() { WriteLogEntriesRequest request = WriteLogEntriesRequest.newBuilder() .addAllEntries( Iterables.transform( ImmutableList.of( LOG_ENTRY1, LOG_ENTRY2.toBuilder().setSeverity(Severity.EMERGENCY).build()), LogEntry.toPbFunction(PROJECT))) .build(); ApiFuture<WriteLogEntriesResponse> apiFuture = SettableApiFuture.create(); EasyMock.expect(loggingRpcMock.write(request)).andReturn(apiFuture); EasyMock.replay(rpcFactoryMock, loggingRpcMock); logging = options.getService(); logging.write( ImmutableList.of( LOG_ENTRY1, LOG_ENTRY2.toBuilder().setSeverity(Severity.EMERGENCY).build())); }
@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); }
@Override public void enhanceLogEntry(com.google.cloud.logging.LogEntry.Builder builder) { String traceId = getCurrentTraceId(); if (traceId != null) { builder.setTrace(traceId); } } }
@Override public void enhanceLogEntry(LogEntry.Builder logEntry) { logEntry.addLabel("test-label-2", "test-value-2"); } }
@Override public void enhanceLogEntry(Builder builder) { builder.addLabel("enhanced", "true"); } };
@Override public void enhanceLogEntry(LogEntry.Builder logEntry) { logEntry.addLabel("test-label-1", "test-value-1"); } }
@Override public void enhanceLogEntry(Builder logEntry) { for (Map.Entry<String, String> label : labels.entrySet()) { logEntry.addLabel(label.getKey(), label.getValue()); } } }
@Override public void enhanceLogEntry(LogEntry.Builder builder) { builder.addLabel("enhanced", "true"); } }
@Override public void enhanceLogEntry(LogEntry.Builder logEntry) { // add additional labels logEntry.addLabel("test-label-1", "test-value-1"); } }
@Override public void enhanceLogEntry(LogEntry.Builder logEntry) { // add additional labels logEntry.addLabel("test-label-1", "test-value-1"); } }
@Override public void enhanceLogEntry(com.google.cloud.logging.LogEntry.Builder builder) { String traceId = getCurrentTraceId(); if (traceId != null) { builder.setTrace(traceId); } } }
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()); }
@Override public void enhanceLogEntry(Builder logEntry) { for (Map.Entry<String, String> label : labels.entrySet()) { logEntry.addLabel(label.getKey(), label.getValue()); } } }
@Override public void enhanceLogEntry(LogEntry.Builder builder) { HttpRequest req = HTTP_REQUEST_METADATA.get(); if (req != null) { builder.setHttpRequest(req); } } }
/** Creates a {@code LogEntry} object given the entry payload. */ public static LogEntry of(Payload<?> payload) { return newBuilder(payload).build(); }