org.apache.logging.log4j.test.appender.ListAppender Java Examples

The following examples show how to use org.apache.logging.log4j.test.appender.ListAppender. 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: 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 #2
Source File: PatternSelectorTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #3
Source File: UserCreationListenerTest.java    From audit-log-plugin with MIT License 6 votes vote down vote up
@Before
public void setup() throws Exception {
    // user ID conformance check
    Field field = HudsonPrivateSecurityRealm.class.getDeclaredField("ID_REGEX");
    field.setAccessible(true);
    field.set(null, null);

    // credentials of four Jenkins accounts
    USERS.put("alice", "alicePassword");
    USERS.put("bob", "bobPassword");
    USERS.put("charlie", "charliePassword");
    USERS.put("debbie", "debbiePassword");

    client = j.createWebClient();
    logout(client);

    app = ListAppender.getListAppender("AuditList").clear();
}
 
Example #4
Source File: LoggingTest.java    From gcp-ingestion with Mozilla Public License 2.0 6 votes vote down vote up
@Test
public void canWriteLogs() {
  final ListAppender appender = logs.getListAppender("STDOUT");

  // emit warning with exception from an async CompletableFuture
  CompletableFuture.runAsync(() -> LoggerFactory.getLogger(LoggingTest.class).warn("msg",
      new UncheckedIOException(new IOException("test"))), ForkJoinPool.commonPool()).join();
  assertThat(appender.getMessages(), containsInAnyOrder(allOf(containsString("LoggingTest"),
      containsString("\"level\":\"WARN\""), containsString("\"message\":\"msg\""), containsString(
          "\"extendedStackTrace\":\"java.io.UncheckedIOException: java.io.IOException: test"))));

  // emit error without exception from an async CompletableFuture
  appender.clear();
  LoggerFactory.getLogger(LoggingTest.class).error("test message");
  assertThat(appender.getMessages(),
      containsInAnyOrder(allOf(containsString("LoggingTest"),
          containsString("\"level\":\"ERROR\""), containsString("\"message\":\"test message\""),
          not(containsString("extendedStackTrace")))));
}
 
Example #5
Source File: Log4j2_2195_Test.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Test
public void test() {
    logger.info("This is a test.", new Exception("Test exception!"));
    ListAppender listAppender = loggerContextRule.getListAppender("ListAppender");
    Assert.assertNotNull(listAppender);
    List<String> events = listAppender.getMessages();
    Assert.assertNotNull(events);
    Assert.assertEquals(1, events.size());
    String logEvent = events.get(0);
    Assert.assertNotNull(logEvent);
    Assert.assertFalse("\"org.junit\" should not be here", logEvent.contains("org.junit"));
    Assert.assertFalse("\"org.eclipse\" should not be here", logEvent.contains("org.eclipse"));
    //
    Layout<? extends Serializable> layout = listAppender.getLayout();
    PatternLayout pLayout = (PatternLayout) layout;
    Assert.assertNotNull(pLayout);
    Serializer eventSerializer = pLayout.getEventSerializer();
    Assert.assertNotNull(eventSerializer);
    //
    Assert.assertTrue("Missing \"|\"", logEvent.contains("|"));
}
 
Example #6
Source File: AbstractScriptFilterTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testGroovyFilter() throws Exception {
    final Logger logger = LogManager.getLogger("TestGroovyFilter");
    logger.traceEntry();
    logger.info("This should not be logged");
    ThreadContext.put("UserId", "JohnDoe");
    logger.info("This should be logged");
    ThreadContext.clearMap();
    final ListAppender app = getContext().getListAppender("List");
    try {
        final List<String> messages = app.getMessages();
        assertNotNull("No Messages", messages);
        assertTrue("Incorrect number of messages. Expected 2, Actual " + messages.size(), messages.size() == 2);
    } finally {
        app.clear();
    }
}
 
Example #7
Source File: AbstractScriptFilterTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testJavascriptFilter() throws Exception {
    final Logger logger = LogManager.getLogger("TestJavaScriptFilter");
    logger.traceEntry();
    logger.info("This should not be logged");
    ThreadContext.put("UserId", "JohnDoe");
    logger.info("This should be logged");
    ThreadContext.clearMap();
    final ListAppender app = getContext().getListAppender("List");
    final List<String> messages = app.getMessages();
    try {
        assertNotNull("No Messages", messages);
        assertTrue("Incorrect number of messages. Expected 2, Actual " + messages.size(), messages.size() == 2);
    } finally {
        app.clear();
    }
}
 
Example #8
Source File: MapFilterTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@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 IndexedReadOnlyStringMap map = mapFilter.getStringMap();
    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.<Collection<?>>getValue("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());


}
 
Example #9
Source File: PatternSelectorTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #10
Source File: PatternSelectorTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testScriptPatternSelector() throws Exception {
    final org.apache.logging.log4j.Logger logger = LogManager.getLogger("TestScriptPatternSelector");
    final org.apache.logging.log4j.Logger logger2 = LogManager.getLogger("NoLocation");
    logger.traceEntry();
    logger.info("Hello World");
    logger2.info("No location information");
    logger.traceExit();
    final ListAppender app = (ListAppender) context.getRequiredAppender("List2");
    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] TestScriptPatternSelector ====== " +
            "o.a.l.l.c.PatternSelectorTest.testScriptPatternSelector:62 Enter ======" + Strings.LINE_SEPARATOR;
    assertEquals(expect, messages.get(0));
    expect = "[INFO ] TestScriptPatternSelector o.a.l.l.c.PatternSelectorTest.testScriptPatternSelector.63 " +
            "Hello World" + Strings.LINE_SEPARATOR;
    assertEquals(expect, messages.get(1));
    assertEquals("[INFO ] NoLocation No location information" + Strings.LINE_SEPARATOR, messages.get(2));
    app.clear();
}
 
