/** * Sets synchronicity of logging writes. By default, writes are asynchronous. * * @param synchronicity {@link Synchronicity} */ public void setSynchronicity(Synchronicity synchronicity) { getLogging().setWriteSynchronicity(synchronicity); }
getLogging().setWriteSynchronicity(config.getSynchronicity());
@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 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 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 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 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 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(); } }
@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)); }
logging.setFlushSeverity(Severity.ERROR); expectLastCall().once(); logging.setWriteSynchronicity(Synchronicity.ASYNC); expectLastCall().once(); logging.write(ImmutableList.of(FINEST_ENTRY), DEFAULT_OPTIONS);
/** * Sets synchronicity of logging writes. By default, writes are asynchronous. * * @param synchronicity {@link Synchronicity} */ public void setSynchronicity(Synchronicity synchronicity) { getLogging().setWriteSynchronicity(synchronicity); }
getLogging().setWriteSynchronicity(config.getSynchronicity());