diff --git a/server/src/main/java/org/elasticsearch/common/logging/DeprecationLogger.java b/server/src/main/java/org/elasticsearch/common/logging/DeprecationLogger.java index b9e4e197895a1..fddaa572edb09 100644 --- a/server/src/main/java/org/elasticsearch/common/logging/DeprecationLogger.java +++ b/server/src/main/java/org/elasticsearch/common/logging/DeprecationLogger.java @@ -11,6 +11,11 @@ import org.apache.logging.log4j.Level; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.elasticsearch.common.regex.Regex; +import org.elasticsearch.common.settings.Settings; + +import java.util.Collections; +import java.util.List; /** * A logger that logs deprecation notices. Logger should be initialized with a class or name which will be used @@ -32,7 +37,7 @@ public class DeprecationLogger { * More serious than WARN by 1, but less serious than ERROR */ public static Level CRITICAL = Level.forName("CRITICAL", Level.WARN.intLevel() - 1); - + private static volatile List skipTheseDeprecations = Collections.emptyList(); private final Logger logger; /** @@ -53,6 +58,19 @@ public static DeprecationLogger getLogger(String name) { return new DeprecationLogger(name); } + /** + * The DeprecationLogger uses the "deprecation.skip_deprecated_settings" setting to decide whether to log a deprecation for a setting. + * This is a node setting. This method initializes the DeprecationLogger class with the node settings for the node in order to read the + * "deprecation.skip_deprecated_settings" setting. This only needs to be called once per JVM. If it is not called, the default behavior + * is to assume that the "deprecation.skip_deprecated_settings" setting is not set. + * @param nodeSettings The settings for this node + */ + public static void initialize(Settings nodeSettings) { + skipTheseDeprecations = nodeSettings == null + ? Collections.emptyList() + : nodeSettings.getAsList("deprecation.skip_deprecated_settings"); + } + private DeprecationLogger(String parentLoggerName) { this.logger = LogManager.getLogger(getLoggerName(parentLoggerName)); } @@ -92,10 +110,12 @@ public DeprecationLogger warn(final DeprecationCategory category, final String k } private DeprecationLogger logDeprecation(Level level, DeprecationCategory category, String key, String msg, Object[] params) { - String opaqueId = HeaderWarning.getXOpaqueId(); - String productOrigin = HeaderWarning.getProductOrigin(); - ESLogMessage deprecationMessage = new DeprecatedMessage(category, key, opaqueId, productOrigin, msg, params); - logger.log(level, deprecationMessage); + if (Regex.simpleMatch(skipTheseDeprecations, key) == false) { + String opaqueId = HeaderWarning.getXOpaqueId(); + String productOrigin = HeaderWarning.getProductOrigin(); + ESLogMessage deprecationMessage = new DeprecatedMessage(category, key, opaqueId, productOrigin, msg, params); + logger.log(level, deprecationMessage); + } return this; } diff --git a/server/src/main/java/org/elasticsearch/common/settings/Setting.java b/server/src/main/java/org/elasticsearch/common/settings/Setting.java index c21e9c536a8d1..68e0d02c0f2bf 100644 --- a/server/src/main/java/org/elasticsearch/common/settings/Setting.java +++ b/server/src/main/java/org/elasticsearch/common/settings/Setting.java @@ -588,15 +588,12 @@ void checkDeprecation(Settings settings) { // It would be convenient to show its replacement key, but replacement is often not so simple final String key = getKey(); DeprecationCategory category = this.isSecure(settings) ? DeprecationCategory.SECURITY : DeprecationCategory.SETTINGS; - List skipTheseDeprecations = settings.getAsList("deprecation.skip_deprecated_settings"); - if (Regex.simpleMatch(skipTheseDeprecations, key) == false) { - String message = "[{}] setting was deprecated in Elasticsearch and will be removed in a future release! " - + "See the breaking changes documentation for the next major version."; - if (this.isDeprecatedWarningOnly()) { - Settings.DeprecationLoggerHolder.deprecationLogger.warn(category, key, message, key); - } else { - Settings.DeprecationLoggerHolder.deprecationLogger.critical(category, key, message, key); - } + String message = "[{}] setting was deprecated in Elasticsearch and will be removed in a future release! " + + "See the breaking changes documentation for the next major version."; + if (this.isDeprecatedWarningOnly()) { + Settings.DeprecationLoggerHolder.deprecationLogger.warn(category, key, message, key); + } else { + Settings.DeprecationLoggerHolder.deprecationLogger.critical(category, key, message, key); } } } diff --git a/server/src/main/java/org/elasticsearch/node/Node.java b/server/src/main/java/org/elasticsearch/node/Node.java index 17a70da21f76f..5c5ffbc237c9e 100644 --- a/server/src/main/java/org/elasticsearch/node/Node.java +++ b/server/src/main/java/org/elasticsearch/node/Node.java @@ -325,6 +325,8 @@ protected Node( final List resourcesToClose = new ArrayList<>(); // register everything we need to release in the case of an error boolean success = false; try { + // Pass the node settings to the DeprecationLogger class so that it can have the deprecation.skip_deprecated_settings setting: + DeprecationLogger.initialize(initialEnvironment.settings()); Settings tmpSettings = Settings.builder() .put(initialEnvironment.settings()) .put(Client.CLIENT_TYPE_SETTING_S.getKey(), CLIENT_TYPE) 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 0d2b8452d3976..6f69991a668af 100644 --- a/server/src/test/java/org/elasticsearch/common/settings/SettingTests.java +++ b/server/src/test/java/org/elasticsearch/common/settings/SettingTests.java @@ -12,6 +12,7 @@ import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.core.LogEvent; import org.elasticsearch.cluster.metadata.IndexMetadata; +import org.elasticsearch.common.logging.DeprecationLogger; import org.elasticsearch.common.logging.Loggers; import org.elasticsearch.common.settings.AbstractScopedSettings.SettingUpdater; import org.elasticsearch.common.settings.Setting.Property; @@ -1385,7 +1386,7 @@ public void testCheckForDeprecation() { } public void testCheckForDeprecationWithSkipSetting() { - final String settingName = "foo.bar"; + final String settingName = "foo.bar.hide.this"; final String settingValue = "blat"; final Setting setting = Setting.simpleString(settingName, settingValue); final Settings settings = Settings.builder().put(settingName, settingValue).build(); @@ -1398,6 +1399,7 @@ public void testCheckForDeprecationWithSkipSetting() { .put(settingName, settingValue) .putList("deprecation.skip_deprecated_settings", settingName) .build(); + DeprecationLogger.initialize(settingsWithSkipDeprecationSetting); deprecatedSetting.checkDeprecation(settingsWithSkipDeprecationSetting); ensureNoWarnings(); }