Search in sources :

Example 91 with Configuration

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

the class PropertiesConfigurationTest method testPropertiesConfiguration.

@Test
public void testPropertiesConfiguration() {
    final Configuration config = context.getConfiguration();
    assertNotNull("No configuration created", config);
    assertEquals("Incorrect State: " + config.getState(), config.getState(), LifeCycle.State.STARTED);
    final Map<String, Appender> appenders = config.getAppenders();
    assertNotNull(appenders);
    assertTrue("Incorrect number of Appenders: " + appenders.size(), appenders.size() == 1);
    final Map<String, LoggerConfig> loggers = config.getLoggers();
    assertNotNull(loggers);
    assertTrue("Incorrect number of LoggerConfigs: " + loggers.size(), loggers.size() == 2);
    final Filter filter = config.getFilter();
    assertNotNull("No Filter", filter);
    assertTrue("Not a Threshold Filter", filter instanceof ThresholdFilter);
    final Logger logger = LogManager.getLogger(getClass());
    logger.info("Welcome to Log4j!");
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ThresholdFilter(org.apache.logging.log4j.core.filter.ThresholdFilter) Configuration(org.apache.logging.log4j.core.config.Configuration) 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.Test)

Example 92 with Configuration

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

the class RollingFilePropertiesTest method testPropertiesConfiguration.

@Test
public void testPropertiesConfiguration() {
    final Configuration config = context.getConfiguration();
    assertNotNull("No configuration created", config);
    assertEquals("Incorrect State: " + config.getState(), config.getState(), LifeCycle.State.STARTED);
    final Map<String, Appender> appenders = config.getAppenders();
    assertNotNull(appenders);
    assertTrue("Incorrect number of Appenders: " + appenders.size(), appenders.size() == 3);
    final Map<String, LoggerConfig> loggers = config.getLoggers();
    assertNotNull(loggers);
    assertTrue("Incorrect number of LoggerConfigs: " + loggers.size(), loggers.size() == 2);
    final Filter filter = config.getFilter();
    assertNotNull("No Filter", filter);
    assertTrue("Not a Threshold Filter", filter instanceof ThresholdFilter);
    final Logger logger = LogManager.getLogger(getClass());
    logger.info("Welcome to Log4j!");
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ThresholdFilter(org.apache.logging.log4j.core.filter.ThresholdFilter) Configuration(org.apache.logging.log4j.core.config.Configuration) 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.Test)

Example 93 with Configuration

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

the class XmlConfigurationPropsTest method testWithConfigProp.

@Test
public void testWithConfigProp() {
    System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG);
    System.setProperty("log4j.level", "warn");
    try {
        final LoggerContext ctx = LoggerContext.getContext();
        ctx.reconfigure();
        final Configuration config = ctx.getConfiguration();
        assertTrue("Configuration is not an XmlConfiguration", config instanceof XmlConfiguration);
    } finally {
        System.clearProperty("log4j.level");
    }
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) LoggerContext(org.apache.logging.log4j.core.LoggerContext) Test(org.junit.Test)

Example 94 with Configuration

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

the class XmlConfigurationPropsTest method testWithProps.

@Test
public void testWithProps() {
    System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG);
    System.setProperty("log4j.level", "warn");
    System.setProperty("log.level", "warn");
    try {
        final LoggerContext ctx = LoggerContext.getContext();
        ctx.reconfigure();
        final Configuration config = ctx.getConfiguration();
        assertTrue("Configuration is not an XmlConfiguration", config instanceof XmlConfiguration);
    } finally {
        System.clearProperty("log4j.level");
        System.clearProperty("log.level");
    }
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) LoggerContext(org.apache.logging.log4j.core.LoggerContext) Test(org.junit.Test)

Example 95 with Configuration

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

the class LoggerContextAdmin method setConfigText.

@Override
public void setConfigText(final String configText, final String charsetName) {
    LOGGER.debug("---------");
    LOGGER.debug("Remote request to reconfigure from config text.");
    try {
        final InputStream in = new ByteArrayInputStream(configText.getBytes(charsetName));
        final ConfigurationSource source = new ConfigurationSource(in);
        final Configuration updated = ConfigurationFactory.getInstance().getConfiguration(loggerContext, source);
        loggerContext.start(updated);
        LOGGER.debug("Completed remote request to reconfigure from config text.");
    } catch (final Exception ex) {
        final String msg = "Could not reconfigure from config text";
        LOGGER.error(msg, ex);
        throw new IllegalArgumentException(msg, ex);
    }
}
Also used : ConfigurationSource(org.apache.logging.log4j.core.config.ConfigurationSource) Configuration(org.apache.logging.log4j.core.config.Configuration) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException)

Aggregations

Configuration (org.apache.logging.log4j.core.config.Configuration)105 LoggerContext (org.apache.logging.log4j.core.LoggerContext)54 Test (org.junit.Test)43 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)39 Appender (org.apache.logging.log4j.core.Appender)21 DefaultConfiguration (org.apache.logging.log4j.core.config.DefaultConfiguration)15 File (java.io.File)12 FileAppender (org.apache.logging.log4j.core.appender.FileAppender)9 RollingFileAppender (org.apache.logging.log4j.core.appender.RollingFileAppender)9 Path (java.nio.file.Path)7 Level (org.apache.logging.log4j.Level)7 NullConfiguration (org.apache.logging.log4j.core.config.NullConfiguration)7 ArrayList (java.util.ArrayList)6 Logger (org.apache.logging.log4j.Logger)6 Filter (org.apache.logging.log4j.core.Filter)6 AbstractLogger (org.apache.logging.log4j.spi.AbstractLogger)6 PatternLayout (org.apache.logging.log4j.core.layout.PatternLayout)5 ListAppender (org.apache.logging.log4j.test.appender.ListAppender)5 ConsoleAppender (org.apache.logging.log4j.core.appender.ConsoleAppender)4 AppenderRef (org.apache.logging.log4j.core.config.AppenderRef)4