Search in sources :

Example 11 with Configuration

use of org.apache.logging.log4j.core.config.Configuration in project logging-log4j2 by apache.

the class Log4j1ConfigurationFactoryTest method testNullAppender.

@Test
public void testNullAppender() throws Exception {
    final Configuration configuration = getConfiguration("config-1.2/log4j-NullAppender.properties");
    final Appender appender = configuration.getAppender("NullAppender");
    assertNotNull(appender);
    assertEquals("NullAppender", appender.getName());
    assertTrue(appender.getClass().getName(), appender instanceof NullAppender);
}
Also used : Appender(org.apache.logging.log4j.core.Appender) NullAppender(org.apache.logging.log4j.core.appender.NullAppender) ConsoleAppender(org.apache.logging.log4j.core.appender.ConsoleAppender) FileAppender(org.apache.logging.log4j.core.appender.FileAppender) RollingFileAppender(org.apache.logging.log4j.core.appender.RollingFileAppender) Configuration(org.apache.logging.log4j.core.config.Configuration) NullAppender(org.apache.logging.log4j.core.appender.NullAppender) Test(org.junit.Test)

Example 12 with Configuration

use of org.apache.logging.log4j.core.config.Configuration in project logging-log4j2 by apache.

the class LoggerTest method testReconfiguration.

@Test
public void testReconfiguration() throws Exception {
    final Configuration oldConfig = context.getConfiguration();
    final int MONITOR_INTERVAL_SECONDS = 5;
    final File file = new File("target/test-classes/" + CONFIG);
    final long orig = file.lastModified();
    final long newTime = orig + 10000;
    assertTrue("setLastModified should have succeeded.", file.setLastModified(newTime));
    TimeUnit.SECONDS.sleep(MONITOR_INTERVAL_SECONDS + 1);
    for (int i = 0; i < 17; ++i) {
        logger.debug("Reconfigure");
    }
    Thread.sleep(100);
    for (int i = 0; i < 20; i++) {
        if (context.getConfiguration() != oldConfig) {
            break;
        }
        Thread.sleep(50);
    }
    final Configuration newConfig = context.getConfiguration();
    assertNotNull("No configuration", newConfig);
    assertNotSame("Reconfiguration failed", newConfig, oldConfig);
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) File(java.io.File) Test(org.junit.Test)

Example 13 with Configuration

use of org.apache.logging.log4j.core.config.Configuration in project logging-log4j2 by apache.

the class CustomLevelsWithFiltersTest method testConfiguration.

@Test
public void testConfiguration() {
    final Configuration configuration = context.getConfiguration();
    assertNotNull(configuration);
    final FileAppender appender = configuration.getAppender("info");
    assertNotNull(appender);
    final CompositeFilter compFilter = (CompositeFilter) appender.getFilter();
    assertNotNull(compFilter);
    final Filter[] filters = compFilter.getFiltersArray();
    assertNotNull(filters);
    boolean foundLevel = false;
    for (final Filter filter : filters) {
        final ThresholdFilter tFilter = (ThresholdFilter) filter;
        if (infom1Level.equals(tFilter.getLevel())) {
            foundLevel = true;
            break;
        }
    }
    Assert.assertTrue("Level not found: " + infom1Level, foundLevel);
}
Also used : FileAppender(org.apache.logging.log4j.core.appender.FileAppender) ThresholdFilter(org.apache.logging.log4j.core.filter.ThresholdFilter) CompositeFilter(org.apache.logging.log4j.core.filter.CompositeFilter) Configuration(org.apache.logging.log4j.core.config.Configuration) CompositeFilter(org.apache.logging.log4j.core.filter.CompositeFilter) ThresholdFilter(org.apache.logging.log4j.core.filter.ThresholdFilter) Test(org.junit.Test)

Example 14 with Configuration

use of org.apache.logging.log4j.core.config.Configuration in project logging-log4j2 by apache.

the class LoggerUpdateTest method resetLevel.

@Test
public void resetLevel() {
    final org.apache.logging.log4j.Logger logger = context.getLogger("com.apache.test");
    logger.traceEntry();
    List<LogEvent> events = app.getEvents();
    assertEquals("Incorrect number of events. Expected 1, actual " + events.size(), 1, events.size());
    app.clear();
    final LoggerContext ctx = LoggerContext.getContext(false);
    final Configuration config = ctx.getConfiguration();
    final LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
    /* You could also specify the actual logger name as below and it will return the LoggerConfig used by the Logger.
           LoggerConfig loggerConfig = getLoggerConfig("com.apache.test");
        */
    loggerConfig.setLevel(Level.DEBUG);
    // This causes all Loggers to refetch information from their LoggerConfig.
    ctx.updateLoggers();
    logger.traceEntry();
    events = app.getEvents();
    assertEquals("Incorrect number of events. Expected 0, actual " + events.size(), 0, events.size());
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) org.apache.logging.log4j(org.apache.logging.log4j) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig) Test(org.junit.Test)

Example 15 with Configuration

use of org.apache.logging.log4j.core.config.Configuration in project logging-log4j2 by apache.

the class PropertiesFileConfigTest method testReconfiguration.

@Test
public void testReconfiguration() throws Exception {
    final Configuration oldConfig = context.getConfiguration();
    final int MONITOR_INTERVAL_SECONDS = 5;
    final File file = new File(CONFIG);
    final long orig = file.lastModified();
    final long newTime = orig + 10000;
    assertTrue("setLastModified should have succeeded.", file.setLastModified(newTime));
    TimeUnit.SECONDS.sleep(MONITOR_INTERVAL_SECONDS + 1);
    for (int i = 0; i < 17; ++i) {
        logger.debug("Reconfigure");
    }
    final int loopCount = 0;
    Configuration newConfig;
    do {
        Thread.sleep(100);
        newConfig = context.getConfiguration();
    } while (newConfig == oldConfig && loopCount < 5);
    assertNotSame("Reconfiguration failed", newConfig, oldConfig);
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) File(java.io.File) Test(org.junit.Test)

Aggregations

Configuration (org.apache.logging.log4j.core.config.Configuration)210 LoggerContext (org.apache.logging.log4j.core.LoggerContext)120 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)71 Test (org.junit.Test)54 Appender (org.apache.logging.log4j.core.Appender)50 DefaultConfiguration (org.apache.logging.log4j.core.config.DefaultConfiguration)37 Test (org.junit.jupiter.api.Test)29 FileAppender (org.apache.logging.log4j.core.appender.FileAppender)22 File (java.io.File)21 RollingFileAppender (org.apache.logging.log4j.core.appender.RollingFileAppender)20 Path (java.nio.file.Path)14 Map (java.util.Map)14 ArrayList (java.util.ArrayList)13 Level (org.apache.logging.log4j.Level)13 ListAppender (org.apache.log4j.ListAppender)12 ConsoleAppender (org.apache.logging.log4j.core.appender.ConsoleAppender)12 ConfigurationSource (org.apache.logging.log4j.core.config.ConfigurationSource)12 PatternLayout (org.apache.logging.log4j.core.layout.PatternLayout)11 AbstractLogger (org.apache.logging.log4j.spi.AbstractLogger)11 InputStream (java.io.InputStream)10