use of org.apache.logging.log4j.core.test.appender.ListAppender in project logging-log4j2 by apache.
the class LogWithRouteTest method testMDC.
@Test
public void testMDC() throws Exception {
MDC.put("Type", "Service");
MDC.put("Name", "John Smith");
try {
final Logger logger = Logger.getLogger("org.apache.test.logging");
logger.debug("This is a test");
final ListAppender listApp = (ListAppender) CTX.getAppender("List");
assertNotNull(listApp);
final List<String> msgs = listApp.getMessages();
assertNotNull("No messages received", msgs);
assertTrue(msgs.size() == 1);
assertTrue("Type is missing", msgs.get(0).contains("Type=Service"));
assertTrue("Name is missing", msgs.get(0).contains("Name=John Smith"));
} finally {
MDC.remove("Type");
MDC.remove("Name");
}
}
use of org.apache.logging.log4j.core.test.appender.ListAppender in project logging-log4j2 by apache.
the class LoggerTest method testTraceWithException.
/**
* Tests logger.trace(Object, Exception).
*/
@Test
public void testTraceWithException() {
final ListAppender appender = new ListAppender("List");
appender.start();
final Logger root = Logger.getRootLogger();
try {
((org.apache.logging.log4j.core.Logger) root.getLogger()).addAppender(appender);
root.setLevel(Level.INFO);
final Logger tracer = Logger.getLogger("com.example.Tracer");
tracer.setLevel(Level.TRACE);
final NullPointerException ex = new NullPointerException();
tracer.trace("Message 1", ex);
root.trace("Discarded Message", ex);
root.trace("Discarded Message", ex);
final List<LogEvent> msgs = appender.getEvents();
assertEquals(1, msgs.size());
final LogEvent event = msgs.get(0);
assertEquals(org.apache.logging.log4j.Level.TRACE, event.getLevel());
assertEquals("Message 1", event.getMessage().getFormattedMessage());
appender.stop();
} finally {
((org.apache.logging.log4j.core.Logger) root.getLogger()).removeAppender(appender);
}
}
use of org.apache.logging.log4j.core.test.appender.ListAppender in project logging-log4j2 by apache.
the class CallerInformationTest method testClassLogger.
@Test
public void testClassLogger() throws Exception {
final ListAppender app = ctx.getListAppender("Class").clear();
final Logger logger = Logger.getLogger("ClassLogger");
logger.info("Ignored message contents.");
logger.warn("Verifying the caller class is still correct.");
logger.error("Hopefully nobody breaks me!");
final List<String> messages = app.getMessages();
assertEquals("Incorrect number of messages.", 3, messages.size());
for (final String message : messages) {
assertEquals("Incorrect caller class name.", this.getClass().getName(), message);
}
}
use of org.apache.logging.log4j.core.test.appender.ListAppender in project logging-log4j2 by apache.
the class LogWithMDCTest method testMDC.
@Test
public void testMDC() throws Exception {
MDC.put("Key1", "John");
MDC.put("Key2", "Smith");
try {
final Logger logger = Logger.getLogger("org.apache.test.logging");
logger.debug("This is a test");
final ListAppender listApp = (ListAppender) CTX.getAppender("List");
assertNotNull(listApp);
final List<String> msgs = listApp.getMessages();
assertNotNull("No messages received", msgs);
assertTrue(msgs.size() == 1);
assertTrue("Key1 is missing", msgs.get(0).contains("Key1=John"));
assertTrue("Key2 is missing", msgs.get(0).contains("Key2=Smith"));
} finally {
MDC.remove("Key1");
MDC.remove("Key2");
}
}
use of org.apache.logging.log4j.core.test.appender.ListAppender in project logging-log4j2 by apache.
the class CallerInformationTest method testClassLogger.
@Test
public void testClassLogger() throws Exception {
final ListAppender app = ctx.getListAppender("Class").clear();
final Logger logger = Logger.getLogger("ClassLogger");
logger.info("Ignored message contents.");
logger.warning("Verifying the caller class is still correct.");
logger.severe("Hopefully nobody breaks me!");
final List<String> messages = app.getMessages();
assertEquals("Incorrect number of messages.", 3, messages.size());
for (final String message : messages) {
assertEquals("Incorrect caller class name.", this.getClass().getName(), message);
}
}
Aggregations