/** * Triggers a push action to output all buffered records to the target handler, * and the target handler will publish them. Then the buffer is cleared. */ public void push() { for (int i = cursor; i < size; i++) { if (buffer[i] != null) { target.publish(buffer[i]); } buffer[i] = null; } for (int i = 0; i < cursor; i++) { if (buffer[i] != null) { target.publish(buffer[i]); } buffer[i] = null; } cursor = 0; }
public void publish(LogRecord record) { Handler t = resolve(); if(t!=null) t.publish(record); }
private void publishLogRecordsTo(final Handler destination) { synchronized (logRecords) { for (final LogRecord record : logRecords) { destination.publish(record); } logRecords.clear(); } }
@Override public Void answer() throws Throwable { verify(log); reset(log); log.removeHandler(handlerCap.getValue()); expectLastCall().once(); log.setLevel(Level.OFF); expectLastCall().once(); handlerCap.getValue().publish(record); assertThat( ((LoggerRule) handlerCap.getValue()).records(), contains(record)); replay(log); return null; } });
@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); }
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)); handler.publish(newLogRecord(Level.SEVERE, MESSAGE)); handler.publish(newLogRecord(LoggingLevel.DEBUG, MESSAGE)); handler.publish(newLogRecord(LoggingLevel.NOTICE, MESSAGE)); handler.publish(newLogRecord(LoggingLevel.ERROR, MESSAGE)); handler.publish(newLogRecord(LoggingLevel.CRITICAL, MESSAGE)); handler.publish(newLogRecord(LoggingLevel.ALERT, MESSAGE)); handler.publish(newLogRecord(LoggingLevel.EMERGENCY, 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 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)); }
@Override public Void answer() throws Throwable { verify(log); reset(log); log.removeHandler(handlerCap.getValue()); expectLastCall().once(); log.setLevel(Level.OFF); expectLastCall().once(); replay(log); handlerCap.getValue().publish(record); ((LoggerRule) handlerCap.getValue()) .assertLogged(sameInstance(record)); return null; } });
@Override public Void answer() throws Throwable { verify(log); reset(log); log.removeHandler(handlerCap.getValue()); expectLastCall().once(); log.setLevel(Level.OFF); expectLastCall().once(); replay(log); handlerCap.getValue().publish(record); ((LoggerRule) handlerCap.getValue()) .assertLogged(sameInstance(record)); return null; } });
@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(); } }
@Override public Void answer() throws Throwable { verify(log); reset(log); log.removeHandler(handlerCap.getValue()); expectLastCall().once(); log.setLevel(Level.OFF); expectLastCall().once(); replay(log); handlerCap.getValue().publish(record); ((LoggerRule) handlerCap.getValue()) .assertLogged(sameInstance(record)); ((LoggerRule) handlerCap.getValue()).assertLogged(not(anything())); return null; } });
@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); }
/** * Stores the log record internally. */ @Override public void publish(final LogRecord record) { if (!isLoggable(record)) { return; } if (fallbackMode) { fallbackHandler.publish(record); return; } if (targetHandler != null) { targetHandler.publish(record); return; } synchronized (logRecords) { logRecords.add(record); } if (!fallbackMode && record.getLevel().intValue() >= fallbackPushLevel.intValue()) { fallbackMode = true; publishLogRecordsTo(fallbackHandler); } }
/** Setup a log handler counting {@link LogRecord} having {@link Level#WARNING}. */ @Before public void setUp() { // when(mockLogHandler.publish(any(LogRecord.class))); doAnswer( new Answer<Object>() { public Object answer(InvocationOnMock invocation) { Object[] arguments = invocation.getArguments(); LogRecord logRecord = (LogRecord) arguments[0]; if (logRecord.getLevel() == Level.WARNING && !logRecord.getSourceMethodName().equals("finalize")) { warningsCount++; } return null; } }) .when(mockLogHandler) .publish(any(LogRecord.class)); dataStore = new JDBCDataStore(); dataStore.getLogger().addHandler(mockLogHandler); }
private void publishToChildren(final ExtLogRecord record) { for (Handler handler : handlers) { handler.publish(record); } } }
private void publishLogRecordsTo(final Handler destination) { synchronized (logRecords) { for (LogRecord record : logRecords) { destination.publish(record); } logRecords.clear(); } } }