org.apache.logging.log4j.core.config.xml.XmlConfiguration Java Examples

The following examples show how to use org.apache.logging.log4j.core.config.xml.XmlConfiguration. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AdvertiserTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@BeforeClass
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();
    }
}
 
Example #2
Source File: SOFAConfigurationFactory.java    From sofa-common-tools with Apache License 2.0 5 votes vote down vote up
@Override
public Configuration getConfiguration(LoggerContext loggerContext, ConfigurationSource source) {
    if (source != null && source != ConfigurationSource.NULL_SOURCE) {
        return loggerContext.getExternalContext() != null ? new SOFAConfiguration()
            : new XmlConfiguration(loggerContext, source);
    }
    return null;
}
 
Example #3
Source File: IbisLoggerConfigurationFactory.java    From iaf with Apache License 2.0 5 votes vote down vote up
@Override
public Configuration getConfiguration(LoggerContext loggerContext, ConfigurationSource source) {
	try {
		setLogDir();
		setLevel();

		String configuration = readLog4jConfiguration(source.getInputStream());
		Properties properties = getProperties();
		Matcher m = Pattern.compile("\\$\\{(?:ctx:)?([^}]*)\\}").matcher(configuration); //Look for properties in the Log4J2 XML
		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);
			}
		}
		ThreadContext.putAll(substitutions); //Only add the substituted variables to the ThreadContext

		//We have to 'reset' the source as the old stream has been read.
		return new XmlConfiguration(loggerContext, source.resetInputStream());
	} catch (IOException e) {
		System.err.println(LOG_PREFIX + "unable to configure Log4J2");
		throw new IllegalStateException(LOG_PREFIX + "unable to configure Log4J2", e);
	}
}
 
Example #4
Source File: LateConfigTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testReconfiguration() throws Exception {
    final Configuration cfg = context.getConfiguration();
    assertNotNull("No configuration", cfg);
    assertTrue("Not set to default configuration", cfg instanceof DefaultConfiguration);
    final File file = new File(CONFIG);
    final LoggerContext loggerContext = LoggerContext.getContext(null, false, file.toURI());
    assertNotNull("No Logger Context", loggerContext);
    final Configuration newConfig = loggerContext.getConfiguration();
    assertTrue("Configuration not reset", cfg != newConfig);
    assertTrue("Reconfiguration failed", newConfig instanceof XmlConfiguration);
    context = LoggerContext.getContext(false);
    final Configuration sameConfig = context.getConfiguration();
    assertTrue("Configuration should not have been reset", newConfig == sameConfig);
}
 
Example #5
Source File: NestedLoggerConfigTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private Configuration loadConfiguration(String resourcePath) throws IOException {
    InputStream in = getClass().getClassLoader().getResourceAsStream(resourcePath);
    try {
        Configuration configuration = new XmlConfiguration(new LoggerContext("test"), new ConfigurationSource(in));
        configuration.initialize();
        configuration.start();
        return configuration;
    } finally {
        in.close();
    }
}
 
Example #6
Source File: CustomConfigurationTest.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Test
public void testConfig() {
    // don't bother using "error" since that's the default; try another level
    final LoggerContext ctx = this.init.getLoggerContext();
    ctx.reconfigure();
    final Configuration config = ctx.getConfiguration();
    assertThat(config, instanceOf(XmlConfiguration.class));
    for (final StatusListener listener : StatusLogger.getLogger().getListeners()) {
        if (listener instanceof StatusConsoleListener) {
            assertSame(listener.getStatusLevel(), Level.INFO);
            break;
        }
    }
    final Layout<? extends Serializable> layout = PatternLayout.newBuilder()
        .setPattern(PatternLayout.SIMPLE_CONVERSION_PATTERN)
        .setConfiguration(config)
        .build();
    // @formatter:off
    final FileAppender appender = FileAppender.newBuilder()
        .setFileName(LOG_FILE)
        .setAppend(false)
        .setName("File")
        .setIgnoreExceptions(false)
        .setBufferSize(4000)
        .setBufferedIo(false)
        .setLayout(layout)
        .build();
    // @formatter:on
    appender.start();
    config.addAppender(appender);
    final AppenderRef ref = AppenderRef.createAppenderRef("File", null, null);
    final AppenderRef[] refs = new AppenderRef[] {ref};

    final LoggerConfig loggerConfig = LoggerConfig.createLogger(false, Level.INFO, "org.apache.logging.log4j",
        "true", refs, null, config, null );
    loggerConfig.addAppender(appender, null, null);
    config.addLogger("org.apache.logging.log4j", loggerConfig);
    ctx.updateLoggers();
    final Logger logger = ctx.getLogger(CustomConfigurationTest.class.getName());
    logger.info("This is a test");
    final File file = new File(LOG_FILE);
    assertThat(file, exists());
    assertThat(file, hasLength(greaterThan(0L)));
}
 
Example #7
Source File: Log4jOverridableConfigurer.java    From herd with Apache License 2.0 3 votes vote down vote up
/**
 * Gets the refresh interval in seconds from the Log4J XML configuration.
 *
 * @param xmlConfigurationString the XML configuration.
 *
 * @return the refresh interval in seconds.
 * @throws IOException if there were any errors parsing the configuration file.
 */
private int getRefreshIntervalSeconds(String xmlConfigurationString) throws IOException
{
    // Create the Log4J configuration object from the configuration string and get the refresh interval in seconds from the configuration.
    XmlConfiguration xmlConfiguration =
        new XmlConfiguration(loggerContext, new ConfigurationSource(new ByteArrayInputStream(xmlConfigurationString.getBytes(StandardCharsets.UTF_8))));
    return xmlConfiguration.getWatchManager().getIntervalSeconds();
}