Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,48 @@
import org.apache.logging.log4j.Logger;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
@ConditionalOnProperty(value = "spring.cloud.config.watch.enabled")
public class Log4j2EventListener implements ApplicationListener<EnvironmentChangeEvent> {
public class Log4j2EventListener implements ApplicationListener<EnvironmentChangeEvent>, EnvironmentAware {
private static Logger LOGGER = LogManager.getLogger(Log4j2EventListener.class);
private Environment environment;

@Override
public void setEnvironment(final Environment environment) {
this.environment = environment;
}

@Override
public void onApplicationEvent(final EnvironmentChangeEvent environmentChangeEvent) {
if (!isWatchEnabled(environmentChangeEvent)) {
LOGGER.debug("Ignoring environment change event; spring.cloud.config.watch.enabled is false");
return;
}
LOGGER.debug("Application change event triggered");
WatchEventManager.publishEvent();
}

/**
* {@code spring.factories} constructs this listener outside the bean factory, so
* {@code @ConditionalOnProperty} never applies. Honor the same property here.
*/
private boolean isWatchEnabled(final EnvironmentChangeEvent event) {
Environment env = this.environment;
if (env == null) {
final Object source = event.getSource();
if (source instanceof ApplicationContext) {
env = ((ApplicationContext) source).getEnvironment();
}
}
if (env == null) {
return true;
}
return !Boolean.FALSE.equals(env.getProperty("spring.cloud.config.watch.enabled", Boolean.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.logging.log4j.spring.cloud.config.client;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.File;
Expand All @@ -35,6 +36,8 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.mock.env.MockEnvironment;
import org.springframework.test.context.junit4.SpringRunner;

/**
Expand Down Expand Up @@ -69,6 +72,26 @@ public void test() {
assertTrue(count.get() > 0);
}

@Test
public void doesNotReloadWhenWatchDisabled() {
final MockEnvironment environment = new MockEnvironment();
environment.setProperty("spring.cloud.config.watch.enabled", "false");
try (GenericApplicationContext context = new GenericApplicationContext()) {
context.setEnvironment(environment);
context.refresh();
final AtomicInteger count = new AtomicInteger(0);
final Source source = new Source(new File("test.java"));
loggerContextRule
.getLoggerContext()
.getConfiguration()
.getWatchManager()
.watch(source, new TestWatcher(count));
new Log4j2EventListener()
.onApplicationEvent(new EnvironmentChangeEvent(context, new HashSet<>()));
assertEquals(0, count.get());
}
}

private static class TestWatcher implements Watcher {

private final AtomicInteger count;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="https://logging.apache.org/xml/ns"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
https://logging.apache.org/xml/ns
https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
type="fixed">
<issue id="4244" link="https://github.com/apache/logging-log4j2/issues/4244"/>
<description format="asciidoc">
Honor `spring.cloud.config.watch.enabled` in `Log4j2EventListener` when it is created from `spring.factories`.
</description>
</entry>