本文整理汇总了Java中org.apache.logging.log4j.test.appender.ListAppender.start方法的典型用法代码示例。如果您正苦于以下问题:Java ListAppender.start方法的具体用法?Java ListAppender.start怎么用?Java ListAppender.start使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.logging.log4j.test.appender.ListAppender
的用法示例。
在下文中一共展示了ListAppender.start方法的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: testTrace
import org.apache.logging.log4j.test.appender.ListAppender; //导入方法依赖的package包/类
/**
* Tests logger.trace(Object).
*/
@Test
public void testTrace() {
final ListAppender appender = new ListAppender("List");
appender.start();
final Logger root = Logger.getRootLogger();
root.getLogger().addAppender(appender);
root.setLevel(Level.INFO);
final Logger tracer = Logger.getLogger("com.example.Tracer");
tracer.setLevel(Level.TRACE);
tracer.trace("Message 1");
root.trace("Discarded Message");
root.trace("Discarded Message");
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().getFormat());
appender.stop();
root.getLogger().removeAppender(appender);
}
示例3: testTraceWithException
import org.apache.logging.log4j.test.appender.ListAppender; //导入方法依赖的package包/类
/**
* Tests logger.trace(Object, Exception).
*/
@Test
public void testTraceWithException() {
final ListAppender appender = new ListAppender("List");
appender.start();
final Logger root = Logger.getRootLogger();
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();
root.getLogger().removeAppender(appender);
}
示例4: testIsTraceEnabled
import org.apache.logging.log4j.test.appender.ListAppender; //导入方法依赖的package包/类
/**
* Tests isTraceEnabled.
*/
@Test
public void testIsTraceEnabled() {
final ListAppender appender = new ListAppender("List");
appender.start();
final Logger root = Logger.getRootLogger();
root.getLogger().addAppender(appender);
root.setLevel(Level.INFO);
final Logger tracer = Logger.getLogger("com.example.Tracer");
tracer.setLevel(Level.TRACE);
assertTrue(tracer.isTraceEnabled());
assertFalse(root.isTraceEnabled());
appender.stop();
root.getLogger().removeAppender(appender);
}
示例5: 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);
}
示例6: 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));
}
示例7: testIsTraceEnabled
import org.apache.logging.log4j.test.appender.ListAppender; //导入方法依赖的package包/类
/**
* Tests isTraceEnabled.
*/
@Test
public void testIsTraceEnabled() {
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);
assertTrue(tracer.isTraceEnabled());
assertFalse(root.isTraceEnabled());
appender.stop();
} finally {
root.getLogger().removeAppender(appender);
}
}
示例8: 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);
}
}
示例9: testServer
import org.apache.logging.log4j.test.appender.ListAppender; //导入方法依赖的package包/类
protected void testServer(final String message1, final String message2) throws Exception {
final Filter socketFilter = new ThreadFilter(Filter.Result.NEUTRAL, Filter.Result.DENY);
final Filter serverFilter = new ThreadFilter(Filter.Result.DENY, Filter.Result.NEUTRAL);
final SocketAppender appender = SocketAppender.createAppender("localhost", this.port, this.protocol, "-1", null, "Test", null,
"false", null, socketFilter, null, null);
appender.start();
final ListAppender listApp = new ListAppender("Events", serverFilter, null, false, false);
listApp.start();
final PatternLayout layout = PatternLayout.createLayout("%m %ex%n", null, null, null, null);
final ConsoleAppender console = ConsoleAppender.createAppender(layout, null, "SYSTEM_OUT", "Console", "false", "true");
final Logger serverLogger = ctx.getLogger(this.getClass().getName());
serverLogger.addAppender(console);
serverLogger.setAdditive(false);
// set appender on root and set level to debug
root.addAppender(appender);
root.addAppender(listApp);
root.setAdditive(false);
root.setLevel(Level.DEBUG);
root.debug(message1);
root.debug(message2);
Thread.sleep(100);
final List<LogEvent> events = listApp.getEvents();
assertNotNull("No event retrieved", events);
assertTrue("No events retrieved", events.size() > 0);
assertTrue("Incorrect event", events.get(0).getMessage().getFormattedMessage().equals(message1));
assertTrue("Incorrect number of events received: " + events.size(), events.size() == 2);
assertTrue("Incorrect event", events.get(1).getMessage().getFormattedMessage().equals(message2));
}
示例10: testServer
import org.apache.logging.log4j.test.appender.ListAppender; //导入方法依赖的package包/类
@Test
public void testServer() throws Exception {
final Filter clientFilter = new MessageFilter(Filter.Result.NEUTRAL, Filter.Result.DENY);
final Filter serverFilter = new MessageFilter(Filter.Result.DENY, Filter.Result.NEUTRAL);
final CompositeFilter clientFilters = CompositeFilter.createFilters(new Filter[]{clientFilter});
final JMSQueueAppender appender = JMSQueueAppender.createAppender("Test", null, null, null, null, null, FACTORY_NAME,
QUEUE_NAME, null, null, null, clientFilters, "true");
appender.start();
final CompositeFilter serverFilters = CompositeFilter.createFilters(new Filter[]{serverFilter});
final ListAppender listApp = new ListAppender("Events", serverFilters, null, false, false);
listApp.start();
final PatternLayout layout = PatternLayout.createLayout("%m %ex%n", null, null, null, null);
final ConsoleAppender console = ConsoleAppender.createAppender(layout, null, "SYSTEM_OUT", "Console", "false", "true");
console.start();
final Logger serverLogger = ctx.getLogger(JMSTopicReceiver.class.getName());
serverLogger.addAppender(console);
serverLogger.setAdditive(false);
// set appender on root and set level to debug
root.addAppender(listApp);
root.addAppender(appender);
root.setAdditive(false);
root.setLevel(Level.DEBUG);
root.debug("This is a test message");
Thread.sleep(100);
final List<LogEvent> events = listApp.getEvents();
assertNotNull("No event retrieved", events);
assertTrue("No events retrieved", events.size() > 0);
assertTrue("Incorrect event", events.get(0).getMessage().getFormattedMessage().equals("This is a test message"));
}
示例11: testServer
import org.apache.logging.log4j.test.appender.ListAppender; //导入方法依赖的package包/类
@Test
public void testServer() throws Exception {
final Filter clientFilter = new MessageFilter(Filter.Result.NEUTRAL, Filter.Result.DENY);
final Filter serverFilter = new MessageFilter(Filter.Result.DENY, Filter.Result.NEUTRAL);
final CompositeFilter clientFilters = CompositeFilter.createFilters(new Filter[]{clientFilter});
final JMSTopicAppender appender = JMSTopicAppender.createAppender("Test", null, null, null, null, null, FACTORY_NAME,
TOPIC_NAME, null, null, null, clientFilters, "true");
appender.start();
final CompositeFilter serverFilters = CompositeFilter.createFilters(new Filter[]{serverFilter});
final ListAppender listApp = new ListAppender("Events", serverFilters, null, false, false);
listApp.start();
final PatternLayout layout = PatternLayout.createLayout("%m %ex%n", null, null, null, null);
final ConsoleAppender console =
ConsoleAppender.createAppender(layout, null, "SYSTEM_OUT", "Console", "false", "true");
console.start();
final Logger serverLogger = ctx.getLogger(JMSTopicReceiver.class.getName());
serverLogger.addAppender(console);
serverLogger.setAdditive(false);
// set appender on root and set level to debug
root.addAppender(listApp);
root.addAppender(appender);
root.setAdditive(false);
root.setLevel(Level.DEBUG);
root.debug("This is a test message");
Thread.sleep(100);
final List<LogEvent> events = listApp.getEvents();
assertNotNull("No event retrieved", events);
assertTrue("No events retrieved", events.size() > 0);
assertTrue("Incorrect event", events.get(0).getMessage().getFormattedMessage().equals("This is a test message"));
}
示例12: 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));
}
示例13: 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));
}
示例14: testTraceWithException
import org.apache.logging.log4j.test.appender.ListAppender; //导入方法依赖的package包/类
/**
* 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);
}
}
示例15: 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 JSONLayout layout = JSONLayout.createLayout("true", "true", "true", "false", null);
final ListAppender appender = new ListAppender("List", null, layout, true, false);
appender.start();
// set appender on root and set level to debug
this.root.addAppender(appender);
this.root.setLevel(Level.DEBUG);
// output starting message
this.root.debug("starting mdc pattern test");
this.root.debug("empty mdc");
ThreadContext.put("key1", "value1");
ThreadContext.put("key2", "value2");
this.root.debug("filled mdc");
ThreadContext.remove("key1");
ThreadContext.remove("key2");
this.root.error("finished mdc pattern test", new NullPointerException("test"));
appender.stop();
final List<String> list = appender.getMessages();
// System.out.println(list);
// [[, {, "logger":"root",, "timestamp":"1376676700199",, "level":"DEBUG",, "thread":"main",,
// "message":"starting mdc pattern test",, "LocationInfo":{,
// "class":"org.apache.logging.log4j.core.layout.JSONLayoutTest",, "method":"testLayout",,
// "file":"JSONLayoutTest.java",, "line":"87", }, },, {, "logger":"root",, "timestamp":"1376676700203",,
// "level":"DEBUG",, "thread":"main",, "message":"empty mdc",, "LocationInfo":{,
// "class":"org.apache.logging.log4j.core.layout.JSONLayoutTest",, "method":"testLayout",,
// "file":"JSONLayoutTest.java",, "line":"89", }, },, {, "logger":"root",, "timestamp":"1376676700204",,
// "level":"DEBUG",, "thread":"main",, "message":"filled mdc",, "LocationInfo":{,
// "class":"org.apache.logging.log4j.core.layout.JSONLayoutTest",, "method":"testLayout",,
// "file":"JSONLayoutTest.java",, "line":"94", },, "Properties":[, {, "name":"key2",, "value":"value2", },, {,
// "name":"key1",, "value":"value1", }, ], },, {, "logger":"root",, "timestamp":"1376676700204",,
// "level":"ERROR",, "thread":"main",, "message":"finished mdc pattern test",,
// "throwable":"java.lang.NullPointerException: test\\n\\tat org.apache.logging.log4j.core.layout.JSONLayoutTest.testLayout(JSONLayoutTest.java:99)\\n\\tat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\\n\\tat sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)\\n\\tat sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\\n\\tat java.lang.reflect.Method.invoke(Method.java:606)\\n\\tat org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)\\n\\tat org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)\\n\\tat org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)\\n\\tat org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)\\n\\tat org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)\\n\\tat org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)\\n\\tat org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)\\n\\tat org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)\\n\\tat org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)\\n\\tat org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)\\n\\tat org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)\\n\\tat org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)\\n\\tat org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)\\n\\tat org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)\\n\\tat org.junit.runners.ParentRunner.run(ParentRunner.java:309)\\n\\tat org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)\\n\\tat org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)\\n\\tat org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)\\n\\tat org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)\\n\\tat org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)\\n\\tat org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)\\n",,
// "LocationInfo":{, "class":"org.apache.logging.log4j.core.layout.JSONLayoutTest",, "method":"testLayout",,
// "file":"JSONLayoutTest.java",, "line":"99", }, },, ]]
this.checkAt("[", 0, list);
this.checkAt("{", 1, list);
this.checkAt("\"logger\":\"root\",", 2, list);
this.checkAt("\"level\":\"DEBUG\",", 4, list);
this.checkAt("\"message\":\"starting mdc pattern test\",", 6, list);
}