diff --git a/server/src/main/java/org/elasticsearch/common/settings/AbstractScopedSettings.java b/server/src/main/java/org/elasticsearch/common/settings/AbstractScopedSettings.java index 9a329daf049..17dcba4f2fb 100644 --- a/server/src/main/java/org/elasticsearch/common/settings/AbstractScopedSettings.java +++ b/server/src/main/java/org/elasticsearch/common/settings/AbstractScopedSettings.java @@ -55,7 +55,7 @@ public abstract class AbstractScopedSettings { private static final Pattern GROUP_KEY_PATTERN = Pattern.compile("^(?:[-\\w]+[.])+$"); private static final Pattern AFFIX_KEY_PATTERN = Pattern.compile("^(?:[-\\w]+[.])+[*](?:[.][-\\w]+)+$"); - protected final Logger logger = LogManager.getLogger(this.getClass()); + private final Logger logger; private final Settings settings; private final List> settingUpdaters = new CopyOnWriteArrayList<>(); @@ -70,6 +70,7 @@ public abstract class AbstractScopedSettings { final Set> settingsSet, final Set> settingUpgraders, final Setting.Property scope) { + this.logger = LogManager.getLogger(this.getClass()); this.settings = settings; this.lastSettingsApplied = Settings.EMPTY; @@ -110,7 +111,8 @@ public abstract class AbstractScopedSettings { } } - protected AbstractScopedSettings(Settings nodeSettings, Settings scopeSettings, AbstractScopedSettings other) { + protected AbstractScopedSettings(Settings nodeSettings, Settings scopeSettings, AbstractScopedSettings other, Logger logger) { + this.logger = logger; this.settings = nodeSettings; this.lastSettingsApplied = scopeSettings; this.scope = other.scope; diff --git a/server/src/main/java/org/elasticsearch/common/settings/IndexScopedSettings.java b/server/src/main/java/org/elasticsearch/common/settings/IndexScopedSettings.java index 847a3e70349..7fd6a5245d6 100644 --- a/server/src/main/java/org/elasticsearch/common/settings/IndexScopedSettings.java +++ b/server/src/main/java/org/elasticsearch/common/settings/IndexScopedSettings.java @@ -24,6 +24,7 @@ import org.elasticsearch.cluster.routing.UnassignedInfo; import org.elasticsearch.cluster.routing.allocation.decider.EnableAllocationDecider; import org.elasticsearch.cluster.routing.allocation.decider.MaxRetryAllocationDecider; import org.elasticsearch.cluster.routing.allocation.decider.ShardsLimitAllocationDecider; +import org.elasticsearch.common.logging.Loggers; import org.elasticsearch.common.settings.Setting.Property; import org.elasticsearch.index.IndexModule; import org.elasticsearch.index.IndexSettings; @@ -190,7 +191,7 @@ public final class IndexScopedSettings extends AbstractScopedSettings { } private IndexScopedSettings(Settings settings, IndexScopedSettings other, IndexMetaData metaData) { - super(settings, metaData.getSettings(), other); + super(settings, metaData.getSettings(), other, Loggers.getLogger(IndexScopedSettings.class, metaData.getIndex())); } public IndexScopedSettings copy(Settings settings, IndexMetaData metaData) { diff --git a/server/src/test/java/org/elasticsearch/common/settings/SettingTests.java b/server/src/test/java/org/elasticsearch/common/settings/SettingTests.java index 86387f2104b..a4b52f9d1d2 100644 --- a/server/src/test/java/org/elasticsearch/common/settings/SettingTests.java +++ b/server/src/test/java/org/elasticsearch/common/settings/SettingTests.java @@ -18,14 +18,23 @@ */ package org.elasticsearch.common.settings; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.core.LogEvent; +import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.common.collect.Tuple; +import org.elasticsearch.common.logging.Loggers; import org.elasticsearch.common.settings.AbstractScopedSettings.SettingUpdater; import org.elasticsearch.common.settings.Setting.Property; import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.index.IndexSettings; import org.elasticsearch.monitor.jvm.JvmInfo; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.MockLogAppender; +import org.elasticsearch.test.junit.annotations.TestLogging; import java.util.Arrays; import java.util.Collections; @@ -40,6 +49,7 @@ import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; +import static org.elasticsearch.index.IndexSettingsTests.newIndexMeta; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasSize; @@ -1085,4 +1095,35 @@ public class SettingTests extends ESTestCase { assertThat(e.getMessage(), containsString("must be stored inside elasticsearch.yml")); } + @TestLogging(value="org.elasticsearch.common.settings.IndexScopedSettings:INFO", + reason="to ensure we log INFO-level messages from IndexScopedSettings") + public void testLogSettingUpdate() throws Exception { + final IndexMetaData metaData = newIndexMeta("index1", + Settings.builder().put(IndexSettings.INDEX_REFRESH_INTERVAL_SETTING.getKey(), "20s").build()); + final IndexSettings settings = new IndexSettings(metaData, Settings.EMPTY); + + final MockLogAppender mockLogAppender = new MockLogAppender(); + mockLogAppender.addExpectation(new MockLogAppender.SeenEventExpectation( + "message", + "org.elasticsearch.common.settings.IndexScopedSettings", + Level.INFO, + "updating [index.refresh_interval] from [20s] to [10s]") { + @Override + public boolean innerMatch(LogEvent event) { + return event.getMarker().getName().equals(" [index1]"); + } + }); + mockLogAppender.start(); + final Logger logger = LogManager.getLogger(IndexScopedSettings.class); + try { + Loggers.addAppender(logger, mockLogAppender); + settings.updateIndexMetaData(newIndexMeta("index1", + Settings.builder().put(IndexSettings.INDEX_REFRESH_INTERVAL_SETTING.getKey(), "10s").build())); + + mockLogAppender.assertAllExpectationsMatched(); + } finally { + Loggers.removeAppender(logger, mockLogAppender); + mockLogAppender.stop(); + } + } }