aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-process
diff options
context:
space:
mode:
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>2019-09-26 16:13:35 +0200
committerSonarTech <sonartech@sonarsource.com>2019-09-27 20:21:05 +0200
commit2542ddd092f5910aed39895a1b3061148aa8e03f (patch)
treee379fede9249c44bd4c9d9d85e76b3ccac55d1d0 /server/sonar-process
parent614259b46994ba20797f3395deb510eda24bf6ba (diff)
downloadsonarqube-2542ddd092f5910aed39895a1b3061148aa8e03f.tar.gz
sonarqube-2542ddd092f5910aed39895a1b3061148aa8e03f.zip
SONAR-12344 es logs must go to stdout too when sonar.log.console=true
Diffstat (limited to 'server/sonar-process')
-rw-r--r--server/sonar-process/src/main/java/org/sonar/process/logging/Log4JPropertiesBuilder.java46
-rw-r--r--server/sonar-process/src/test/java/org/sonar/process/logging/Log4JPropertiesBuilderTest.java22
2 files changed, 61 insertions, 7 deletions
diff --git a/server/sonar-process/src/main/java/org/sonar/process/logging/Log4JPropertiesBuilder.java b/server/sonar-process/src/main/java/org/sonar/process/logging/Log4JPropertiesBuilder.java
index bc37a13a4a1..d365281dc18 100644
--- a/server/sonar-process/src/main/java/org/sonar/process/logging/Log4JPropertiesBuilder.java
+++ b/server/sonar-process/src/main/java/org/sonar/process/logging/Log4JPropertiesBuilder.java
@@ -87,7 +87,20 @@ public class Log4JPropertiesBuilder extends AbstractLogHelper {
private String writeFileAppender(RootLoggerConfig config, File logDir, String logPattern) {
String appenderName = "file_" + config.getProcessId().getLogFilenamePrefix();
RollingPolicy rollingPolicy = createRollingPolicy(logDir, config.getProcessId().getLogFilenamePrefix());
- Appender appender = new Appender(appenderName, rollingPolicy, logPattern);
+ FileAppender appender = new FileAppender(appenderName, rollingPolicy, logPattern);
+ appender.writeAppenderProperties();
+ return appender.getAppenderRef();
+ }
+
+ public void configureGlobalStdoutLog(RootLoggerConfig config, String logPattern) {
+ String appenderRef = writeStdoutAppender(config, logPattern);
+
+ putProperty(ROOT_LOGGER_NAME + ".appenderRef." + appenderRef + ".ref", appenderRef);
+ }
+
+ private String writeStdoutAppender(RootLoggerConfig config, String logPattern) {
+ String appenderName = "stdout";
+ ConsoleAppender appender = new ConsoleAppender(appenderName, logPattern);
appender.writeAppenderProperties();
return appender.getAppenderRef();
}
@@ -153,13 +166,13 @@ public class Log4JPropertiesBuilder extends AbstractLogHelper {
}
}
- private class Appender {
+ private class FileAppender {
private final String prefix;
private final String appenderName;
private final RollingPolicy rollingPolicy;
private final String logPattern;
- private Appender(String appenderName, RollingPolicy rollingPolicy, String logPattern) {
+ private FileAppender(String appenderName, RollingPolicy rollingPolicy, String logPattern) {
this.prefix = "appender." + appenderName + ".";
this.appenderName = appenderName;
this.rollingPolicy = rollingPolicy;
@@ -183,6 +196,33 @@ public class Log4JPropertiesBuilder extends AbstractLogHelper {
}
}
+ private class ConsoleAppender {
+ private final String prefix;
+ private final String appenderName;
+ private final String logPattern;
+
+ private ConsoleAppender(String appenderName, String logPattern) {
+ this.prefix = "appender." + appenderName + ".";
+ this.appenderName = appenderName;
+ this.logPattern = logPattern;
+ }
+
+ void writeAppenderProperties() {
+ put("type", "Console");
+ put("name", appenderName);
+ put("layout.type", "PatternLayout");
+ put("layout.pattern", logPattern);
+ }
+
+ void put(String key, String value) {
+ Log4JPropertiesBuilder.this.putProperty(this.prefix + key, value);
+ }
+
+ String getAppenderRef() {
+ return appenderName;
+ }
+ }
+
private abstract class RollingPolicy {
final String filenamePrefix;
final File logsDir;
diff --git a/server/sonar-process/src/test/java/org/sonar/process/logging/Log4JPropertiesBuilderTest.java b/server/sonar-process/src/test/java/org/sonar/process/logging/Log4JPropertiesBuilderTest.java
index 5398f87b65b..cc11d307d33 100644
--- a/server/sonar-process/src/test/java/org/sonar/process/logging/Log4JPropertiesBuilderTest.java
+++ b/server/sonar-process/src/test/java/org/sonar/process/logging/Log4JPropertiesBuilderTest.java
@@ -262,7 +262,7 @@ public class Log4JPropertiesBuilderTest {
ROLLING_POLICY_PROPERTY, "none");
underTest.configureGlobalFileLog(esRootLoggerConfig, logDir, logPattern);
- verifyPropertiesForConfigureGlobalFileLog(underTest.get(),
+ verifyProperties(underTest.get(),
"appender.file_es.type", "File",
"appender.file_es.name", "file_es",
"appender.file_es.fileName", new File(logDir, "es.log").getAbsolutePath(),
@@ -272,6 +272,20 @@ public class Log4JPropertiesBuilderTest {
}
@Test
+ public void name() {
+ String logPattern = randomAlphanumeric(15);
+ Log4JPropertiesBuilder underTest = newLog4JPropertiesBuilder();
+ underTest.configureGlobalStdoutLog(esRootLoggerConfig, logPattern);
+
+ verifyProperties(underTest.get(),
+ "appender.stdout.type", "Console",
+ "appender.stdout.name", "stdout",
+ "appender.stdout.layout.type", "PatternLayout",
+ "appender.stdout.layout.pattern", logPattern,
+ "rootLogger.appenderRef.stdout.ref", "stdout");
+ }
+
+ @Test
public void apply_fails_with_IAE_if_LogLevelConfig_does_not_have_rootLoggerName_of_Log4J() {
Log4JPropertiesBuilder underTest = newLog4JPropertiesBuilder();
LogLevelConfig logLevelConfig = LogLevelConfig.newBuilder(randomAlphanumeric(2)).build();
@@ -476,7 +490,7 @@ public class Log4JPropertiesBuilderTest {
}
private void verifyTimeRollingPolicy(Log4JPropertiesBuilder builder, File logDir, String logPattern, String datePattern, int maxFiles) {
- verifyPropertiesForConfigureGlobalFileLog(builder.get(),
+ verifyProperties(builder.get(),
"appender.file_es.type", "RollingFile",
"appender.file_es.name", "file_es",
"appender.file_es.filePattern", new File(logDir, "es.%d{" + datePattern + "}.log").getAbsolutePath(),
@@ -500,7 +514,7 @@ public class Log4JPropertiesBuilderTest {
}
private void verifySizeRollingPolicy(Log4JPropertiesBuilder builder, File logDir, String logPattern, String sizePattern, int maxFiles) {
- verifyPropertiesForConfigureGlobalFileLog(builder.get(),
+ verifyProperties(builder.get(),
"appender.file_es.type", "RollingFile",
"appender.file_es.name", "file_es",
"appender.file_es.filePattern", new File(logDir, "es.%i.log").getAbsolutePath(),
@@ -515,7 +529,7 @@ public class Log4JPropertiesBuilderTest {
"rootLogger.appenderRef.file_es.ref", "file_es");
}
- private void verifyPropertiesForConfigureGlobalFileLog(Properties properties, String... expectedPropertyKeysAndValuesOrdered) {
+ private void verifyProperties(Properties properties, String... expectedPropertyKeysAndValuesOrdered) {
assertThat(properties.get("status")).isEqualTo("ERROR");
if (expectedPropertyKeysAndValuesOrdered.length == 0) {
assertThat(properties.size()).isEqualTo(1);