Example #11
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 #12
Source File: CategoryTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@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);
    ((org.apache.logging.log4j.core.Logger) 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));
}
 
Example #13
Source File: LogWithRouteTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testMDC() throws Exception {
    MDC.put("Type", "Service");
    MDC.put("Name", "John Smith");
    try {
        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"));
    } finally {
        MDC.remove("Type");
        MDC.remove("Name");
    }
}
 
Example #14
Source File: LogWithMDCTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testMDC() throws Exception {
    MDC.put("Key1", "John");
    MDC.put("Key2", "Smith");
    try {
        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"));
    } finally {
        MDC.remove("Key1");
        MDC.remove("Key2");
    }
}
 
Example #15
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 #16
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 #17
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 #18
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 #19
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 #20
Source File: CategoryTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testClassName() {
    final Category category = Category.getInstance("TestCategory");
    final Layout<String> layout = PatternLayout.newBuilder().setPattern("%d %p %C{1.} [%t] %m%n").build();
    final ListAppender appender = new ListAppender("List2", null, layout, false, false);
    appender.start();
    category.setAdditivity(false);
    ((org.apache.logging.log4j.core.Logger) 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));
}
 
Example #21
Source File: LogWithRouteTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testMDC() throws Exception {
    MDC.put("Type", "Service");
    MDC.put("Name", "John Smith");
    try {
        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"));
    } finally {
        MDC.remove("Type");
        MDC.remove("Name");
    }
}
 
Example #22
Source File: LogWithMDCTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testMDC() throws Exception {
    MDC.put("Key1", "John");
    MDC.put("Key2", "Smith");
    try {
        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"));
    } finally {
        MDC.remove("Key1");
        MDC.remove("Key2");
    }
}
 
Example #23
Source File: LoggerTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private void verify(final String name, final String expected) {
    final ListAppender listApp = ctx.getListAppender(name);
    assertNotNull("Missing Appender", listApp);
    final List<String> events = listApp.getMessages();
    assertTrue("Incorrect number of messages. Expected 1 Actual " + events.size(), events.size()== 1);
    final String actual = events.get(0);
    assertEquals("Incorrect message. Expected " + expected + ". Actual " + actual, expected, actual);
    listApp.clear();
}
 
Example #24
Source File: CallerInformationTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testMethodLogger() throws Exception {
    final ListAppender app = ctx.getListAppender("Method").clear();
    final Log logger = LogFactory.getLog("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);
    }
}
 
Example #25
Source File: CallerInformationTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testClassLogger() throws Exception {
    final ListAppender app = ctx.getListAppender("Class").clear();
    final Log logger = LogFactory.getLog("ClassLogger");
    logger.info("Ignored message contents.");
    logger.warn("Verifying the caller class is still correct.");
    logger.error("Hopefully nobody breaks me!");
    final List<String> messages = app.getMessages();
    assertEquals("Incorrect number of messages.", 3, messages.size());
    for (final String message : messages) {
        assertEquals("Incorrect caller class name.", this.getClass().getName(), message);
    }
}
 
Example #26
Source File: ApiLoggerTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    logger = Logger.getLogger(LOGGER_NAME);
    assertThat(logger.getLevel(), equalTo(java.util.logging.Level.FINE));
    eventAppender = ListAppender.getListAppender("TestAppender");
    flowAppender = ListAppender.getListAppender("FlowAppender");
    stringAppender = ListAppender.getListAppender("StringAppender");
    assertNotNull(eventAppender);
    assertNotNull(flowAppender);
    assertNotNull(stringAppender);
}
 
Example #27
Source File: CallerInformationTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testMethodLogger() throws Exception {
    final ListAppender app = ctx.getListAppender("Method").clear();
    final Logger logger = LoggerFactory.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);
    }
}
 
Example #28
Source File: BracketInNotInterpolatedMessageTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
public void noInterpolation() {
    final Logger logger = Logger.getLogger("Test");
    logger.info("{raw}");
    logger.log(new LogRecord(INFO, "{raw}"));// should lead to the same as previous but was not the case LOG4J2-1251
    final List<LogEvent> events = ListAppender.getListAppender("TestAppender").getEvents();
    assertThat(events, hasSize(2));
    assertEquals("{raw}", events.get(0).getMessage().getFormattedMessage());
    assertEquals("{raw}", events.get(1).getMessage().getFormattedMessage());
}
 
Example #29
Source File: ExitTagTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private void verify(final String expected) {
    final ListAppender listApp = context.getListAppender("List");
    final List<String> events = 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
    {
        listApp.clear();
    }
}
 
Example #30
Source File: CallerInformationTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testMethodLogger() throws Exception {
    final ListAppender app = ctx.getListAppender("Method").clear();
    final Logger logger = Logger.getLogger("MethodLogger");
    logger.info("More messages.");
    logger.warning("CATASTROPHE INCOMING!");
    logger.severe("ZOMBIES!!!");
    logger.warning("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);
    }
}