Search in sources :

Example 1 with ConsoleAppenderFactory

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);
    }
}
Also used : Request(org.eclipse.jetty.server.Request) Response(org.eclipse.jetty.server.Response) ConsoleAppenderFactory(io.dropwizard.logging.ConsoleAppenderFactory) BeforeEach(org.junit.jupiter.api.BeforeEach) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) Arrays(java.util.Arrays) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) LoggerFactory(org.slf4j.LoggerFactory) MarkerFactory(org.slf4j.MarkerFactory) IAccessEvent(ch.qos.logback.access.spi.IAccessEvent) LogbackAccessRequestLogFactory(io.dropwizard.request.logging.LogbackAccessRequestLogFactory) Jackson(io.dropwizard.jackson.Jackson) JsonNode(com.fasterxml.jackson.databind.JsonNode) YamlConfigurationFactory(io.dropwizard.configuration.YamlConfigurationFactory) BaseValidator(io.dropwizard.validation.BaseValidator) HttpFields(org.eclipse.jetty.http.HttpFields) DeferredProcessingAware(ch.qos.logback.core.spi.DeferredProcessingAware) PrintStream(java.io.PrintStream) Awaitility.await(org.awaitility.Awaitility.await) MetricRegistry(com.codahale.metrics.MetricRegistry) RequestLog(org.eclipse.jetty.server.RequestLog) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ResourceConfigurationSourceProvider(io.dropwizard.configuration.ResourceConfigurationSourceProvider) Mockito.when(org.mockito.Mockito.when) BootstrapLogging(io.dropwizard.logging.BootstrapLogging) DefaultLoggingFactory(io.dropwizard.logging.DefaultLoggingFactory) Assertions.entry(org.assertj.core.api.Assertions.entry) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) Level(ch.qos.logback.classic.Level) Marker(org.slf4j.Marker) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) PrintStream(java.io.PrintStream) MetricRegistry(com.codahale.metrics.MetricRegistry) DefaultLoggingFactory(io.dropwizard.logging.DefaultLoggingFactory) JsonNode(com.fasterxml.jackson.databind.JsonNode) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Marker(org.slf4j.Marker) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) Test(org.junit.jupiter.api.Test)

Aggregations

IAccessEvent (ch.qos.logback.access.spi.IAccessEvent)1 Level (ch.qos.logback.classic.Level)1 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)1 DeferredProcessingAware (ch.qos.logback.core.spi.DeferredProcessingAware)1 MetricRegistry (com.codahale.metrics.MetricRegistry)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ResourceConfigurationSourceProvider (io.dropwizard.configuration.ResourceConfigurationSourceProvider)1 YamlConfigurationFactory (io.dropwizard.configuration.YamlConfigurationFactory)1 Jackson (io.dropwizard.jackson.Jackson)1 BootstrapLogging (io.dropwizard.logging.BootstrapLogging)1 ConsoleAppenderFactory (io.dropwizard.logging.ConsoleAppenderFactory)1 DefaultLoggingFactory (io.dropwizard.logging.DefaultLoggingFactory)1 LogbackAccessRequestLogFactory (io.dropwizard.request.logging.LogbackAccessRequestLogFactory)1 BaseValidator (io.dropwizard.validation.BaseValidator)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 PrintStream (java.io.PrintStream)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 TimeUnit (java.util.concurrent.TimeUnit)1