Merge pull request #13934 from brwe/log-config-order

settings in log config file should not overwrite custom parameters
This commit is contained in:
Britta Weber 2015-10-05 13:49:55 +02:00
commit 6e29facd0a
2 changed files with 37 additions and 7 deletions

View File

@ -90,12 +90,14 @@ public class LogConfigurator {
loaded = true; loaded = true;
// TODO: this is partly a copy of InternalSettingsPreparer...we should pass in Environment and not do all this... // TODO: this is partly a copy of InternalSettingsPreparer...we should pass in Environment and not do all this...
Environment environment = new Environment(settings); Environment environment = new Environment(settings);
Settings.Builder settingsBuilder = settingsBuilder().put(settings); Settings.Builder settingsBuilder = settingsBuilder();
resolveConfig(environment, settingsBuilder); resolveConfig(environment, settingsBuilder);
settingsBuilder settingsBuilder
.putProperties("elasticsearch.", System.getProperties()) .putProperties("elasticsearch.", System.getProperties())
.putProperties("es.", System.getProperties()) .putProperties("es.", System.getProperties());
.replacePropertyPlaceholders(); // add custom settings after config was added so that they are not overwritten by config
settingsBuilder.put(settings);
settingsBuilder.replacePropertyPlaceholders();
Properties props = new Properties(); Properties props = new Properties();
for (Map.Entry<String, String> entry : settingsBuilder.build().getAsMap().entrySet()) { for (Map.Entry<String, String> entry : settingsBuilder.build().getAsMap().entrySet()) {
String key = "log4j." + entry.getKey(); String key = "log4j." + entry.getKey();

View File

@ -21,20 +21,21 @@ package org.elasticsearch.common.logging.log4j;
import org.apache.log4j.Appender; import org.apache.log4j.Appender;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.elasticsearch.common.cli.CliToolTestCase;
import org.elasticsearch.common.logging.ESLogger; import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.env.Environment; import org.elasticsearch.env.Environment;
import org.elasticsearch.node.internal.InternalSettingsPreparer;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.ESTestCase;
import org.hamcrest.Matchers;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.notNullValue;
/** /**
* *
@ -148,7 +149,34 @@ public class LoggingConfigurationTests extends ESTestCase {
LogConfigurator.resolveConfig(environment, builder); LogConfigurator.resolveConfig(environment, builder);
Settings logSettings = builder.build(); Settings logSettings = builder.build();
assertThat(logSettings.get("yml"), Matchers.nullValue()); assertThat(logSettings.get("yml"), nullValue());
}
// tests that custom settings are not overwritten by settings in the config file
@Test
public void testResolveOrder() throws Exception {
Path tmpDir = createTempDir();
Path loggingConf = tmpDir.resolve(loggingConfiguration("yaml"));
Files.write(loggingConf, "logger.test: INFO, file\n".getBytes(StandardCharsets.UTF_8));
Files.write(loggingConf, "appender.file.type: file\n".getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
Environment environment = InternalSettingsPreparer.prepareEnvironment(
Settings.builder()
.put("path.conf", tmpDir.toAbsolutePath())
.put("path.home", createTempDir().toString())
.put("logger.test", "TRACE, console")
.put("appender.console.type", "console")
.put("appender.console.layout.type", "consolePattern")
.put("appender.console.layout.conversionPattern", "[%d{ISO8601}][%-5p][%-25c] %m%n")
.build(), new CliToolTestCase.MockTerminal());
LogConfigurator.configure(environment.settings());
// args should overwrite whatever is in the config
ESLogger esLogger = Log4jESLoggerFactory.getLogger("test");
Logger logger = ((Log4jESLogger) esLogger).logger();
Appender appender = logger.getAppender("console");
assertThat(appender, notNullValue());
assertTrue(logger.isTraceEnabled());
appender = logger.getAppender("file");
assertThat(appender, nullValue());
} }
private static String loggingConfiguration(String suffix) { private static String loggingConfiguration(String suffix) {