JobMessageLogAppender extends AppenderBase<ILoggingEvent> { @Override protected void append(final ILoggingEvent eventObject) { Map<String, String> mdcMap = eventObject.getMDCPropertyMap(); if (mdcMap.containsKey("job_id") && eventObject.getMarker() != null && JobMarker.JOB.contains(eventObject.getMarker())) { String jobId = mdcMap.get("job_id"); Level level = eventObject.getLevel(); de.otto.edison.jobs.domain.Level edisonLevel = logLevelToEdisonLevel(level); String message = eventObject.getFormattedMessage(); try { final JobMessage jobMessage = jobMessage(edisonLevel, message, OffsetDateTime.now()); jobService.appendMessage(jobId, jobMessage); } catch(final RuntimeException e) { addError("Failed to persist job message (jobId=" + jobId + "): " + message, e); } } } @Autowired  JobMessageLogAppender(final JobService jobService);   }
@Test public void shouldPublishEventWithJobIdAndLevelWARN() { final LoggingEvent loggingEvent = createLoggingEvent(Level.WARN); jobEventAppender.append(loggingEvent); final ArgumentCaptor<JobMessage> messageCaptor = forClass(JobMessage.class); verify(jobService).appendMessage(eq("someJobId"), messageCaptor.capture()); assertMessageEvent(messageCaptor, de.otto.edison.jobs.domain.Level.WARNING); }
@Test public void shouldPublishEventWithJobIdAndLevelINFO() { final LoggingEvent loggingEvent = createLoggingEvent(Level.INFO); jobEventAppender.append(loggingEvent); final ArgumentCaptor<JobMessage> messageCaptor = forClass(JobMessage.class); verify(jobService).appendMessage(eq("someJobId"), messageCaptor.capture()); assertMessageEvent(messageCaptor, de.otto.edison.jobs.domain.Level.INFO); }
@Test public void shouldNotLogWhenNoJobIdInMDC() { final LoggingEvent loggingEvent = new LoggingEvent(); jobEventAppender.append(loggingEvent); verifyZeroInteractions(jobService); }
@Test public void shouldPublishEventWithJobIdAndLevelERROR() { final LoggingEvent loggingEvent = createLoggingEvent(Level.ERROR); jobEventAppender.append(loggingEvent); final ArgumentCaptor<JobMessage> messageCaptor = forClass(JobMessage.class); verify(jobService).appendMessage(eq("someJobId"), messageCaptor.capture()); assertMessageEvent(messageCaptor, de.otto.edison.jobs.domain.Level.ERROR); }