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);
}
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);
}
Aggregations