Java Code Examples for org.apache.logging.log4j.test.appender.ListAppender#stop()

The following examples show how to use org.apache.logging.log4j.test.appender.ListAppender#stop() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CsvParameterLayoutTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example 2
Source File: LoggerTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 3
Source File: LoggerTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
/**
 * Tests isTraceEnabled.
 */
@Test
public void testIsTraceEnabled() {
    final ListAppender appender = new ListAppender("List");
    appender.start();
    final Logger root = Logger.getRootLogger();
    try {
        ((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);

        assertTrue(tracer.isTraceEnabled());
        assertFalse(root.isTraceEnabled());
        appender.stop();
    } finally {
        ((org.apache.logging.log4j.core.Logger) root.getLogger()).removeAppender(appender);
    }
}
 
Example 4
Source File: LoggerTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@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 {
        ((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);
    }
}
 
Example 5
Source File: LoggerTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 6
Source File: LoggerTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
/**
 * Tests isTraceEnabled.
 */
@Test
public void testIsTraceEnabled() {
    final ListAppender appender = new ListAppender("List");
    appender.start();
    final Logger root = Logger.getRootLogger();
    try {
        ((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);

        assertTrue(tracer.isTraceEnabled());
        assertFalse(root.isTraceEnabled());
        appender.stop();
    } finally {
        ((org.apache.logging.log4j.core.Logger) root.getLogger()).removeAppender(appender);
    }
}
 
Example 7
Source File: LoggerTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@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);
    }
}
 
Example 8
Source File: CsvParameterLayoutTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
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));
}
 
Example 9
Source File: LoggerTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
/**
 * Tests logger.trace(Object, Exception).
 */
@Test
public void testTraceWithException() {
    final ListAppender appender = new ListAppender("List");
    appender.start();
    final Logger root = Logger.getRootLogger();
    try {
        ((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);
        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 {
        ((org.apache.logging.log4j.core.Logger) root.getLogger()).removeAppender(appender);
    }
}
 
Example 10
Source File: LoggerTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
/**
 * Tests logger.trace(Object, Exception).
 */
@Test
public void testTraceWithException() {
    final ListAppender appender = new ListAppender("List");
    appender.start();
    final Logger root = Logger.getRootLogger();
    try {
        ((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);
        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 {
        ((org.apache.logging.log4j.core.Logger) root.getLogger()).removeAppender(appender);
    }
}
 
Example 11
Source File: PatternLayoutMainMapLookupTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testHeader() {
    final ListAppender listApp = context.getListAppender("List");
    final Logger logger = context.getLogger(this.getClass().getName());
    logger.info("Hello World");
    final List<String> initialMessages = listApp.getMessages();
    Assert.assertFalse(initialMessages.isEmpty());
    final String messagesStr = initialMessages.toString();
    Assert.assertEquals(messagesStr, "Header: value0", initialMessages.get(0));
    listApp.stop();
    final List<String> finalMessages = listApp.getMessages();
    Assert.assertEquals(3, finalMessages.size());
    Assert.assertEquals("Footer: value1", finalMessages.get(2));
}