本文整理汇总了Java中org.apache.logging.log4j.test.appender.ListAppender.getMessages方法的典型用法代码示例。如果您正苦于以下问题:Java ListAppender.getMessages方法的具体用法?Java ListAppender.getMessages怎么用?Java ListAppender.getMessages使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.logging.log4j.test.appender.ListAppender
的用法示例。
在下文中一共展示了ListAppender.getMessages方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testClassName
import org.apache.logging.log4j.test.appender.ListAppender; //导入方法依赖的package包/类
@Test
public void testClassName() {
final Category category = Category.getInstance("TestCategory");
final Layout<String> layout = PatternLayout.createLayout("%d %p %C{1.} [%t] %m%n", null, null, null, null);
final ListAppender appender = new ListAppender("List2", null, layout, false, false);
appender.start();
category.setAdditivity(false);
category.getLogger().addAppender(appender);
category.error("Test Message");
final List<String> msgs = appender.getMessages();
assertTrue("Incorrect number of messages. Expected 1 got " + msgs.size(), msgs.size() == 1);
final String msg = msgs.get(0);
appender.clear();
final String threadName = Thread.currentThread().getName();
final String expected = "ERROR o.a.l.CategoryTest [" + threadName + "] Test Message" + Constants.LINE_SEP;
assertTrue("Incorrect message \"" + msg + "\"" + " expected \"" + expected +"\"", msg.endsWith(expected));
}
示例2: testLog
import org.apache.logging.log4j.test.appender.ListAppender; //导入方法依赖的package包/类
@Test
@SuppressWarnings("deprecation")
public void testLog() {
final PatternLayout layout = PatternLayout.createLayout("%d %C %L %m", null, null, null, null);
final ListAppender appender = new ListAppender("List", null, layout, false, false);
appender.start();
final Logger root = Logger.getRootLogger();
root.getLogger().addAppender(appender);
root.setLevel(Level.INFO);
final MyLogger log = new MyLogger(root);
log.logInfo("This is a test", null);
root.log(Priority.INFO, "Test msg2", null);
root.log(Priority.INFO, "Test msg3");
final List<String> msgs = appender.getMessages();
assertTrue("Incorrect number of messages", msgs.size() == 3);
final String msg = msgs.get(0);
assertTrue("Message contains incorrect class name: " + msg, msg.contains(LoggerTest.class.getName()));
appender.stop();
root.getLogger().removeAppender(appender);
}
示例3: testClassName
import org.apache.logging.log4j.test.appender.ListAppender; //导入方法依赖的package包/类
@Test
public void testClassName() {
final Category category = Category.getInstance("TestCategory");
final Layout<String> layout = PatternLayout.newBuilder().withPattern("%d %p %C{1.} [%t] %m%n").build();
final ListAppender appender = new ListAppender("List2", null, layout, false, false);
appender.start();
category.setAdditivity(false);
category.getLogger().addAppender(appender);
category.error("Test Message");
final List<String> msgs = appender.getMessages();
assertTrue("Incorrect number of messages. Expected 1 got " + msgs.size(), msgs.size() == 1);
final String msg = msgs.get(0);
appender.clear();
final String threadName = Thread.currentThread().getName();
final String expected = "ERROR o.a.l.CategoryTest [" + threadName + "] Test Message" + Strings.LINE_SEPARATOR;
assertTrue("Incorrect message " + Strings.dquote(msg) + " expected " + Strings.dquote(expected), msg.endsWith(expected));
}
示例4: testGroovyFilter
import org.apache.logging.log4j.test.appender.ListAppender; //导入方法依赖的package包/类
@Test
public void testGroovyFilter() throws Exception {
final Logger logger = LogManager.getLogger("TestGroovyFilter");
logger.traceEntry();
logger.info("This should not be logged");
ThreadContext.put("UserId", "JohnDoe");
logger.info("This should be logged");
ThreadContext.clearMap();
final ListAppender app = getContext().getListAppender("List");
try {
final List<String> messages = app.getMessages();
assertNotNull("No Messages", messages);
assertTrue("Incorrect number of messages. Expected 2, Actual " + messages.size(), messages.size() == 2);
} finally {
app.clear();
}
}
示例5: testLog
import org.apache.logging.log4j.test.appender.ListAppender; //导入方法依赖的package包/类
@Test
@SuppressWarnings("deprecation")
public void testLog() {
final PatternLayout layout = PatternLayout.newBuilder().withPattern("%d %C %L %m").build();
final ListAppender appender = new ListAppender("List", null, layout, false, false);
appender.start();
final Logger root = Logger.getRootLogger();
try {
root.getLogger().addAppender(appender);
root.setLevel(Level.INFO);
final MyLogger log = new MyLogger(root);
log.logInfo("This is a test", null);
root.log(Priority.INFO, "Test msg2", null);
root.log(Priority.INFO, "Test msg3");
final List<String> msgs = appender.getMessages();
assertTrue("Incorrect number of messages", msgs.size() == 3);
final String msg = msgs.get(0);
assertTrue("Message contains incorrect class name: " + msg, msg.contains(LoggerTest.class.getName()));
appender.stop();
} finally {
root.getLogger().removeAppender(appender);
}
}
示例6: test
import org.apache.logging.log4j.test.appender.ListAppender; //导入方法依赖的package包/类
@Test
public void test() {
logger.info("This is a test.", new Exception("Test exception!"));
ListAppender listAppender = loggerContextRule.getListAppender("ListAppender");
Assert.assertNotNull(listAppender);
List<String> events = listAppender.getMessages();
Assert.assertNotNull(events);
Assert.assertEquals(1, events.size());
String logEvent = events.get(0);
Assert.assertNotNull(logEvent);
Assert.assertFalse("\"org.junit\" should not be here", logEvent.contains("org.junit"));
Assert.assertFalse("\"org.eclipse\" should not be here", logEvent.contains("org.eclipse"));
//
Layout<? extends Serializable> layout = listAppender.getLayout();
PatternLayout pLayout = (PatternLayout) layout;
Assert.assertNotNull(pLayout);
Serializer eventSerializer = pLayout.getEventSerializer();
Assert.assertNotNull(eventSerializer);
//
Assert.assertTrue("Missing \"|\"", logEvent.contains("|"));
}
示例7: testJavaScriptPatternSelector
import org.apache.logging.log4j.test.appender.ListAppender; //导入方法依赖的package包/类
@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();
}
示例8: testMarkerPatternSelector
import org.apache.logging.log4j.test.appender.ListAppender; //导入方法依赖的package包/类
@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();
}
示例9: testLayout
import org.apache.logging.log4j.test.appender.ListAppender; //导入方法依赖的package包/类
/**
* Test case for MDC conversion pattern.
*/
@Test
public void testLayout() throws Exception {
// set up appender
final XMLLayout layout = XMLLayout.createLayout("true", "true", "true", null, null, null);
final ListAppender appender = new ListAppender("List", null, layout, true, false);
appender.start();
// set appender on root and set level to debug
root.addAppender(appender);
root.setLevel(Level.DEBUG);
// output starting message
root.debug("starting mdc pattern test");
root.debug("empty mdc");
ThreadContext.put("key1", "value1");
ThreadContext.put("key2", "value2");
root.debug("filled mdc");
ThreadContext.remove("key1");
ThreadContext.remove("key2");
root.error("finished mdc pattern test", new NullPointerException("test"));
appender.stop();
final List<String> list = appender.getMessages();
assertTrue("Incorrect number of lines. Require at least 50 " + list.size(), list.size() > 50);
final String string = list.get(0);
assertTrue("Incorrect header: " + string, string.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
assertTrue("Incorrect footer", list.get(list.size() - 1).equals("</Events>"));
assertTrue("Incorrect body. Expected " + body + " Actual: " + list.get(7), list.get(7).trim().equals(body));
}
示例10: testLayoutNormalApi
import org.apache.logging.log4j.test.appender.ListAppender; //导入方法依赖的package包/类
static void testLayoutNormalApi(final Logger root, final AbstractCsvLayout layout, final boolean messageApi)
throws Exception {
removeAppenders(root);
// set up appender
final ListAppender appender = new ListAppender("List", null, layout, true, false);
appender.start();
appender.countDownLatch = new CountDownLatch(4);
// set appender on root and set level to debug
root.addAppender(appender);
root.setLevel(Level.DEBUG);
// output messages
if (messageApi) {
logDebugObjectArrayMessage(root);
} else {
logDebugNormalApi(root);
}
final int msgCount = 4;
if (appender.getMessages().size() < msgCount) {
// wait until background thread finished processing
appender.countDownLatch.await(10, TimeUnit.SECONDS);
}
assertEquals("Background thread did not finish processing: msg count", msgCount, appender.getMessages().size());
// don't stop appender until background thread is done
appender.stop();
final List<String> list = appender.getMessages();
final char d = layout.getFormat().getDelimiter();
Assert.assertEquals("1" + d + "2" + d + "3", list.get(0));
Assert.assertEquals("2" + d + "3", list.get(1));
Assert.assertEquals("5" + d + "6", list.get(2));
Assert.assertEquals("7" + d + "8" + d + "9" + d + "10", list.get(3));
}
示例11: testClassLogger
import org.apache.logging.log4j.test.appender.ListAppender; //导入方法依赖的package包/类
@Test
public void testClassLogger() throws Exception {
final ListAppender app = ctx.getListAppender("Class").clear();
final Logger logger = LoggerFactory.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);
}
}
示例12: testMDC
import org.apache.logging.log4j.test.appender.ListAppender; //导入方法依赖的package包/类
@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"));
}
示例13: testMethodLogger
import org.apache.logging.log4j.test.appender.ListAppender; //导入方法依赖的package包/类
@Test
public void testMethodLogger() throws Exception {
final ListAppender app = ctx.getListAppender("Method").clear();
final Log logger = LogFactory.getLog("MethodLogger");
logger.info("More messages.");
logger.warn("CATASTROPHE INCOMING!");
logger.error("ZOMBIES!!!");
logger.warn("brains~~~");
logger.info("Itchy. Tasty.");
final List<String> messages = app.getMessages();
assertEquals("Incorrect number of messages.", 5, messages.size());
for (final String message : messages) {
assertEquals("Incorrect caller method name.", "testMethodLogger", message);
}
}
示例14: verify
import org.apache.logging.log4j.test.appender.ListAppender; //导入方法依赖的package包/类
private void verify(final String expected) {
final ListAppender listApp = ListAppender.getListAppender("List");
assertNotNull("Missing Appender", listApp);
final List<String> events = listApp.getMessages();
assertTrue("Incorrect number of messages. Expected 1 Actual " + events.size(), events.size() == 1);
final String actual = events.get(0);
assertEquals("Incorrect message. Expected " + expected + ". Actual " + actual, expected, actual);
listApp.clear();
}
示例15: verify
import org.apache.logging.log4j.test.appender.ListAppender; //导入方法依赖的package包/类
private void verify(final String name, final String expected) {
final ListAppender listApp = context.getListAppender(name);
final List<String> events = listApp.getMessages();
assertThat(events, hasSize(1));
final String actual = events.get(0);
assertThat(actual, equalTo(expected));
listApp.clear();
}