/** * 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] }
/** Initialize and configure the cloud logging service. */ @Override public synchronized void start() { if (isStarted()) { return; } MonitoredResource resource = getMonitoredResource(getProjectId()); defaultWriteOptions = new WriteOption[] {WriteOption.logName(getLogName()), WriteOption.resource(resource)}; getLogging().setFlushSeverity(severityFor(getFlushLevel())); loggingEnhancers = new ArrayList<>(); List<LoggingEnhancer> resourceEnhancers = MonitoredResourceUtil.getResourceEnhancers(); loggingEnhancers.addAll(resourceEnhancers); loggingEnhancers.addAll(getLoggingEnhancers()); loggingEventEnhancers = new ArrayList<>(); loggingEventEnhancers.addAll(getLoggingEventEnhancers()); super.start(); }
new WriteOption[] { WriteOption.logName(logName), WriteOption.resource(resource), WriteOption.labels( ImmutableMap.of(
new WriteOption[] { WriteOption.logName(logName), WriteOption.resource(resource), WriteOption.labels( ImmutableMap.of(
/** Initialize and configure the cloud logging service. */ @Override public synchronized void start() { if (isStarted()) { return; } MonitoredResource resource = getMonitoredResource(getProjectId()); defaultWriteOptions = new WriteOption[] {WriteOption.logName(getLogName()), WriteOption.resource(resource)}; getLogging().setFlushSeverity(severityFor(getFlushLevel())); loggingEnhancers = new ArrayList<>(); List<LoggingEnhancer> resourceEnhancers = MonitoredResourceUtil.getResourceEnhancers(); loggingEnhancers.addAll(resourceEnhancers); loggingEnhancers.addAll(getLoggingEnhancers()); loggingEventEnhancers = new ArrayList<>(); loggingEventEnhancers.addAll(getLoggingEventEnhancers()); super.start(); }
@Test public void testEnhancedLogEntry() { expect(options.getProjectId()).andReturn(PROJECT).anyTimes(); expect(options.getService()).andReturn(logging); MonitoredResource resource = MonitoredResource.of("custom", ImmutableMap.<String, String>of()); logging.setFlushSeverity(Severity.ERROR); expectLastCall().once(); logging.setWriteSynchronicity(Synchronicity.ASYNC); expectLastCall().once(); logging.write( ImmutableList.of(FINEST_ENHANCED_ENTRY), WriteOption.logName(LOG_NAME), WriteOption.resource(resource), WriteOption.labels(BASE_SEVERITY_MAP)); expectLastCall().once(); replay(options, logging); LoggingEnhancer enhancer = new LoggingEnhancer() { @Override public void enhanceLogEntry(Builder builder) { builder.addLabel("enhanced", "true"); } }; Handler handler = new LoggingHandler(LOG_NAME, options, resource, Collections.singletonList(enhancer)); handler.setLevel(Level.ALL); handler.setFormatter(new TestFormatter()); handler.publish(newLogRecord(Level.FINEST, MESSAGE)); }
@Test public void testTraceEnhancedLogEntry() { expect(options.getProjectId()).andReturn(PROJECT).anyTimes(); expect(options.getService()).andReturn(logging); MonitoredResource resource = MonitoredResource.of("custom", ImmutableMap.<String, String>of()); logging.setFlushSeverity(Severity.ERROR); expectLastCall().once(); logging.setWriteSynchronicity(Synchronicity.ASYNC); expectLastCall().once(); logging.write( ImmutableList.of(TRACE_ENTRY), WriteOption.logName(LOG_NAME), WriteOption.resource(resource), WriteOption.labels(BASE_SEVERITY_MAP)); expectLastCall().once(); replay(options, logging); LoggingEnhancer enhancer = new TraceLoggingEnhancer(); TraceLoggingEnhancer.setCurrentTraceId("projects/projectId/traces/traceId"); Handler handler = new LoggingHandler(LOG_NAME, options, resource, Collections.singletonList(enhancer)); handler.setLevel(Level.ALL); handler.setFormatter(new TestFormatter()); handler.publish(newLogRecord(Level.FINEST, MESSAGE)); }
@Test public void testWriteLogEntriesAsyncWithOptions() { Map<String, String> labels = ImmutableMap.of("key", "value"); WriteLogEntriesRequest request = WriteLogEntriesRequest.newBuilder() .putAllLabels(labels) .setLogName(LOG_NAME_PB) .setResource(MONITORED_RESOURCE.toPb()) .addAllEntries( Iterables.transform( ImmutableList.of(LOG_ENTRY1, LOG_ENTRY2), LogEntry.toPbFunction(PROJECT))) .build(); WriteLogEntriesResponse response = WriteLogEntriesResponse.newBuilder().build(); EasyMock.expect(loggingRpcMock.write(request)).andReturn(ApiFutures.immediateFuture(response)); EasyMock.replay(rpcFactoryMock, loggingRpcMock); logging = options.getService(); logging.write( ImmutableList.of(LOG_ENTRY1, LOG_ENTRY2), WriteOption.logName(LOG_NAME), WriteOption.resource(MONITORED_RESOURCE), WriteOption.labels(labels)); logging.flush(); }
@Test public void testWriteLogEntriesWithOptions() { Map<String, String> labels = ImmutableMap.of("key", "value"); WriteLogEntriesRequest request = WriteLogEntriesRequest.newBuilder() .putAllLabels(labels) .setLogName(LOG_NAME_PB) .setResource(MONITORED_RESOURCE.toPb()) .addAllEntries( Iterables.transform( ImmutableList.of(LOG_ENTRY1, LOG_ENTRY2), LogEntry.toPbFunction(PROJECT))) .build(); WriteLogEntriesResponse response = WriteLogEntriesResponse.newBuilder().build(); EasyMock.expect(loggingRpcMock.write(request)).andReturn(ApiFutures.immediateFuture(response)); EasyMock.replay(rpcFactoryMock, loggingRpcMock); logging = options.getService(); logging.write( ImmutableList.of(LOG_ENTRY1, LOG_ENTRY2), WriteOption.logName(LOG_NAME), WriteOption.resource(MONITORED_RESOURCE), WriteOption.labels(labels)); }
@Test public void testPublishCustomResource() { expect(options.getProjectId()).andReturn(PROJECT).anyTimes(); expect(options.getService()).andReturn(logging); logging.setFlushSeverity(Severity.ERROR); expectLastCall().once(); logging.setWriteSynchronicity(Synchronicity.ASYNC); expectLastCall().once(); MonitoredResource resource = MonitoredResource.of("custom", ImmutableMap.<String, String>of()); logging.write( ImmutableList.of(FINEST_ENTRY), WriteOption.logName(LOG_NAME), WriteOption.resource(resource), WriteOption.labels(BASE_SEVERITY_MAP)); expectLastCall().once(); replay(options, logging); Handler handler = new LoggingHandler(LOG_NAME, options, resource); handler.setLevel(Level.ALL); handler.setFormatter(new TestFormatter()); handler.publish(newLogRecord(Level.FINEST, MESSAGE)); }
@Test public void testWriteOption() { WriteOption writeOption = WriteOption.labels(LABELS); assertEquals(LABELS, writeOption.getValue()); assertEquals(WriteOption.OptionType.LABELS, writeOption.getOptionType()); writeOption = WriteOption.logName(LOG_NAME); assertEquals(LOG_NAME, writeOption.getValue()); assertEquals(WriteOption.OptionType.LOG_NAME, writeOption.getOptionType()); writeOption = WriteOption.resource(RESOURCE); assertEquals(RESOURCE, writeOption.getValue()); assertEquals(WriteOption.OptionType.RESOURCE, writeOption.getOptionType()); } }