Search in sources :

Example 1 with XmlConfiguration

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

the class AdvertiserTest method setupClass.

@BeforeAll
public static void setupClass() {
    final File file = new File(STATUS_LOG);
    file.delete();
    System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG);
    final LoggerContext ctx = LoggerContext.getContext();
    final Configuration config = ctx.getConfiguration();
    if (config instanceof XmlConfiguration) {
        final String name = config.getName();
        if (name == null || !name.equals("XMLConfigTest")) {
            ctx.reconfigure();
        }
    } else {
        ctx.reconfigure();
    }
}
Also used : XmlConfiguration(org.apache.logging.log4j.core.config.xml.XmlConfiguration) XmlConfiguration(org.apache.logging.log4j.core.config.xml.XmlConfiguration) File(java.io.File) LoggerContext(org.apache.logging.log4j.core.LoggerContext) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 2 with XmlConfiguration

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

the class LateConfigTest method testReconfiguration.

@Test
public void testReconfiguration() throws Exception {
    final Configuration cfg = context.getConfiguration();
    assertNotNull(cfg, "No configuration");
    assertTrue(cfg instanceof DefaultConfiguration, "Not set to default configuration");
    final File file = new File(CONFIG);
    final LoggerContext loggerContext = LoggerContext.getContext(null, false, file.toURI());
    assertNotNull(loggerContext, "No Logger Context");
    final Configuration newConfig = loggerContext.getConfiguration();
    assertNotSame(cfg, newConfig, "Configuration not reset");
    assertTrue(newConfig instanceof XmlConfiguration, "Reconfiguration failed");
    context = LoggerContext.getContext(false);
    final Configuration sameConfig = context.getConfiguration();
    assertSame(newConfig, sameConfig, "Configuration should not have been reset");
}
Also used : DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) XmlConfiguration(org.apache.logging.log4j.core.config.xml.XmlConfiguration) Configuration(org.apache.logging.log4j.core.config.Configuration) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) XmlConfiguration(org.apache.logging.log4j.core.config.xml.XmlConfiguration) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 3 with XmlConfiguration

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

the class NestedLoggerConfigTest method loadConfiguration.

private Configuration loadConfiguration(String resourcePath) throws IOException {
    try (InputStream in = getClass().getClassLoader().getResourceAsStream(resourcePath)) {
        Configuration configuration = new XmlConfiguration(new LoggerContext("test"), new ConfigurationSource(in));
        configuration.initialize();
        configuration.start();
        return configuration;
    }
}
Also used : XmlConfiguration(org.apache.logging.log4j.core.config.xml.XmlConfiguration) InputStream(java.io.InputStream) XmlConfiguration(org.apache.logging.log4j.core.config.xml.XmlConfiguration) LoggerContext(org.apache.logging.log4j.core.LoggerContext)

Example 4 with XmlConfiguration

use of org.apache.logging.log4j.core.config.xml.XmlConfiguration in project iaf by ibissource.

the class IbisLoggerConfigurationFactory method getConfiguration.

@Override
public Configuration getConfiguration(LoggerContext loggerContext, ConfigurationSource source) {
    try {
        setLogDir();
        setLevel();
        String configuration = readLog4jConfiguration(source.getInputStream());
        Properties properties = getProperties();
        // Look for properties in the Log4J2 XML
        Matcher m = Pattern.compile("\\$\\{(?:ctx:)?([^}]*)\\}").matcher(configuration);
        Map<String, String> substitutions = new HashMap<>();
        while (m.find()) {
            String key = m.group(1);
            String value = resolveValueRecursively(properties, key);
            if (value != null) {
                substitutions.put(key, value);
            }
        }
        // Only add the substituted variables to the ThreadContext
        ThreadContext.putAll(substitutions);
        return new // We have to 'reset' the source as the old stream has been read.
        XmlConfiguration(// We have to 'reset' the source as the old stream has been read.
        loggerContext, // We have to 'reset' the source as the old stream has been read.
        source.resetInputStream()) {

            // Add hashcode to toString() so we can differentiate the XmlConfigurations in the startup log
            @Override
            public String toString() {
                return this.getClass().getCanonicalName() + "@" + Integer.toHexString(this.hashCode()) + "[location=" + getConfigurationSource() + "]";
            }
        };
    } catch (IOException e) {
        System.err.println(LOG_PREFIX + "unable to configure Log4J2");
        throw new IllegalStateException(LOG_PREFIX + "unable to configure Log4J2", e);
    }
}
Also used : Matcher(java.util.regex.Matcher) HashMap(java.util.HashMap) XmlConfiguration(org.apache.logging.log4j.core.config.xml.XmlConfiguration) IOException(java.io.IOException) Properties(java.util.Properties)

Aggregations

XmlConfiguration (org.apache.logging.log4j.core.config.xml.XmlConfiguration)4 File (java.io.File)2 LoggerContext (org.apache.logging.log4j.core.LoggerContext)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 Properties (java.util.Properties)1 Matcher (java.util.regex.Matcher)1 Configuration (org.apache.logging.log4j.core.config.Configuration)1 DefaultConfiguration (org.apache.logging.log4j.core.config.DefaultConfiguration)1 BeforeAll (org.junit.jupiter.api.BeforeAll)1 Test (org.junit.jupiter.api.Test)1