Search in sources :

Example 11 with LoggerContextSource

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

the class FileOutputTest method testConfig.

@Test
@LoggerContextSource("classpath:log4j-filetest.xml")
public void testConfig() throws IOException {
    final Path logFile = Paths.get("target", "status.log");
    assertTrue(Files.exists(logFile), "Status output file does not exist");
    assertTrue(Files.size(logFile) > 0, "File is empty");
}
Also used : Path(java.nio.file.Path) Test(org.junit.jupiter.api.Test) LoggerContextSource(org.apache.logging.log4j.core.test.junit.LoggerContextSource)

Example 12 with LoggerContextSource

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

the class StyleConverterTest method testReplacement.

@Test
@LoggerContextSource("log4j-style.xml")
public void testReplacement(final LoggerContext context, @Named("List") final ListAppender app) {
    final Logger logger = context.getLogger("LoggerTest");
    logger.error(this.getClass().getName());
    final List<String> msgs = app.getMessages();
    assertNotNull(msgs);
    assertEquals(1, msgs.size(), "Incorrect number of messages. Should be 1 is " + msgs.size());
    assertTrue(msgs.get(0).endsWith(EXPECTED), "Replacement failed - expected ending " + EXPECTED + ", actual " + msgs.get(0));
}
Also used : Logger(org.apache.logging.log4j.Logger) Test(org.junit.jupiter.api.Test) LoggerContextSource(org.apache.logging.log4j.core.test.junit.LoggerContextSource)

Example 13 with LoggerContextSource

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

the class ConcurrentLoggingWithGelfLayoutTest method testConcurrentLogging.

@Test
@LoggerContextSource("log4j2-gelf-layout.xml")
public void testConcurrentLogging(final LoggerContext context) throws Throwable {
    final Logger log = context.getLogger(ConcurrentLoggingWithGelfLayoutTest.class);
    final int threadCount = Runtime.getRuntime().availableProcessors() * 2;
    final CountDownLatch latch = new CountDownLatch(threadCount);
    final List<Throwable> thrown = Collections.synchronizedList(new ArrayList<>());
    for (int x = 0; x < threadCount; x++) {
        final Thread t = new Thread(() -> {
            log.info(latch.getCount());
            try {
                for (int i = 0; i < 64; i++) {
                    log.info("First message.");
                    log.info("Second message.");
                }
            } finally {
                latch.countDown();
            }
        });
        // Appender is configured with ignoreExceptions="false";
        // any exceptions are propagated to the caller, so we can catch them here.
        t.setUncaughtExceptionHandler((t1, e) -> thrown.add(e));
        t.start();
    }
    latch.await();
    // if any error occurred, fail this test
    if (!thrown.isEmpty()) {
        throw thrown.get(0);
    }
    // simple test to ensure content is not corrupted
    if (Files.exists(PATH)) {
        try (Stream<String> lines = Files.lines(PATH, Charset.defaultCharset())) {
            lines.forEach(line -> assertThat(line, both(startsWith("{\"version\":\"1.1\",\"host\":\"myself\",\"timestamp\":")).and(endsWith("\"}"))));
        }
    }
}
Also used : Logger(org.apache.logging.log4j.Logger) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test) LoggerContextSource(org.apache.logging.log4j.core.test.junit.LoggerContextSource)

Example 14 with LoggerContextSource

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

the class DynamicThresholdFilterTest method testConfig.

@Test
@LoggerContextSource("log4j2-dynamicfilter.xml")
public void testConfig(final Configuration config) {
    final Filter filter = config.getFilter();
    assertNotNull(filter, "No DynamicThresholdFilter");
    assertTrue(filter instanceof DynamicThresholdFilter, "Not a DynamicThresholdFilter");
    final DynamicThresholdFilter dynamic = (DynamicThresholdFilter) filter;
    final String key = dynamic.getKey();
    assertNotNull(key, "Key is null");
    assertEquals("loginId", key, "Incorrect key value");
    final Map<String, Level> map = dynamic.getLevelMap();
    assertNotNull(map, "Map is null");
    assertEquals(1, map.size(), "Incorrect number of map elements");
}
Also used : Filter(org.apache.logging.log4j.core.Filter) Level(org.apache.logging.log4j.Level) Test(org.junit.jupiter.api.Test) LoggerContextSource(org.apache.logging.log4j.core.test.junit.LoggerContextSource)

Example 15 with LoggerContextSource

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

the class StructuredDataFilterTest method testConfig.

@Test
@LoggerContextSource("log4j2-sdfilter.xml")
public void testConfig(final Configuration config) {
    final Filter filter = config.getFilter();
    assertNotNull(filter, "No StructuredDataFilter");
    assertTrue(filter instanceof StructuredDataFilter, "Not a StructuredDataFilter");
    final StructuredDataFilter sdFilter = (StructuredDataFilter) filter;
    assertFalse(sdFilter.isAnd(), "Should not be And filter");
    final IndexedReadOnlyStringMap map = sdFilter.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");
}
Also used : Filter(org.apache.logging.log4j.core.Filter) Collection(java.util.Collection) IndexedReadOnlyStringMap(org.apache.logging.log4j.util.IndexedReadOnlyStringMap) 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