Search in sources :

Example 6 with ListAppender

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

the class AutoConfigTest method testListAppender.

@Test
public void testListAppender() {
    Logger logger = LogManager.getLogger("test");
    logger.debug("This is a test of the root logger");
    LoggerContext loggerContext = org.apache.logging.log4j.LogManager.getContext(false);
    Configuration configuration = ((org.apache.logging.log4j.core.LoggerContext) 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.spi.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 7 with ListAppender

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

the class RewriteAppenderTest method testRewrite.

@Test
public void testRewrite() throws Exception {
    Logger logger = LogManager.getLogger("test");
    ThreadContext.put("key1", "This is a test");
    ThreadContext.put("hello", "world");
    long logTime = System.currentTimeMillis();
    logger.debug("Say hello");
    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("key2"));
    assertEquals("Key value is incorrect", "Log4j", events.get(0).getProperties().get("key2"));
    assertTrue("Timestamp is before point of logging", events.get(0).getTimeStamp() >= logTime);
}
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) LoggingEvent(org.apache.log4j.spi.LoggingEvent) ListAppender(org.apache.log4j.ListAppender) 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