use of org.apache.logging.log4j.core.test.appender.ListAppender in project logging-log4j2 by apache.
the class LoggerTest method testLog.
@Test
@SuppressWarnings("deprecation")
public void testLog() {
final PatternLayout layout = PatternLayout.newBuilder().setPattern("%d %C %L %m").build();
final ListAppender appender = new ListAppender("List", null, layout, false, false);
appender.start();
final Logger root = Logger.getRootLogger();
try {
((org.apache.logging.log4j.core.Logger) 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 {
((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 LoggerTest method testTrace.
/* Don't support hierarchy
public void testHierarchy1() {
Hierarchy h = new Hierarchy(new RootLogger((Level) Level.ERROR));
Logger a0 = h.getLogger("a");
assertEquals("a", a0.getName());
assertNull(a0.getLevel());
assertSame(Level.ERROR, a0.getEffectiveLevel());
Logger a1 = h.getLogger("a");
assertSame(a0, a1);
} */
/**
* Tests logger.trace(Object).
*/
@Test
public void testTrace() {
final ListAppender appender = new ListAppender("List");
appender.start();
final Logger root = Logger.getRootLogger();
((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);
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();
((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 testMethodLogger.
@Test
public void testMethodLogger() throws Exception {
final ListAppender app = ctx.getListAppender("Method").clear();
final Logger logger = Logger.getLogger("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);
}
}
use of org.apache.logging.log4j.core.test.appender.ListAppender in project logging-log4j2 by apache.
the class AsyncAppenderTest method exceptionTest.
static void exceptionTest(final LoggerContext context) throws InterruptedException {
final ExtendedLogger logger = context.getLogger(AsyncAppender.class);
final Exception parent = new IllegalStateException("Test");
final Throwable child = new LoggingException("This is a test", parent);
logger.error("This is a test", child);
final ListAppender appender = context.getConfiguration().getAppender("List");
final List<String> messages;
try {
messages = appender.getMessages(1, 2, TimeUnit.SECONDS);
} finally {
appender.clear();
}
assertNotNull(messages);
assertEquals(1, messages.size());
assertTrue(messages.get(0).contains(parent.getClass().getName()));
}
use of org.apache.logging.log4j.core.test.appender.ListAppender in project logging-log4j2 by apache.
the class AsyncAppenderTest method rewriteTest.
static void rewriteTest(final LoggerContext context) throws InterruptedException {
final ExtendedLogger logger = context.getLogger(AsyncAppender.class);
logger.error("This is a test");
logger.warn("Hello world!");
final ListAppender appender = context.getConfiguration().getAppender("List");
final List<String> messages;
try {
messages = appender.getMessages(2, 2, TimeUnit.SECONDS);
} finally {
appender.clear();
}
assertNotNull(messages);
assertEquals(2, messages.size());
final String messagePrefix = AsyncAppenderTest.class.getName() + " rewriteTest ";
assertEquals(messagePrefix + "This is a test", messages.get(0));
assertEquals(messagePrefix + "Hello world!", messages.get(1));
}
Aggregations