/** * Sets minimum logging level to log immediately and flush any pending writes. * * @param flushLevel minimum log level to trigger flush */ public void setFlushLevel(Level flushLevel) { this.flushLevel = flushLevel; getLogging().setFlushSeverity(severityFor(flushLevel)); }
/** 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(); }
}; getLogging().setFlushSeverity(severityFor(flushLevel)); getLogging().setWriteSynchronicity(config.getSynchronicity());
@Test public void testWriteLogEntriesWithSeverityFlushEnabled() { WriteLogEntriesRequest request = WriteLogEntriesRequest.newBuilder() .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.setFlushSeverity(Severity.DEFAULT); logging.write(ImmutableList.of(LOG_ENTRY1, LOG_ENTRY2)); }
@Test public void testAddHandler() { expect(options.getProjectId()).andReturn(PROJECT).anyTimes(); expect(options.getService()).andReturn(logging); logging.setFlushSeverity(Severity.ERROR); expectLastCall().once(); logging.setWriteSynchronicity(Synchronicity.ASYNC); expectLastCall().andVoid(); logging.write(ImmutableList.of(FINEST_ENTRY), DEFAULT_OPTIONS); expectLastCall().once(); replay(options, logging); LoggingHandler handler = new LoggingHandler(LOG_NAME, options, DEFAULT_RESOURCE) { @Override public void close() { // Make close NOOP to avoid mock close exception } }; handler.setLevel(Level.ALL); handler.setFormatter(new TestFormatter()); Logger logger = Logger.getLogger(getClass().getName()); logger.setLevel(Level.ALL); LoggingHandler.addHandler(logger, handler); logger.log(newLogRecord(Level.FINEST, MESSAGE)); }
@Test public void testFlushLevelConfigUpdatesLoggingFlushSeverity() { LogEntry logEntry = LogEntry.newBuilder(StringPayload.of("this is a test")) .setTimestamp(100000L) .setSeverity(Severity.WARNING) .setLabels( new ImmutableMap.Builder<String, String>() .put("levelName", "WARN") .put("levelValue", String.valueOf(30000L)) .build()) .build(); logging.setFlushSeverity(Severity.WARNING); Capture<Iterable<LogEntry>> capturedArgument = Capture.newInstance(); logging.write(capture(capturedArgument), (WriteOption) anyObject(), (WriteOption) anyObject()); replay(logging); Timestamp timestamp = Timestamp.ofTimeSecondsAndNanos(100000, 0); LoggingEvent loggingEvent = createLoggingEvent(Level.WARN, timestamp.getSeconds()); // error is the default, updating to warn for test loggingAppender.setFlushLevel(Level.WARN); loggingAppender.start(); loggingAppender.doAppend(loggingEvent); verify(logging); assertThat(capturedArgument.getValue().iterator().hasNext()).isTrue(); assertThat(capturedArgument.getValue().iterator().next()).isEqualTo(logEntry); }
@Test public void testEnhancersAddCorrectLabelsToLogEntries() { LogEntry logEntry = LogEntry.newBuilder(StringPayload.of("this is a test")) .setTimestamp(100000L) .setSeverity(Severity.WARNING) .setLabels( new ImmutableMap.Builder<String, String>() .put("levelName", "WARN") .put("levelValue", String.valueOf(30000L)) .put("test-label-1", "test-value-1") .put("test-label-2", "test-value-2") .build()) .build(); logging.setFlushSeverity(Severity.ERROR); Capture<Iterable<LogEntry>> capturedArgument = Capture.newInstance(); logging.write(capture(capturedArgument), (WriteOption) anyObject(), (WriteOption) anyObject()); expectLastCall().once(); replay(logging); loggingAppender.addEnhancer("com.example.enhancers.TestLoggingEnhancer"); loggingAppender.addEnhancer("com.example.enhancers.AnotherTestLoggingEnhancer"); loggingAppender.start(); Timestamp timestamp = Timestamp.ofTimeSecondsAndNanos(100000, 0); LoggingEvent loggingEvent = createLoggingEvent(Level.WARN, timestamp.getSeconds()); loggingAppender.doAppend(loggingEvent); verify(logging); assertThat(capturedArgument.getValue().iterator().hasNext()).isTrue(); assertThat(capturedArgument.getValue().iterator().next()).isEqualTo(logEntry); }
@Test public void testReportFormatError() { expect(options.getProjectId()).andReturn(PROJECT).anyTimes(); expect(options.getService()).andReturn(logging); logging.setFlushSeverity(Severity.ERROR); expectLastCall().once(); logging.setWriteSynchronicity(Synchronicity.ASYNC); expectLastCall().once(); replay(options, logging); Formatter formatter = EasyMock.createStrictMock(Formatter.class); RuntimeException ex = new RuntimeException(); ErrorManager errorManager = EasyMock.createStrictMock(ErrorManager.class); errorManager.error(null, ex, ErrorManager.FORMAT_FAILURE); expectLastCall().once(); LogRecord record = newLogRecord(Level.FINEST, MESSAGE); expect(formatter.format(record)).andThrow(ex); replay(errorManager, formatter); Handler handler = new LoggingHandler(LOG_NAME, options, DEFAULT_RESOURCE); handler.setLevel(Level.ALL); handler.setErrorManager(errorManager); handler.setFormatter(formatter); handler.publish(record); verify(errorManager, formatter); }
@Test public void testReportWriteError() { expect(options.getProjectId()).andReturn(PROJECT).anyTimes(); expect(options.getService()).andReturn(logging); RuntimeException ex = new RuntimeException(); logging.setFlushSeverity(Severity.ERROR); expectLastCall().once(); logging.setWriteSynchronicity(Synchronicity.ASYNC); expectLastCall().once(); logging.write(ImmutableList.of(FINEST_ENTRY), DEFAULT_OPTIONS); expectLastCall().andStubThrow(ex); replay(options, logging); ErrorManager errorManager = EasyMock.createStrictMock(ErrorManager.class); errorManager.error(null, ex, ErrorManager.WRITE_FAILURE); expectLastCall().once(); replay(errorManager); Handler handler = new LoggingHandler(LOG_NAME, options, DEFAULT_RESOURCE); handler.setLevel(Level.ALL); handler.setErrorManager(errorManager); handler.setFormatter(new TestFormatter()); handler.publish(newLogRecord(Level.FINEST, MESSAGE)); verify(errorManager); }
@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 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 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 testClose() throws Exception { expect(options.getProjectId()).andReturn(PROJECT).anyTimes(); expect(options.getService()).andReturn(logging); logging.setFlushSeverity(Severity.ERROR); expectLastCall().once(); logging.setWriteSynchronicity(Synchronicity.ASYNC); expectLastCall().once(); logging.write(ImmutableList.of(FINEST_ENTRY), DEFAULT_OPTIONS); expectLastCall().once(); logging.close(); expectLastCall().once(); replay(options, logging); Handler handler = new LoggingHandler(LOG_NAME, options, DEFAULT_RESOURCE); handler.setLevel(Level.ALL); handler.setFormatter(new TestFormatter()); handler.publish(newLogRecord(Level.FINEST, MESSAGE)); handler.close(); handler.close(); } }
.build()) .build(); logging.setFlushSeverity(Severity.ERROR); Capture<Iterable<LogEntry>> capturedArgument = Capture.newInstance(); logging.write(capture(capturedArgument), (WriteOption) anyObject(), (WriteOption) anyObject());
@Test public void testReportFlushError() { expect(options.getProjectId()).andReturn(PROJECT).anyTimes(); expect(options.getService()).andReturn(logging); RuntimeException ex = new RuntimeException(); logging.setFlushSeverity(Severity.ERROR); expectLastCall().once(); logging.setWriteSynchronicity(Synchronicity.ASYNC); expectLastCall().once(); logging.write(ImmutableList.of(FINEST_ENTRY), DEFAULT_OPTIONS); expectLastCall().once(); logging.flush(); expectLastCall().andThrow(ex); ErrorManager errorManager = EasyMock.createStrictMock(ErrorManager.class); errorManager.error(null, ex, ErrorManager.FLUSH_FAILURE); expectLastCall().once(); replay(options, logging, errorManager); LogRecord record = newLogRecord(Level.FINEST, MESSAGE); Handler handler = new LoggingHandler(LOG_NAME, options, DEFAULT_RESOURCE); handler.setLevel(Level.ALL); handler.setErrorManager(errorManager); handler.setFormatter(new TestFormatter()); handler.publish(record); handler.flush(); verify(errorManager); }
public void testFlushLevel() { expect(options.getProjectId()).andReturn(PROJECT).anyTimes(); expect(options.getService()).andReturn(logging); logging.setFlushSeverity(Severity.ERROR); expectLastCall().once(); logging.setWriteSynchronicity(Synchronicity.ASYNC); expectLastCall().once(); logging.setFlushSeverity(Severity.WARNING); expectLastCall().once(); logging.write( ImmutableList.of( FINEST_ENTRY, FINER_ENTRY, FINE_ENTRY, CONFIG_ENTRY, INFO_ENTRY, WARNING_ENTRY), DEFAULT_OPTIONS); expectLastCall().once(); replay(options, logging); LoggingHandler handler = new LoggingHandler(LOG_NAME, options, DEFAULT_RESOURCE); handler.setLevel(Level.ALL); handler.setFlushLevel(Level.WARNING); handler.setFormatter(new TestFormatter()); handler.publish(newLogRecord(Level.FINEST, MESSAGE)); handler.publish(newLogRecord(Level.FINER, MESSAGE)); handler.publish(newLogRecord(Level.FINE, MESSAGE)); handler.publish(newLogRecord(Level.CONFIG, MESSAGE)); handler.publish(newLogRecord(Level.INFO, MESSAGE)); handler.publish(newLogRecord(Level.WARNING, MESSAGE)); }
@Test public void testSyncWrite() { expect(options.getProjectId()).andReturn(PROJECT).anyTimes(); expect(options.getService()).andReturn(logging); LogEntry entry = LogEntry.newBuilder(Payload.StringPayload.of(MESSAGE)) .setSeverity(Severity.DEBUG) .addLabel("levelName", "FINEST") .addLabel("levelValue", String.valueOf(Level.FINEST.intValue())) .setTimestamp(123456789L) .build(); logging.setFlushSeverity(Severity.ERROR); expectLastCall().once(); logging.setWriteSynchronicity(Synchronicity.ASYNC); expectLastCall().once(); logging.setWriteSynchronicity(Synchronicity.SYNC); expectLastCall().once(); logging.write(ImmutableList.of(entry), DEFAULT_OPTIONS); expectLastCall().once(); replay(options, logging); LoggingHandler handler = new LoggingHandler(LOG_NAME, options, DEFAULT_RESOURCE); handler.setLevel(Level.ALL); handler.setSynchronicity(Synchronicity.SYNC); handler.setFormatter(new TestFormatter()); LogRecord record = new LogRecord(Level.FINEST, MESSAGE); record.setMillis(123456789L); handler.publish(record); }
@Test public void testDefaultWriteOptionsHasExpectedDefaults() { logging.setFlushSeverity(Severity.ERROR); Capture<WriteOption> logNameArg = Capture.newInstance(); Capture<WriteOption> resourceArg = Capture.newInstance(); logging.write((Iterable<LogEntry>) anyObject(), capture(logNameArg), capture(resourceArg)); expectLastCall().once(); replay(logging); loggingAppender.start(); Timestamp timestamp = Timestamp.ofTimeSecondsAndNanos(100000, 0); LoggingEvent loggingEvent = createLoggingEvent(Level.ERROR, timestamp.getSeconds()); loggingAppender.doAppend(loggingEvent); assertThat(logNameArg.getValue()).isEqualTo(defaultWriteOptions[0]); // TODO(chingor): Fix this test to work on GCE and locally // assertThat(resourceArg.getValue()).isEqualTo(defaultWriteOptions[1]); }
expect(options.getProjectId()).andReturn(PROJECT).anyTimes(); expect(options.getService()).andReturn(logging); logging.setFlushSeverity(Severity.ERROR); expectLastCall().once(); logging.setWriteSynchronicity(Synchronicity.ASYNC);
/** * Sets minimum logging level to log immediately and flush any pending writes. * * @param flushLevel minimum log level to trigger flush */ public void setFlushLevel(Level flushLevel) { this.flushLevel = flushLevel; getLogging().setFlushSeverity(severityFor(flushLevel)); }