Search in sources :

Example 1 with LoggerContextSource

use of org.apache.logging.log4j.core.test.junit.LoggerContextSource in project logging-log4j2 by apache.

the class MapFilterTest method testConfig.

@Test
@LoggerContextSource("log4j2-mapfilter.xml")
public void testConfig(final Configuration config, @Named("LIST") final ListAppender app) {
    final Filter filter = config.getFilter();
    assertNotNull(filter, "No MapFilter");
    assertTrue(filter instanceof MapFilter, "Not a MapFilter");
    final MapFilter mapFilter = (MapFilter) filter;
    assertFalse(mapFilter.isAnd(), "Should not be And filter");
    final IndexedReadOnlyStringMap map = mapFilter.getStringMap();
    assertNotNull(map, "No Map");
    assertFalse(map.isEmpty(), "No elements in Map");
    assertEquals(1, map.size(), "Incorrect number of elements in Map");
    assertTrue(map.containsKey("eventId"), "Map does not contain key eventId");
    assertEquals(2, map.<Collection<?>>getValue("eventId").size(), "List does not contain 2 elements");
    final Logger logger = LogManager.getLogger(MapFilterTest.class);
    final Map<String, String> eventMap = new HashMap<>();
    eventMap.put("eventId", "Login");
    logger.debug(new StringMapMessage(eventMap));
    final List<String> msgs = app.getMessages();
    assertNotNull(msgs, "No messages");
    assertFalse(msgs.isEmpty(), "No messages");
}
Also used : StringMapMessage(org.apache.logging.log4j.message.StringMapMessage) Filter(org.apache.logging.log4j.core.Filter) HashMap(java.util.HashMap) Collection(java.util.Collection) IndexedReadOnlyStringMap(org.apache.logging.log4j.util.IndexedReadOnlyStringMap) Logger(org.apache.logging.log4j.Logger) Test(org.junit.jupiter.api.Test) LoggerContextSource(org.apache.logging.log4j.core.test.junit.LoggerContextSource)

Example 2 with LoggerContextSource

use of org.apache.logging.log4j.core.test.junit.LoggerContextSource in project logging-log4j2 by apache.

the class PropertiesConfigurationTest method testRootLoggerOnly.

@Test
@LoggerContextSource("log4j2-properties-root-only.properties")
void testRootLoggerOnly(final Configuration config) {
    assertEquals(config.getState(), LifeCycle.State.STARTED, "Incorrect State: " + config.getState());
    final Map<String, Appender> appenders = config.getAppenders();
    assertNotNull(appenders);
    assertEquals(appenders.size(), 1, "Incorrect number of Appenders: " + appenders.size());
    final Map<String, LoggerConfig> loggers = config.getLoggers();
    assertNotNull(loggers);
    assertEquals(loggers.size(), 1, "Incorrect number of LoggerConfigs: " + loggers.size());
    final Filter filter = config.getFilter();
    assertNotNull(filter, "No Filter");
    assertThat(filter, instanceOf(ThresholdFilter.class));
    final Logger logger = LogManager.getLogger(getClass());
    logger.info("Welcome to Log4j!");
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ListAppender(org.apache.logging.log4j.core.test.appender.ListAppender) ThresholdFilter(org.apache.logging.log4j.core.filter.ThresholdFilter) Filter(org.apache.logging.log4j.core.Filter) ThresholdFilter(org.apache.logging.log4j.core.filter.ThresholdFilter) Logger(org.apache.logging.log4j.Logger) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig) Test(org.junit.jupiter.api.Test) LoggerContextSource(org.apache.logging.log4j.core.test.junit.LoggerContextSource)

Example 3 with LoggerContextSource

use of org.apache.logging.log4j.core.test.junit.LoggerContextSource in project logging-log4j2 by apache.

the class MemoryMappedFileAppenderTest method testMemMapLocation.

