Search in sources :

Example 56 with ListAppender

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

Example 57 with ListAppender

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

Example 58 with ListAppender

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

Example 59 with ListAppender

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()));
}
Also used : ExtendedLogger(org.apache.logging.log4j.spi.ExtendedLogger) LoggingException(org.apache.logging.log4j.LoggingException) ListAppender(org.apache.logging.log4j.core.test.appender.ListAppender) LoggingException(org.apache.logging.log4j.LoggingException)

Example 60 with ListAppender

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));
}
Also used : ExtendedLogger(org.apache.logging.log4j.spi.ExtendedLogger) ListAppender(org.apache.logging.log4j.core.test.appender.ListAppender)

Aggregations

ListAppender (org.apache.logging.log4j.core.test.appender.ListAppender)60 Test (org.junit.Test)24 Appender (org.apache.logging.log4j.core.Appender)21 Test (org.junit.jupiter.api.Test)15 Configuration (org.apache.logging.log4j.core.config.Configuration)6 StructuredDataMessage (org.apache.logging.log4j.message.StructuredDataMessage)5 AbstractLogger (org.apache.logging.log4j.spi.AbstractLogger)5 ConsoleAppender (org.apache.logging.log4j.core.appender.ConsoleAppender)4 KeyValuePair (org.apache.logging.log4j.core.util.KeyValuePair)4 AbstractJacksonLayout (org.apache.logging.log4j.jackson.AbstractJacksonLayout)4 Marker (org.apache.logging.log4j.Marker)3 LogEvent (org.apache.logging.log4j.core.LogEvent)3 Logger (org.apache.logging.log4j.core.Logger)3 DefaultConfiguration (org.apache.logging.log4j.core.config.DefaultConfiguration)3 ExtendedLogger (org.apache.logging.log4j.spi.ExtendedLogger)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 Logger (java.util.logging.Logger)2 Log (org.apache.commons.logging.Log)2 LoggerContext (org.apache.logging.log4j.core.LoggerContext)2 RoutingAppender (org.apache.logging.log4j.core.appender.routing.RoutingAppender)2