Search in sources :

Example 16 with LoggerContextSource

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

the class PropertiesConfigurationTest method testPropertiesConfiguration.

@Test
@LoggerContextSource("log4j2-properties.properties")
void testPropertiesConfiguration(final Configuration config) {
    assertEquals(config.getState(), LifeCycle.State.STARTED, "Incorrect State: " + config.getState());
    final Map<String, Appender> appenders = config.getAppenders();
    assertNotNull(appenders);
    assertEquals(1, appenders.size(), "Incorrect number of Appenders: " + appenders.size());
    final Map<String, LoggerConfig> loggers = config.getLoggers();
    assertNotNull(loggers);
    assertEquals(2, loggers.size(), "Incorrect number of LoggerConfigs: " + loggers.size());
    final Filter filter = config.getFilter();
    assertNotNull(filter, "No Filter");
    assertTrue(filter instanceof ThresholdFilter, "Not a Threshold Filter");
    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 17 with LoggerContextSource

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

the class PropertiesConfigurationTest method testRollingFile.

@Test
@LoggerContextSource("log4j-rolling.properties")
void testRollingFile(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(), 3, "Incorrect number of Appenders: " + appenders.size());
    final Map<String, LoggerConfig> loggers = config.getLoggers();
    assertNotNull(loggers);
    assertEquals(loggers.size(), 2, "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 18 with LoggerContextSource

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

the class PropertiesConfigurationTest method testTrailingSpaceOnLevel.

@Test
@LoggerContextSource("log4j2-properties-trailing-space-on-level.properties")
void testTrailingSpaceOnLevel(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(), 2, "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());
    assertEquals(Level.DEBUG, logger.getLevel(), "Incorrect level " + logger.getLevel());
    logger.debug("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 19 with LoggerContextSource

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

the class CassandraAppenderIT method appendManyEvents.

@Disabled("Doesn't work in Java 11 at this Cassandra version")
@Test
@CassandraFixture(keyspace = "test", setup = DDL)
@LoggerContextSource("CassandraAppenderTest.xml")
public void appendManyEvents(final LoggerContext context, final Cluster cluster) throws Exception {
    final Logger logger = context.getLogger(getClass());
    ThreadContext.put("test", "mdc");
    ThreadContext.push("ndc");
    for (int i = 0; i < 20; i++) {
        logger.info(MarkerManager.getMarker("MARKER"), "Test log message");
    }
    ThreadContext.clearAll();
    TimeUnit.SECONDS.sleep(3);
    int i = 0;
    try (final Session session = cluster.connect("test")) {
        for (final Row row : session.execute("SELECT * FROM logs")) {
            assertNotNull(row.get("id", UUID.class));
            assertNotNull(row.get("timeid", UUID.class));
            assertNotNull(row.get("timestamp", Date.class));
            assertEquals("Test log message", row.getString("message"));
            assertEquals("MARKER", row.getString("marker"));
            assertEquals("INFO", row.getString("level"));
            assertEquals(getClass().getName(), row.getString("logger"));
            final Map<String, String> mdc = row.getMap("mdc", String.class, String.class);
            assertEquals(1, mdc.size());
            assertEquals("mdc", mdc.get("test"));
            final List<String> ndc = row.getList("ndc", String.class);
            assertEquals(1, ndc.size());
            assertEquals("ndc", ndc.get(0));
            ++i;
        }
    }
    assertEquals(20, i);
}
Also used : Row(com.datastax.driver.core.Row) Logger(org.apache.logging.log4j.Logger) UUID(java.util.UUID) Date(java.util.Date) Session(com.datastax.driver.core.Session) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled) LoggerContextSource(org.apache.logging.log4j.core.test.junit.LoggerContextSource)

Example 20 with LoggerContextSource

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

the class ConfigurationFactoryTest method xinclude.

@Test
@LoggerContextSource("log4j-xinclude.xml")
void xinclude(final LoggerContext context) throws IOException {
    checkConfiguration(context);
    final Path logFile = Paths.get("target", "test-xinclude.log");
    checkFileLogger(context, logFile);
}
Also used : Path(java.nio.file.Path) 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