use of io.dropwizard.logging.ConsoleAppenderFactory in project dropwizard by dropwizard.
the class LayoutIntegrationTests method testLogJsonToConsole.
@Test
void testLogJsonToConsole() throws Exception {
ConsoleAppenderFactory<ILoggingEvent> consoleAppenderFactory = getAppenderFactory("yaml/json-log-default.yml");
DefaultLoggingFactory defaultLoggingFactory = new DefaultLoggingFactory();
defaultLoggingFactory.setAppenders(Collections.singletonList(consoleAppenderFactory));
assertThat(consoleAppenderFactory.getLayout()).isInstanceOfSatisfying(EventJsonLayoutBaseFactory.class, eventJsonLayoutBaseFactory -> assertThat(eventJsonLayoutBaseFactory).satisfies(factory -> assertThat(factory).isNotNull()).satisfies(factory -> assertThat(factory.getIncludes()).contains(EventAttribute.LEVEL, EventAttribute.THREAD_NAME, EventAttribute.MDC, EventAttribute.MARKER, EventAttribute.LOGGER_NAME, EventAttribute.MESSAGE, EventAttribute.EXCEPTION, EventAttribute.TIMESTAMP)).satisfies(factory -> assertThat(factory.isFlattenMdc()).isFalse()).satisfies(factory -> assertThat(factory.getIncludesMdcKeys()).isEmpty()).satisfies(factory -> assertThat(factory.getExceptionFormat()).isNull()));
PrintStream old = System.out;
ByteArrayOutputStream redirectedStream = new ByteArrayOutputStream();
try {
System.setOut(new PrintStream(redirectedStream));
defaultLoggingFactory.configure(new MetricRegistry(), "json-log-test");
Marker marker = MarkerFactory.getMarker("marker");
LoggerFactory.getLogger("com.example.app").info(marker, "Application log");
// Need to wait, because the logger is async
await().atMost(1, TimeUnit.SECONDS).until(() -> !redirectedStream.toString().isEmpty());
JsonNode jsonNode = objectMapper.readTree(redirectedStream.toString());
assertThat(jsonNode.get("timestamp").isTextual()).isTrue();
assertThat(jsonNode.get("level").asText()).isEqualTo("INFO");
assertThat(jsonNode.get("logger").asText()).isEqualTo("com.example.app");
assertThat(jsonNode.get("marker").asText()).isEqualTo("marker");
assertThat(jsonNode.get("message").asText()).isEqualTo("Application log");
} finally {
System.setOut(old);
}
}
Aggregations