@Test
@LoggerContextSource("MemoryMappedFileAppenderLocationTest.xml")
void testMemMapLocation(final LoggerContext context) throws Exception {
    final Logger log = context.getLogger(getClass());
    final Path logFile = Paths.get("target", "MemoryMappedFileAppenderLocationTest.log");
    final int expectedFileLength = Integers.ceilingNextPowerOfTwo(32000);
    assertEquals(32768, expectedFileLength);
    try {
        log.warn("Test log1");
        assertTrue(Files.exists(logFile));
        assertEquals(expectedFileLength, Files.size(logFile));
        log.warn("Test log2");
        assertEquals(expectedFileLength, Files.size(logFile));
    } finally {
        context.stop();
    }
    int expectedLength = Strings.isEmpty(System.getProperty("jdk.module.path")) ? 272 : 332;
    assertEquals(expectedLength + 2 * System.lineSeparator().length(), Files.size(logFile), "Expected file size to shrink");
    final List<String> lines = Files.readAllLines(logFile);
    assertEquals(2, lines.size());
    assertTrue(lines.get(0).endsWith("org.apache.logging.log4j.core.appender.MemoryMappedFileAppenderTest.testMemMapLocation(MemoryMappedFileAppenderTest.java:105): Test log1"));
    assertTrue(lines.get(1).endsWith("org.apache.logging.log4j.core.appender.MemoryMappedFileAppenderTest.testMemMapLocation(MemoryMappedFileAppenderTest.java:108): Test log2"));
}
Also used : Path(java.nio.file.Path) Logger(org.apache.logging.log4j.Logger) Test(org.junit.jupiter.api.Test) LoggerContextSource(org.apache.logging.log4j.core.test.junit.LoggerContextSource)

Example 4 with LoggerContextSource

use of org.apache.logging.log4j.core.test.junit.LoggerContextSource in project logging-log4j2 by apache.

the class MemoryMappedFileAppenderTest method testMemMapBasics.

@Test
@LoggerContextSource("MemoryMappedFileAppenderTest.xml")
public void testMemMapBasics(final LoggerContext context) throws Exception {
    final Logger log = context.getLogger(getClass());
    final Path logFile = Paths.get("target", "MemoryMappedFileAppenderTest.log");
    try {
        log.warn("Test log1");
        assertTrue(Files.exists(logFile));
        assertEquals(MemoryMappedFileManager.DEFAULT_REGION_LENGTH, Files.size(logFile));
        log.warn("Test log2");
        assertEquals(MemoryMappedFileManager.DEFAULT_REGION_LENGTH, Files.size(logFile));
    } finally {
        context.stop();
    }
    final int LINESEP = System.lineSeparator().length();
    assertEquals(18 + 2 * LINESEP, Files.size(logFile));
    final List<String> lines = Files.readAllLines(logFile);
    assertThat(lines, both(hasSize(2)).and(contains("Test log1", "Test log2")));
}
Also used : Path(java.nio.file.Path) Logger(org.apache.logging.log4j.Logger) Test(org.junit.jupiter.api.Test) LoggerContextSource(org.apache.logging.log4j.core.test.junit.LoggerContextSource)

Example 5 with LoggerContextSource

use of org.apache.logging.log4j.core.test.junit.LoggerContextSource in project logging-log4j2 by apache.

the class AsyncAppenderTest method testNoLocationInformation.

@Test
@LoggerContextSource("log4j-asynch-no-location.xml")
public void testNoLocationInformation(final LoggerContext context, @Named("List") final ListAppender appender) throws InterruptedException {
    final ExtendedLogger logger = context.getLogger(getClass());
    logger.error("This is a test");
    logger.warn("Hello world!");
    final List<String> messages;
    try {
        messages = appender.getMessages(2, 2, TimeUnit.SECONDS);
    } finally {
        appender.clear();
    }
    assertNotNull(messages);
    assertEquals(2, messages.size());
    assertEquals("?  This is a test", messages.get(0));
    assertEquals("?  Hello world!", messages.get(1));
}
Also used : ExtendedLogger(org.apache.logging.log4j.spi.ExtendedLogger) Test(org.junit.jupiter.api.Test) LoggerContextSource(org.apache.logging.log4j.core.test.junit.LoggerContextSource)

Aggregations

LoggerContextSource (org.apache.logging.log4j.core.test.junit.LoggerContextSource)26 Test (org.junit.jupiter.api.Test)26 Logger (org.apache.logging.log4j.Logger)14 Path (java.nio.file.Path)9 Filter (org.apache.logging.log4j.core.Filter)7 Appender (org.apache.logging.log4j.core.Appender)4 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)4 ThresholdFilter (org.apache.logging.log4j.core.filter.ThresholdFilter)4 ListAppender (org.apache.logging.log4j.core.test.appender.ListAppender)4 Collection (java.util.Collection)2 ExtendedLogger (org.apache.logging.log4j.spi.ExtendedLogger)2 StatusLogger (org.apache.logging.log4j.status.StatusLogger)2 IndexedReadOnlyStringMap (org.apache.logging.log4j.util.IndexedReadOnlyStringMap)2 Tag (org.junit.jupiter.api.Tag)2 Row (com.datastax.driver.core.Row)1 Session (com.datastax.driver.core.Session)1 Field (java.lang.reflect.Field)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 UUID (java.util.UUID)1