Search in sources :

Example 1 with Source

use of org.apache.logging.log4j.core.util.Source in project logging-log4j2 by apache.

the class AbstractConfiguration method monitorSource.

private void monitorSource(final Reconfigurable reconfigurable, final ConfigurationSource configSource) {
    if (configSource.getLastModified() > 0) {
        final Source cfgSource = new Source(configSource);
        final Watcher watcher = WatcherFactory.getInstance(pluginPackages).newWatcher(cfgSource, this, reconfigurable, listeners, configSource.getLastModified());
        if (watcher != null) {
            watchManager.watch(cfgSource, watcher);
        }
    } else {
        LOGGER.info("{} does not support dynamic reconfiguration", configSource.getURI());
    }
}
Also used : Watcher(org.apache.logging.log4j.core.util.Watcher) Source(org.apache.logging.log4j.core.util.Source)

Example 2 with Source

use of org.apache.logging.log4j.core.util.Source in project logging-log4j2 by apache.

the class Log4j2EventListenerTest method test.

@Test
public void test() throws Exception {
    AtomicInteger count = new AtomicInteger(0);
    Source source = new Source(new File("test.java"));
    loggerContextRule.getLoggerContext().getConfiguration().getWatchManager().watch(source, new TestWatcher(count));
    publisher.publishEvent(new EnvironmentChangeEvent(new HashSet<>()));
    assertTrue(count.get() > 0);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EnvironmentChangeEvent(org.springframework.cloud.context.environment.EnvironmentChangeEvent) File(java.io.File) Source(org.apache.logging.log4j.core.util.Source) HashSet(java.util.HashSet) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with Source

use of org.apache.logging.log4j.core.util.Source in project logging-log4j2 by apache.

the class AbstractConfiguration method initializeWatchers.

protected void initializeWatchers(final Reconfigurable reconfigurable, final ConfigurationSource configSource, final int monitorIntervalSeconds) {
    if (configSource != null && (configSource.getFile() != null || configSource.getURL() != null)) {
        if (monitorIntervalSeconds > 0) {
            watchManager.setIntervalSeconds(monitorIntervalSeconds);
            if (configSource.getFile() != null) {
                final Source cfgSource = new Source(configSource);
                final long lastModifeid = configSource.getFile().lastModified();
                final ConfigurationFileWatcher watcher = new ConfigurationFileWatcher(this, reconfigurable, listeners, lastModifeid);
                watchManager.watch(cfgSource, watcher);
            } else {
                if (configSource.getURL() != null) {
                    monitorSource(reconfigurable, configSource);
                }
            }
        } else if (watchManager.hasEventListeners() && configSource.getURL() != null && monitorIntervalSeconds >= 0) {
            monitorSource(reconfigurable, configSource);
        }
    }
}
Also used : Source(org.apache.logging.log4j.core.util.Source)

Example 4 with Source

use of org.apache.logging.log4j.core.util.Source in project logging-log4j2 by apache.

the class CompositeConfiguration method setup.

@Override
public void setup() {
    final AbstractConfiguration targetConfiguration = configurations.get(0);
    staffChildConfiguration(targetConfiguration);
    final WatchManager watchManager = getWatchManager();
    final WatchManager targetWatchManager = targetConfiguration.getWatchManager();
    if (targetWatchManager.getIntervalSeconds() > 0) {
        watchManager.setIntervalSeconds(targetWatchManager.getIntervalSeconds());
        final Map<Source, Watcher> watchers = targetWatchManager.getConfigurationWatchers();
        for (final Map.Entry<Source, Watcher> entry : watchers.entrySet()) {
            watchManager.watch(entry.getKey(), entry.getValue().newWatcher(this, listeners, entry.getValue().getLastModified()));
        }
    }
    for (final AbstractConfiguration sourceConfiguration : configurations.subList(1, configurations.size())) {
        staffChildConfiguration(sourceConfiguration);
        final Node sourceRoot = sourceConfiguration.getRootNode();
        mergeStrategy.mergeConfigurations(rootNode, sourceRoot, getPluginManager());
        if (LOGGER.isEnabled(Level.ALL)) {
            final StringBuilder sb = new StringBuilder();
            printNodes("", rootNode, sb);
            System.out.println(sb.toString());
        }
        final int monitorInterval = sourceConfiguration.getWatchManager().getIntervalSeconds();
        if (monitorInterval > 0) {
            final int currentInterval = watchManager.getIntervalSeconds();
            if (currentInterval <= 0 || monitorInterval < currentInterval) {
                watchManager.setIntervalSeconds(monitorInterval);
            }
            final WatchManager sourceWatchManager = sourceConfiguration.getWatchManager();
            final Map<Source, Watcher> watchers = sourceWatchManager.getConfigurationWatchers();
            for (final Map.Entry<Source, Watcher> entry : watchers.entrySet()) {
                watchManager.watch(entry.getKey(), entry.getValue().newWatcher(this, listeners, entry.getValue().getLastModified()));
            }
        }
    }
}
Also used : AbstractConfiguration(org.apache.logging.log4j.core.config.AbstractConfiguration) Node(org.apache.logging.log4j.plugins.Node) Watcher(org.apache.logging.log4j.core.util.Watcher) Map(java.util.Map) WatchManager(org.apache.logging.log4j.core.util.WatchManager) Source(org.apache.logging.log4j.core.util.Source) ConfigurationSource(org.apache.logging.log4j.core.config.ConfigurationSource)

Aggregations

Source (org.apache.logging.log4j.core.util.Source)4 Watcher (org.apache.logging.log4j.core.util.Watcher)2 File (java.io.File)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AbstractConfiguration (org.apache.logging.log4j.core.config.AbstractConfiguration)1 ConfigurationSource (org.apache.logging.log4j.core.config.ConfigurationSource)1 WatchManager (org.apache.logging.log4j.core.util.WatchManager)1 Node (org.apache.logging.log4j.plugins.Node)1 Test (org.junit.Test)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 EnvironmentChangeEvent (org.springframework.cloud.context.environment.EnvironmentChangeEvent)1