### Question:
LogicalLogRecord { public String format(StackTraceProcessor processor) throws Exception { StringBuilder result = new StringBuilder(); ISO_ALWAYS_WITH_MILLISECONDS.formatTo(timestamp, result); result.append(","); diagnosticContext.printContextInformation(result); result.append(message.getMessageCode()); result.append(","); new Formatter(result).format(message.getMessagePattern(), details); if (cause.isPresent()) { result.append(" "); processor.process(cause.get(), result); } return result.toString(); } LogicalLogRecord(Instant timestamp, DiagnosticContext diagnosticContext, T message, Optional<Throwable> cause, Object... details); String format(StackTraceProcessor processor);  }### Answer:
@Test public void format_shouldProduceAnAppropriatelyFormattedMessage() throws Exception { Instant instant = Instant.parse("2014-04-01T13:37:00.123Z"); LogicalLogRecord<TestMessages> record = new LogicalLogRecord<>(instant, SAMPLE_DIAGNOSTIC_CONTEXT, TestMessages.Bar, Optional.empty(), 42); String result = record.format(PROCESSOR_SHOULD_NOT_BE_CALLED); assertEquals("2014-04-01T13:37:00.123Z,CODE-Bar,A Bar event occurred, with argument 42", result); }
@Test public void format_shouldIncludeMilliseconds_whenTheTimestampIsAnEvenSecond() throws Exception { Instant instant = Instant.parse("2014-04-01T13:37:00.000Z"); LogicalLogRecord<TestMessages> record = new LogicalLogRecord<>(instant, SAMPLE_DIAGNOSTIC_CONTEXT, TestMessages.Foo, Optional.empty()); String result = record.format(PROCESSOR_SHOULD_NOT_BE_CALLED); assertEquals("2014-04-01T13:37:00.000Z,CODE-Foo,An event of some kind occurred", result); }
@Test public void format_shouldProduceAnAppropriatelyFormattedMessage_givenAThrowable() throws Exception { Instant instant = Instant.parse("2014-04-01T13:37:00.123Z"); Throwable expectedThrowable = new RuntimeException(); LogicalLogRecord<TestMessages> record = new LogicalLogRecord<>(instant, SAMPLE_DIAGNOSTIC_CONTEXT, TestMessages.Bar, Optional.of(expectedThrowable), 42); StackTraceProcessor processor = (t, out) -> { assertSame(expectedThrowable, t); out.append("#EXCEPTION_HERE#"); }; String result = record.format(processor); assertEquals("2014-04-01T13:37:00.123Z,CODE-Bar,A Bar event occurred, with argument 42 #EXCEPTION_HERE#", result); }
@Test public void format_shouldIncludeTheDiagnosticContextInTheFormattedMessage() throws Exception { Instant instant = Instant.parse("2014-04-01T13:37:00.123Z"); Map<String, String> correlationIds = new HashMap<>(); correlationIds.put("user", "joeUser"); DiagnosticContext dc = spy(new DiagnosticContext(() -> correlationIds)); LogicalLogRecord<TestMessages> record = new LogicalLogRecord<>(instant, dc, TestMessages.Bar, Optional.empty(), 42); String result = record.format(PROCESSOR_SHOULD_NOT_BE_CALLED); assertEquals("2014-04-01T13:37:00.123Z,user=joeUser,CODE-Bar,A Bar event occurred, with argument 42", result); verify(dc).printContextInformation(any()); }