use of org.apache.logging.log4j.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");
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"));
}
use of org.apache.logging.log4j.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");
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"));
}
use of org.apache.logging.log4j.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 {
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 {
root.getLogger().removeAppender(appender);
}
}
use of org.apache.logging.log4j.test.appender.ListAppender in project logging-log4j2 by apache.
the class PatternSelectorTest method testJavaScriptPatternSelector.
@Test
public void testJavaScriptPatternSelector() throws Exception {
final org.apache.logging.log4j.Logger logger = LogManager.getLogger("TestJavaScriptPatternSelector");
final org.apache.logging.log4j.Logger logger2 = LogManager.getLogger("JavascriptNoLocation");
logger.traceEntry();
logger.info("Hello World");
logger2.info("No location information");
logger.traceExit();
final ListAppender app = (ListAppender) context.getRequiredAppender("List3");
assertNotNull("No ListAppender", app);
final List<String> messages = app.getMessages();
assertNotNull("No Messages", messages);
assertTrue("Incorrect number of messages. Expected 4, Actual " + messages.size() + ": " + messages, messages.size() == 4);
String expect = "[TRACE] TestJavaScriptPatternSelector ====== " + "o.a.l.l.c.PatternSelectorTest.testJavaScriptPatternSelector:85 Enter ======" + Strings.LINE_SEPARATOR;
assertEquals(expect, messages.get(0));
expect = "[INFO ] TestJavaScriptPatternSelector " + "o.a.l.l.c.PatternSelectorTest.testJavaScriptPatternSelector.86 Hello World" + Strings.LINE_SEPARATOR;
assertEquals(expect, messages.get(1));
assertEquals("[INFO ] JavascriptNoLocation No location information" + Strings.LINE_SEPARATOR, messages.get(2));
app.clear();
}
use of org.apache.logging.log4j.test.appender.ListAppender in project logging-log4j2 by apache.
the class PatternSelectorTest method testMarkerPatternSelector.
@Test
public void testMarkerPatternSelector() throws Exception {
final org.apache.logging.log4j.Logger logger = LogManager.getLogger("TestMarkerPatternSelector");
logger.traceEntry();
logger.info("Hello World");
logger.traceExit();
final ListAppender app = (ListAppender) context.getRequiredAppender("List");
assertNotNull("No ListAppender", app);
final List<String> messages = app.getMessages();
assertNotNull("No Messages", messages);
assertTrue("Incorrect number of messages. Expected 3, Actual " + messages.size() + ": " + messages, messages.size() == 3);
final String expect = String.format("[TRACE] TestMarkerPatternSelector ====== " + "o.a.l.l.c.PatternSelectorTest.testMarkerPatternSelector:43 Enter ======%n");
assertEquals(expect, messages.get(0));
assertEquals("[INFO ] TestMarkerPatternSelector Hello World" + Strings.LINE_SEPARATOR, messages.get(1));
app.clear();
}
Aggregations