Search in sources :

Example 6 with ListAppender

use of org.apache.logging.log4j.test.appender.ListAppender in project logging-log4j2 by apache.

the class LogWithMDCTest method testMDC.

@Test
public void testMDC() throws Exception {
    MDC.put("Key1", "John");
    MDC.put("Key2", "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("Key1 is missing", msgs.get(0).contains("Key1=John"));
    assertTrue("Key2 is missing", msgs.get(0).contains("Key2=Smith"));
}
Also used : ListAppender(org.apache.logging.log4j.test.appender.ListAppender) Test(org.junit.Test)

Example 7 with ListAppender

use of org.apache.logging.log4j.test.appender.ListAppender in project logging-log4j2 by apache.

the class LogWithRouteTest method testMDC.

@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"));
}
Also used : ListAppender(org.apache.logging.log4j.test.appender.ListAppender) Test(org.junit.Test)

Example 8 with ListAppender

use of org.apache.logging.log4j.test.appender.ListAppender in project logging-log4j2 by apache.

the class LoggerTest method testTraceWithException.

/**
     * 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);
    }
}
Also used : LogEvent(org.apache.logging.log4j.core.LogEvent) ListAppender(org.apache.logging.log4j.test.appender.ListAppender) Test(org.junit.Test)

Example 9 with ListAppender

use of org.apache.logging.log4j.test.appender.ListAppender in project logging-log4j2 by apache.

the class PatternSelectorTest method testJavaScriptPatternSelector.

@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();
}
Also used : ListAppender(org.apache.logging.log4j.test.appender.ListAppender) Test(org.junit.Test)

Example 10 with ListAppender

use of org.apache.logging.log4j.test.appender.ListAppender in project logging-log4j2 by apache.

the class PatternSelectorTest method testMarkerPatternSelector.

@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();
}
Also used : ListAppender(org.apache.logging.log4j.test.appender.ListAppender) Test(org.junit.Test)

Aggregations

ListAppender (org.apache.logging.log4j.test.appender.ListAppender)64 Test (org.junit.Test)46 Appender (org.apache.logging.log4j.core.Appender)17 Logger (org.apache.logging.log4j.Logger)8 LogEvent (org.apache.logging.log4j.core.LogEvent)6 Configuration (org.apache.logging.log4j.core.config.Configuration)5 AbstractLogger (org.apache.logging.log4j.spi.AbstractLogger)5 Marker (org.apache.logging.log4j.Marker)4 Logger (org.apache.logging.log4j.core.Logger)4 KeyValuePair (org.apache.logging.log4j.core.util.KeyValuePair)4 StructuredDataMessage (org.apache.logging.log4j.message.StructuredDataMessage)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Logger (java.util.logging.Logger)2 Log (org.apache.commons.logging.Log)2 AppenderControl (org.apache.logging.log4j.core.config.AppenderControl)2 Logger (org.slf4j.Logger)2 InputStream (java.io.InputStream)1 ObjectInputStream (java.io.ObjectInputStream)1