本文整理汇总了Java中org.apache.logging.log4j.test.appender.ListAppender类的典型用法代码示例。如果您正苦于以下问题:Java ListAppender类的具体用法?Java ListAppender怎么用?Java ListAppender使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ListAppender类属于org.apache.logging.log4j.test.appender包,在下文中一共展示了ListAppender类的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: verify
import org.apache.logging.log4j.test.appender.ListAppender; //导入依赖的package包/类
private void verify(final String expected) {
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
final Map<String, Appender> list = ctx.getConfiguration().getAppenders();
final Appender listApp = list.get("List");
assertNotNull("Missing Appender", listApp);
assertTrue("Not a ListAppender", listApp instanceof ListAppender);
final List<String> events = ((ListAppender) listApp).getMessages();
try
{
assertEquals("Incorrect number of messages.", 1, events.size());
assertEquals("Incorrect message.", "o.a.l.l.t.CatchingTagTest " + expected, events.get(0));
}
finally
{
((ListAppender) listApp).clear();
}
}
示例7: verify
import org.apache.logging.log4j.test.appender.ListAppender; //导入依赖的package包/类
private void verify(final String expected) {
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
final Map<String, Appender> list = ctx.getConfiguration().getAppenders();
final Appender listApp = list.get("List");
assertNotNull("Missing Appender", listApp);
assertTrue("Not a ListAppender", listApp instanceof ListAppender);
final List<String> events = ((ListAppender) listApp).getMessages();
try
{
assertEquals("Incorrect number of messages.", 1, events.size());
assertEquals("Incorrect message.", "o.a.l.l.t.EntryTagTest " + expected, events.get(0));
}
finally
{
((ListAppender) listApp).clear();
}
}
示例8: verify
import org.apache.logging.log4j.test.appender.ListAppender; //导入依赖的package包/类
@SuppressWarnings("unchecked")
private void verify(final String expected) {
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
final Map<String, Appender> list = ctx.getConfiguration().getAppenders();
final Appender listApp = list.get("List");
assertNotNull("Missing Appender", listApp);
assertTrue("Not a ListAppender", listApp instanceof ListAppender);
final List<String> events = ((ListAppender) listApp).getMessages();
try
{
assertEquals("Incorrect number of messages.", 1, events.size());
assertEquals("Incorrect message.", "o.a.l.l.t.LoggingMessageTagSupportTest " + expected, events.get(0));
}
finally
{
((ListAppender) listApp).clear();
}
}
示例9: verify
import org.apache.logging.log4j.test.appender.ListAppender; //导入依赖的package包/类
private void verify(final String expected) {
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
final Map<String, Appender> list = ctx.getConfiguration().getAppenders();
final Appender listApp = list.get("List");
assertNotNull("Missing Appender", listApp);
assertTrue("Not a ListAppender", listApp instanceof ListAppender);
final List<String> events = ((ListAppender) listApp).getMessages();
try
{
assertEquals("Incorrect number of messages.", 1, events.size());
assertEquals("Incorrect message.", "o.a.l.l.t.ExitTagTest " + expected, events.get(0));
}
finally
{
((ListAppender) listApp).clear();
}
}
示例10: before
import org.apache.logging.log4j.test.appender.ListAppender; //导入依赖的package包/类
@Before
public void before() {
config = ctx.getConfiguration();
for (final Map.Entry<String, Appender> entry : config.getAppenders().entrySet()) {
if (entry.getKey().equals("List")) {
app = (ListAppender) entry.getValue();
} else if (entry.getKey().equals("HostTest")) {
host = (ListAppender) entry.getValue();
} else if (entry.getKey().equals("NoThrowable")) {
noThrown = (ListAppender) entry.getValue();
}
}
assertNotNull("No Appender", app);
assertNotNull("No Host Appender", host);
app.clear();
host.clear();
}
示例11: setupClass
import org.apache.logging.log4j.test.appender.ListAppender; //导入依赖的package包/类
@BeforeClass
public static void setupClass() {
System.setProperty(Constants.LOG4J_LOG_EVENT_FACTORY, TestLogEventFactory.class.getName());
System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG);
ctx = (LoggerContext) LogManager.getContext(false);
config = ctx.getConfiguration();
for (final Map.Entry<String, Appender> entry : config.getAppenders().entrySet()) {
if (entry.getKey().equals("List")) {
app = (ListAppender) entry.getValue();
break;
}
}
if (app == null) {
fail("No List Appender could be found");
}
}
示例12: testConfig
import org.apache.logging.log4j.test.appender.ListAppender; //导入依赖的package包/类
@Test
public void testConfig() {
final Configuration config = context.getConfiguration();
final Filter filter = config.getFilter();
assertNotNull("No MapFilter", filter);
assertTrue("Not a MapFilter", filter instanceof MapFilter);
final MapFilter mapFilter = (MapFilter) filter;
assertFalse("Should not be And filter", mapFilter.isAnd());
final Map<String, List<String>> map = mapFilter.getMap();
assertNotNull("No Map", map);
assertFalse("No elements in Map", map.isEmpty());
assertEquals("Incorrect number of elements in Map", 1, map.size());
assertTrue("Map does not contain key eventId", map.containsKey("eventId"));
assertEquals("List does not contain 2 elements", 2, map.get("eventId").size());
final Logger logger = LogManager.getLogger(MapFilterTest.class);
final Map<String, String> eventMap = new HashMap<>();
eventMap.put("eventId", "Login");
logger.debug(new StringMapMessage(eventMap));
final ListAppender app = context.getListAppender("LIST");
final List<String> msgs = app.getMessages();
assertNotNull("No messages", msgs);
assertFalse("No messages", msgs.isEmpty());
}
示例13: testLogJsonArgument
import org.apache.logging.log4j.test.appender.ListAppender; //导入依赖的package包/类
@Test
public void testLogJsonArgument() throws InterruptedException {
final ListAppender appender = (ListAppender) init.getAppender("List");
appender.countDownLatch = new CountDownLatch(4);
appender.clear();
final Logger logger = (Logger) LogManager.getRootLogger();
final String json = "{\"id\":10,\"name\":\"Alice\"}";
logger.error("log:{}", json);
// wait until background thread finished processing
final int msgCount = 1;
if (appender.getMessages().size() < msgCount) {
appender.countDownLatch.await(5, 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 String eventStr = list.get(0).toString();
Assert.assertTrue(eventStr, eventStr.contains(json));
}
示例14: 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));
}
示例15: 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);
}
}