diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2017-08-17 17:26:14 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2017-09-05 14:24:12 +0200 |
commit | 72e202b73cffcedc1ca3cc7311008f2a39006d21 (patch) | |
tree | eefa4271b6c7ac84c5c9c2d1fea42c5f81397698 /server/sonar-process-monitor | |
parent | f513a71898b0ec20696140763ecffb37246212c0 (diff) | |
download | sonarqube-72e202b73cffcedc1ca3cc7311008f2a39006d21.tar.gz sonarqube-72e202b73cffcedc1ca3cc7311008f2a39006d21.zip |
SONAR-9239 configure log4j to log as before in es.log
with support for loggers configuration through LogLevelConfig, all three rolling strategies (time, size and none) and almost the same log pattern (shortening of logger names is a big different in log4j)
Diffstat (limited to 'server/sonar-process-monitor')
4 files changed, 183 insertions, 24 deletions
diff --git a/server/sonar-process-monitor/src/main/java/org/sonar/application/AppLogging.java b/server/sonar-process-monitor/src/main/java/org/sonar/application/AppLogging.java index 2755f893c55..5e19aab5945 100644 --- a/server/sonar-process-monitor/src/main/java/org/sonar/application/AppLogging.java +++ b/server/sonar-process-monitor/src/main/java/org/sonar/application/AppLogging.java @@ -137,7 +137,7 @@ public class AppLogging { configureWithWrapperWritingToFile(ctx); } helper.apply( - LogLevelConfig.newBuilder() + LogLevelConfig.newBuilder(helper.getRootLoggerName()) .rootLevelFor(ProcessId.APP) .immutableLevel("com.hazelcast", Level.toLevel( diff --git a/server/sonar-process-monitor/src/main/java/org/sonar/application/process/CommandFactoryImpl.java b/server/sonar-process-monitor/src/main/java/org/sonar/application/process/CommandFactoryImpl.java index 2a2bffde8a9..35d018126c0 100644 --- a/server/sonar-process-monitor/src/main/java/org/sonar/application/process/CommandFactoryImpl.java +++ b/server/sonar-process-monitor/src/main/java/org/sonar/application/process/CommandFactoryImpl.java @@ -23,11 +23,9 @@ import java.io.File; import java.nio.file.Path; import java.util.Map; import java.util.Optional; -import java.util.Properties; import org.sonar.application.config.AppSettings; import org.sonar.process.ProcessId; import org.sonar.process.ProcessProperties; -import org.sonar.process.logging.LogbackHelper; import static org.sonar.process.ProcessProperties.HTTPS_PROXY_HOST; import static org.sonar.process.ProcessProperties.HTTPS_PROXY_PORT; @@ -71,7 +69,7 @@ public class CommandFactoryImpl implements CommandFactory { .setWorkDir(executable.getParentFile().getParentFile()) .setExecutable(executable) .setConfDir(confDir) - .setLog4j2Properties(buildLog4j2Properties(logDir)) + .setLog4j2Properties(new EsLogging().createProperties(settings.getProps(), logDir)) .setArguments(this.settings.getProps().rawProperties()) .setClusterName(settingsMap.get("cluster.name")) .setHost(settingsMap.get("network.host")) @@ -87,26 +85,6 @@ public class CommandFactoryImpl implements CommandFactory { return res; } - private Properties buildLog4j2Properties(File logDir) { - // FIXME create a Log4jHelper which shares code with LogbackHelper to build this Properties object + not make LogbackHelper.resolveLevel public + provide correct log format, rolling policy, ... - String logLevel = LogbackHelper.resolveLevel(settings.getProps(), "sonar.log.level", "sonar.log.level.es").toString(); - Properties log4j2Properties = new Properties(); - log4j2Properties.put("status", "error"); - log4j2Properties.put("appender.rolling.type", "RollingFile"); - log4j2Properties.put("appender.rolling.name", "rolling"); - log4j2Properties.put("appender.rolling.fileName", new File(logDir, "es.log").getAbsolutePath()); - log4j2Properties.put("appender.rolling.layout.type", "PatternLayout"); - log4j2Properties.put("appender.rolling.layout.pattern", "[%d{ISO8601}][%-5p][%-25c{1.}] %marker%.-10000m%n"); - log4j2Properties.put("appender.rolling.filePattern", "${sys:es.logs}-%d{yyyy-MM-dd}.log"); - log4j2Properties.put("appender.rolling.policies.type", "Policies"); - log4j2Properties.put("appender.rolling.policies.time.type", "TimeBasedTriggeringPolicy"); - log4j2Properties.put("appender.rolling.policies.time.interval", "1"); - log4j2Properties.put("appender.rolling.policies.time.modulate", "true"); - log4j2Properties.put("rootLogger.level", logLevel); - log4j2Properties.put("rootLogger.appenderRef.rolling.ref", "rolling"); - return log4j2Properties; - } - private static String getExecutable() { if (System.getProperty("os.name").startsWith("Windows")) { return "elasticsearch/bin/elasticsearch.bat"; diff --git a/server/sonar-process-monitor/src/main/java/org/sonar/application/process/EsLogging.java b/server/sonar-process-monitor/src/main/java/org/sonar/application/process/EsLogging.java new file mode 100644 index 00000000000..4dadb95c9fa --- /dev/null +++ b/server/sonar-process-monitor/src/main/java/org/sonar/application/process/EsLogging.java @@ -0,0 +1,50 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.application.process; + +import ch.qos.logback.classic.Level; +import java.io.File; +import java.util.Properties; +import org.sonar.process.ProcessId; +import org.sonar.process.Props; +import org.sonar.process.logging.Log4JPropertiesBuilder; +import org.sonar.process.logging.LogLevelConfig; +import org.sonar.process.logging.RootLoggerConfig; + +import static org.sonar.process.logging.RootLoggerConfig.newRootLoggerConfigBuilder; + +public class EsLogging { + + public Properties createProperties(Props props, File logDir) { + Log4JPropertiesBuilder log4JPropertiesBuilder = new Log4JPropertiesBuilder(props); + RootLoggerConfig config = newRootLoggerConfigBuilder().setProcessId(ProcessId.ELASTICSEARCH).build(); + String logPattern = log4JPropertiesBuilder.buildLogPattern(config); + + log4JPropertiesBuilder.internalLogLevel(Level.ERROR); + log4JPropertiesBuilder.configureGlobalFileLog(config, logDir, logPattern); + log4JPropertiesBuilder.apply( + LogLevelConfig.newBuilder(log4JPropertiesBuilder.getRootLoggerName()) + .rootLevelFor(ProcessId.ELASTICSEARCH) + .build()); + + return log4JPropertiesBuilder.get(); + } + +} diff --git a/server/sonar-process-monitor/src/test/java/org/sonar/application/process/EsLoggingTest.java b/server/sonar-process-monitor/src/test/java/org/sonar/application/process/EsLoggingTest.java new file mode 100644 index 00000000000..a5f55d14886 --- /dev/null +++ b/server/sonar-process-monitor/src/test/java/org/sonar/application/process/EsLoggingTest.java @@ -0,0 +1,131 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.application.process; + +import java.io.File; +import java.io.IOException; +import java.util.HashSet; +import java.util.Properties; +import java.util.Set; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.sonar.process.Props; + +import static org.assertj.core.api.Assertions.assertThat; + +public class EsLoggingTest { + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + + private EsLogging underTest = new EsLogging(); + + @Test + public void createProperties_with_empty_props() throws IOException { + File logDir = temporaryFolder.newFolder(); + Properties properties = underTest.createProperties(newProps(), logDir); + + verifyProperties(properties, + "status", "ERROR", + "appender.file_es.type", "RollingFile", + "appender.file_es.name", "file_es", + "appender.file_es.filePattern", new File(logDir, "es.%d{yyyy-MM-dd}.log").getAbsolutePath(), + "appender.file_es.fileName", new File(logDir, "es.log").getAbsolutePath(), + "appender.file_es.layout.type", "PatternLayout", + "appender.file_es.layout.pattern", "%d{yyyy.MM.dd HH:mm:ss} %-5level es[][%logger{1.}] %msg%n", + "appender.file_es.policies.type", "Policies", + "appender.file_es.policies.time.type", "TimeBasedTriggeringPolicy", + "appender.file_es.policies.time.interval", "1", + "appender.file_es.policies.time.modulate", "true", + "appender.file_es.strategy.type", "DefaultRolloverStrategy", + "appender.file_es.strategy.fileIndex", "nomax", + "appender.file_es.strategy.action.type", "Delete", + "appender.file_es.strategy.action.basepath", logDir.getAbsolutePath(), + "appender.file_es.strategy.action.maxDepth", "1", + "appender.file_es.strategy.action.condition.type", "IfFileName", + "appender.file_es.strategy.action.condition.glob", "es*", + "appender.file_es.strategy.action.condition.nested_condition.type", "IfAccumulatedFileCount", + "appender.file_es.strategy.action.condition.nested_condition.exceeds", "7", + "rootLogger.level", "INFO", + "rootLogger.appenderRef.file_es.ref", "file_es"); + } + + @Test + public void createProperties_sets_root_logger_to_INFO_if_no_property_is_set() throws IOException { + File logDir = temporaryFolder.newFolder(); + Properties properties = underTest.createProperties(newProps(), logDir); + + assertThat(properties.getProperty("rootLogger.level")).isEqualTo("INFO"); + } + + @Test + public void createProperties_sets_root_logger_to_global_property_if_set() throws IOException { + File logDir = temporaryFolder.newFolder(); + Properties properties = underTest.createProperties(newProps("sonar.log.level", "TRACE"), logDir); + + assertThat(properties.getProperty("rootLogger.level")).isEqualTo("TRACE"); + } + + @Test + public void createProperties_sets_root_logger_to_process_property_if_set() throws IOException { + File logDir = temporaryFolder.newFolder(); + Properties properties = underTest.createProperties(newProps("sonar.log.level.es", "DEBUG"), logDir); + + assertThat(properties.getProperty("rootLogger.level")).isEqualTo("DEBUG"); + } + + @Test + public void createProperties_sets_root_logger_to_process_property_over_global_property_if_both_set() throws IOException { + File logDir = temporaryFolder.newFolder(); + Properties properties = underTest.createProperties( + newProps( + "sonar.log.level", "DEBUG", + "sonar.log.level.es", "TRACE"), + logDir); + + assertThat(properties.getProperty("rootLogger.level")).isEqualTo("TRACE"); + } + + private static Props newProps(String... propertyKeysAndValues) { + assertThat(propertyKeysAndValues.length % 2).describedAs("Number of parameters must be even").isEqualTo(0); + Properties properties = new Properties(); + for (int i = 0; i < propertyKeysAndValues.length; i++) { + properties.put(propertyKeysAndValues[i++], propertyKeysAndValues[i]); + } + return new Props(properties); + } + + private void verifyProperties(Properties properties, String... expectedPropertyKeysAndValuesOrdered) { + if (expectedPropertyKeysAndValuesOrdered.length == 0) { + assertThat(properties.size()).isEqualTo(0); + } else { + assertThat(expectedPropertyKeysAndValuesOrdered.length % 2).describedAs("Number of parameters must be even").isEqualTo(0); + Set<String> keys = new HashSet<>(expectedPropertyKeysAndValuesOrdered.length / 2 + 1); + keys.add("status"); + for (int i = 0; i < expectedPropertyKeysAndValuesOrdered.length; i++) { + String key = expectedPropertyKeysAndValuesOrdered[i++]; + String value = expectedPropertyKeysAndValuesOrdered[i]; + assertThat(properties.get(key)).describedAs("Unexpected value for property " + key).isEqualTo(value); + keys.add(key); + } + assertThat(properties.keySet()).containsOnly(keys.toArray()); + } + } +} |