diff --git a/implementation/src/main/java/io/smallrye/config/ConfigValueConfigSource.java b/implementation/src/main/java/io/smallrye/config/ConfigValueConfigSource.java index b0b1c7636..2bc2501d8 100644 --- a/implementation/src/main/java/io/smallrye/config/ConfigValueConfigSource.java +++ b/implementation/src/main/java/io/smallrye/config/ConfigValueConfigSource.java @@ -3,6 +3,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.Reader; +import java.lang.invoke.MethodHandles; import java.util.AbstractCollection; import java.util.AbstractMap; import java.util.AbstractSet; @@ -14,7 +15,9 @@ import java.util.Set; import org.eclipse.microprofile.config.spi.ConfigSource; +import org.jboss.logging.Logger; +import io.smallrye.config._private.ConfigLogging; import io.smallrye.config._private.ConfigMessages; /** @@ -340,6 +343,9 @@ final class ConfigValueProperties extends HashMap { private final String configSourceName; private final int configSourceOrdinal; + private static ConfigLogging log = Logger.getMessageLogger(MethodHandles.lookup(), ConfigLogging.class, + "io.smallrye.config"); + public ConfigValueProperties(final String configSourceName, final int configSourceOrdinal) { this.configSourceName = configSourceName; this.configSourceOrdinal = configSourceOrdinal; @@ -401,7 +407,7 @@ private void load0(LineReader lr) throws IOException { } String key = loadConvert(lr.lineBuf, 0, keyLen, convtBuf); String value = loadConvert(lr.lineBuf, valueStart, limit - valueStart, convtBuf); - put(key, ConfigValue.builder() + ConfigValue oldConfigValue = put(key, ConfigValue.builder() .withName(key) .withValue(value) .withRawValue(value) @@ -409,6 +415,10 @@ private void load0(LineReader lr) throws IOException { .withConfigSourceOrdinal(configSourceOrdinal) .withLineNumber(lr.lineNumber) .build()); + if (oldConfigValue != null) { + ConfigLogging.log.duplicateValue(oldConfigValue.getName(), oldConfigValue.getConfigSourceName(), + oldConfigValue.getValue()); + } } } diff --git a/implementation/src/main/java/io/smallrye/config/_private/ConfigLogging.java b/implementation/src/main/java/io/smallrye/config/_private/ConfigLogging.java index aab962000..4feebaeb3 100644 --- a/implementation/src/main/java/io/smallrye/config/_private/ConfigLogging.java +++ b/implementation/src/main/java/io/smallrye/config/_private/ConfigLogging.java @@ -40,4 +40,9 @@ public interface ConfigLogging extends BasicLogger { @LogMessage(level = Logger.Level.DEBUG) @Message(id = 1006, value = "Loaded ConfigSource %s with ordinal %d") void loadedConfigSource(String name, int ordinal); + + @LogMessage(level = Logger.Level.WARN) + @Message(id = 1007, value = "Duplicate value found for name : %s, config source name : %s, (old value : %s)") + void duplicateValue(String key, String sourceName, String oldValue); + } diff --git a/implementation/src/test/java/io/smallrye/config/ConfigValuePropertiesTest.java b/implementation/src/test/java/io/smallrye/config/ConfigValuePropertiesTest.java index cc38375ba..a518eaee6 100644 --- a/implementation/src/test/java/io/smallrye/config/ConfigValuePropertiesTest.java +++ b/implementation/src/test/java/io/smallrye/config/ConfigValuePropertiesTest.java @@ -79,4 +79,17 @@ void wrapValue() throws Exception { assertEquals(2, map.get("key2").getLineNumber()); assertEquals(6, map.get("key3").getLineNumber()); } + + @Test + void logDuplicateValue() throws Exception { + ConfigValueProperties map = new ConfigValueProperties("config", 1); + String config = "key=value\n" + + "key2=value\n" + + "key2=value2\n"; + map.load(new StringReader(config)); + + // property expected with the last value found + assertEquals("value2", map.get("key2").getValue()); + } + }