Search in sources :

Example 1 with ListAppender

use of org.apache.log4j.ListAppender in project logging-log4j2 by apache.

the class AsyncAppenderTest method testAsyncXml.

@Test
public void testAsyncXml() throws Exception {
    try (final LoggerContext loggerContext = TestConfigurator.configure("target/test-classes/log4j1-async.xml")) {
        final Logger logger = LogManager.getLogger("test");
        logger.debug("This is a test of the root logger");
        Thread.sleep(50);
        final Configuration configuration = loggerContext.getConfiguration();
        final Map<String, Appender> appenders = configuration.getAppenders();
        ListAppender messageAppender = null;
        for (final Map.Entry<String, Appender> entry : appenders.entrySet()) {
            if (entry.getKey().equals("list")) {
                messageAppender = (ListAppender) ((AppenderAdapter.Adapter) entry.getValue()).getAppender();
            }
        }
        assertNotNull("No Message Appender", messageAppender);
        final List<String> messages = messageAppender.getMessages();
        assertTrue("No messages", messages != null && messages.size() > 0);
    }
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ListAppender(org.apache.log4j.ListAppender) Configuration(org.apache.logging.log4j.core.config.Configuration) ListAppender(org.apache.log4j.ListAppender) AppenderAdapter(org.apache.log4j.bridge.AppenderAdapter) Logger(org.apache.log4j.Logger) LoggerContext(org.apache.logging.log4j.core.LoggerContext) Map(java.util.Map) Test(org.junit.Test)

Example 2 with ListAppender

use of org.apache.log4j.ListAppender in project logging-log4j2 by apache.

the class MapRewriteAppenderTest method testRewrite.

@Test
public void testRewrite() throws Exception {
    Logger logger = LogManager.getLogger("test");
    Map<String, String> map = new HashMap<>();
    map.put("message", "This is a test");
    map.put("hello", "world");
    logger.debug(map);
    LoggerContext context = (LoggerContext) org.apache.logging.log4j.LogManager.getContext(false);
    Configuration configuration = context.getConfiguration();
    Map<String, Appender> appenders = configuration.getAppenders();
    ListAppender eventAppender = null;
    for (Map.Entry<String, Appender> entry : appenders.entrySet()) {
        if (entry.getKey().equals("events")) {
            eventAppender = (ListAppender) ((AppenderAdapter.Adapter) entry.getValue()).getAppender();
        }
    }
    assertNotNull("No Event Appender", eventAppender);
    List<LoggingEvent> events = eventAppender.getEvents();
    assertTrue("No events", events != null && events.size() > 0);
    assertNotNull("No properties in the event", events.get(0).getProperties());
    assertTrue("Key was not inserted", events.get(0).getProperties().containsKey("hello"));
    assertEquals("Key value is incorrect", "world", events.get(0).getProperties().get("hello"));
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ListAppender(org.apache.log4j.ListAppender) Configuration(org.apache.logging.log4j.core.config.Configuration) HashMap(java.util.HashMap) AppenderAdapter(org.apache.log4j.bridge.AppenderAdapter) Logger(org.apache.log4j.Logger) LoggerContext(org.apache.logging.log4j.core.LoggerContext) LoggingEvent(org.apache.log4j.spi.LoggingEvent) ListAppender(org.apache.log4j.ListAppender) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 3 with ListAppender

use of org.apache.log4j.ListAppender in project logging-log4j2 by apache.

the class PropertiesConfigurationTest method testListAppender.

@Test
public void testListAppender() throws Exception {
    try (LoggerContext loggerContext = TestConfigurator.configure("target/test-classes/log4j1-list.properties")) {
        final Logger logger = LogManager.getLogger("test");
        logger.debug("This is a test of the root logger");
        final Configuration configuration = loggerContext.getConfiguration();
        final Map<String, Appender> appenders = configuration.getAppenders();
        ListAppender eventAppender = null;
        ListAppender messageAppender = null;
        for (final Map.Entry<String, Appender> entry : appenders.entrySet()) {
            if (entry.getKey().equals("list")) {
                messageAppender = (ListAppender) ((AppenderAdapter.Adapter) entry.getValue()).getAppender();
            } else if (entry.getKey().equals("events")) {
                eventAppender = (ListAppender) ((AppenderAdapter.Adapter) entry.getValue()).getAppender();
            }
        }
        assertNotNull("No Event Appender", eventAppender);
        assertNotNull("No Message Appender", messageAppender);
        final List<LoggingEvent> events = eventAppender.getEvents();
        assertTrue("No events", events != null && events.size() > 0);
        final List<String> messages = messageAppender.getMessages();
        assertTrue("No messages", messages != null && messages.size() > 0);
    }
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ListAppender(org.apache.log4j.ListAppender) FileAppender(org.apache.logging.log4j.core.appender.FileAppender) Configuration(org.apache.logging.log4j.core.config.Configuration) FilterAdapter(org.apache.log4j.bridge.FilterAdapter) AppenderAdapter(org.apache.log4j.bridge.AppenderAdapter) Logger(org.apache.log4j.Logger) LoggerContext(org.apache.logging.log4j.core.LoggerContext) AppenderAdapter(org.apache.log4j.bridge.AppenderAdapter) LoggingEvent(org.apache.log4j.spi.LoggingEvent) ListAppender(org.apache.log4j.ListAppender) Map(java.util.Map) Test(org.junit.Test)

Example 4 with ListAppender

use of org.apache.log4j.ListAppender in project logging-log4j2 by apache.

the class XmlConfigurationTest method testListAppender.

@Test
public void testListAppender() throws Exception {
    LoggerContext loggerContext = configure("target/test-classes/log4j1-list.xml");
    Logger logger = LogManager.getLogger("test");
    logger.debug("This is a test of the root logger");
    Configuration configuration = loggerContext.getConfiguration();
    Map<String, Appender> appenders = configuration.getAppenders();
    ListAppender eventAppender = null;
    ListAppender messageAppender = null;
    for (Map.Entry<String, Appender> entry : appenders.entrySet()) {
        if (entry.getKey().equals("list")) {
            messageAppender = (ListAppender) ((AppenderAdapter.Adapter) entry.getValue()).getAppender();
        } else if (entry.getKey().equals("events")) {
            eventAppender = (ListAppender) ((AppenderAdapter.Adapter) entry.getValue()).getAppender();
        }
    }
    assertNotNull("No Event Appender", eventAppender);
    assertNotNull("No Message Appender", messageAppender);
    List<LoggingEvent> events = eventAppender.getEvents();
    assertTrue("No events", events != null && events.size() > 0);
    List<String> messages = messageAppender.getMessages();
    assertTrue("No messages", messages != null && messages.size() > 0);
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ListAppender(org.apache.log4j.ListAppender) Configuration(org.apache.logging.log4j.core.config.Configuration) AppenderAdapter(org.apache.log4j.bridge.AppenderAdapter) Logger(org.apache.log4j.Logger) LoggerContext(org.apache.logging.log4j.core.LoggerContext) AppenderAdapter(org.apache.log4j.bridge.AppenderAdapter) LoggingEvent(org.apache.log4j.spi.LoggingEvent) ListAppender(org.apache.log4j.ListAppender) Map(java.util.Map) Test(org.junit.Test)

Example 5 with ListAppender

use of org.apache.log4j.ListAppender in project logging-log4j2 by apache.

the class AsyncAppenderTest method testAsyncProperties.

@Test
public void testAsyncProperties() throws Exception {
    try (final LoggerContext loggerContext = TestConfigurator.configure("target/test-classes/log4j1-async.properties")) {
        final Logger logger = LogManager.getLogger("test");
        logger.debug("This is a test of the root logger");
        Thread.sleep(50);
        final Configuration configuration = loggerContext.getConfiguration();
        final Map<String, Appender> appenders = configuration.getAppenders();
        ListAppender messageAppender = null;
        for (final Map.Entry<String, Appender> entry : appenders.entrySet()) {
            if (entry.getKey().equals("list")) {
                messageAppender = (ListAppender) ((AppenderAdapter.Adapter) entry.getValue()).getAppender();
            }
        }
        assertNotNull("No Message Appender", messageAppender);
        final List<String> messages = messageAppender.getMessages();
        assertTrue("No messages", messages != null && messages.size() > 0);
    }
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ListAppender(org.apache.log4j.ListAppender) Configuration(org.apache.logging.log4j.core.config.Configuration) ListAppender(org.apache.log4j.ListAppender) AppenderAdapter(org.apache.log4j.bridge.AppenderAdapter) Logger(org.apache.log4j.Logger) LoggerContext(org.apache.logging.log4j.core.LoggerContext) Map(java.util.Map) Test(org.junit.Test)

Aggregations

Map (java.util.Map)7 ListAppender (org.apache.log4j.ListAppender)7 Logger (org.apache.log4j.Logger)7 AppenderAdapter (org.apache.log4j.bridge.AppenderAdapter)7 Appender (org.apache.logging.log4j.core.Appender)7 Configuration (org.apache.logging.log4j.core.config.Configuration)7 Test (org.junit.Test)7 LoggerContext (org.apache.logging.log4j.core.LoggerContext)6 LoggingEvent (org.apache.log4j.spi.LoggingEvent)5 HashMap (java.util.HashMap)1 FilterAdapter (org.apache.log4j.bridge.FilterAdapter)1 FileAppender (org.apache.logging.log4j.core.appender.FileAppender)1 LoggerContext (org.apache.logging.log4j.spi.LoggerContext)1