aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-server
diff options
context:
space:
mode:
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>2016-11-29 13:48:00 +0100
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>2016-12-01 12:29:40 +0100
commit1f9bb0b02f646739bfbdfe9dc9e8ed15c86e4e52 (patch)
tree2338a6b5d034cc174fd18cf36c72970d2004d337 /server/sonar-server
parent93ea99696bfed149fa1f46888e864b3bde4b4328 (diff)
downloadsonarqube-1f9bb0b02f646739bfbdfe9dc9e8ed15c86e4e52.tar.gz
sonarqube-1f9bb0b02f646739bfbdfe9dc9e8ed15c86e4e52.zip
SONAR-8335 make changing root level change level of all loggers
Diffstat (limited to 'server/sonar-server')
-rw-r--r--server/sonar-server/src/main/java/org/sonar/ce/log/CeProcessLogging.java18
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/app/ServerProcessLogging.java59
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/app/WebServerProcessLogging.java18
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/platform/ServerLogging.java22
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel1.java2
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/platform/ws/ChangeLogLevelAction.java7
-rw-r--r--server/sonar-server/src/test/java/org/sonar/ce/log/CeProcessLoggingTest.java43
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/app/WebServerProcessLoggingTest.java41
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/platform/ServerLoggingTest.java79
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/platform/ws/ChangeLogLevelActionTest.java8
10 files changed, 193 insertions, 104 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/ce/log/CeProcessLogging.java b/server/sonar-server/src/main/java/org/sonar/ce/log/CeProcessLogging.java
index 76fdafff714..52a385de58f 100644
--- a/server/sonar-server/src/main/java/org/sonar/ce/log/CeProcessLogging.java
+++ b/server/sonar-server/src/main/java/org/sonar/ce/log/CeProcessLogging.java
@@ -19,13 +19,12 @@
*/
package org.sonar.ce.log;
-import org.sonar.process.logging.LogbackHelper;
import org.sonar.process.ProcessId;
-import org.sonar.process.Props;
+import org.sonar.process.logging.LogDomain;
+import org.sonar.process.logging.LogLevelConfig;
import org.sonar.server.app.ServerProcessLogging;
import static org.sonar.ce.log.CeLogging.MDC_CE_TASK_UUID;
-import static org.sonar.process.logging.LogbackHelper.LogDomain;
/**
* Configure logback for the Compute Engine process. Logs are written to file "ce.log" in SQ's log directory.
@@ -37,9 +36,14 @@ public class CeProcessLogging extends ServerProcessLogging {
}
@Override
- protected void extendConfiguration(LogbackHelper helper, Props props) {
- helper.configureLoggerLogLevelFromDomain("sql", props, ProcessId.COMPUTE_ENGINE, LogDomain.SQL);
- helper.configureLoggerLogLevelFromDomain("es", props, ProcessId.COMPUTE_ENGINE, LogDomain.ES_CLIENT);
- helper.configureLoggersLogLevelFromDomain(JMX_RMI_LOGGER_NAMES, props, ProcessId.COMPUTE_ENGINE, LogDomain.JMX);
+ protected void extendLogLevelConfiguration(LogLevelConfig.Builder logLevelConfigBuilder) {
+ logLevelConfigBuilder.levelByDomain("sql", ProcessId.COMPUTE_ENGINE, LogDomain.SQL);
+ logLevelConfigBuilder.levelByDomain("es", ProcessId.COMPUTE_ENGINE, LogDomain.ES_CLIENT);
+ JMX_RMI_LOGGER_NAMES.forEach(loggerName -> logLevelConfigBuilder.levelByDomain(loggerName, ProcessId.COMPUTE_ENGINE, LogDomain.JMX));
+ }
+
+ @Override
+ protected void extendConfigure() {
+ // nothing to do
}
}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/app/ServerProcessLogging.java b/server/sonar-server/src/main/java/org/sonar/server/app/ServerProcessLogging.java
index daadb4f1ddf..baeaa88d318 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/app/ServerProcessLogging.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/app/ServerProcessLogging.java
@@ -19,35 +19,60 @@
*/
package org.sonar.server.app;
+import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import com.google.common.collect.ImmutableSet;
import java.util.Set;
+import org.sonar.process.logging.LogLevelConfig;
import org.sonar.process.logging.LogbackHelper;
import org.sonar.process.ProcessId;
import org.sonar.process.Props;
import org.sonar.process.logging.RootLoggerConfig;
-import org.sonar.server.platform.ServerLogging;
import static org.sonar.process.logging.RootLoggerConfig.newRootLoggerConfigBuilder;
public abstract class ServerProcessLogging {
protected static final Set<String> JMX_RMI_LOGGER_NAMES = ImmutableSet.of(
- "javax.management.remote.timeout",
- "javax.management.remote.misc",
- "javax.management.remote.rmi",
- "javax.management.mbeanserver",
- "sun.rmi.loader",
- "sun.rmi.transport.tcp",
- "sun.rmi.transport.misc",
- "sun.rmi.server.call",
- "sun.rmi.dgc");
+ "javax.management.remote.timeout",
+ "javax.management.remote.misc",
+ "javax.management.remote.rmi",
+ "javax.management.mbeanserver",
+ "sun.rmi.loader",
+ "sun.rmi.transport.tcp",
+ "sun.rmi.transport.misc",
+ "sun.rmi.server.call",
+ "sun.rmi.dgc");
private final ProcessId processId;
private final String threadIdFieldPattern;
private final LogbackHelper helper = new LogbackHelper();
+ private final LogLevelConfig logLevelConfig;
protected ServerProcessLogging(ProcessId processId, String threadIdFieldPattern) {
this.processId = processId;
this.threadIdFieldPattern = threadIdFieldPattern;
+ this.logLevelConfig = createLogLevelConfiguration(processId);
+ }
+
+ private LogLevelConfig createLogLevelConfiguration(ProcessId processId) {
+ LogLevelConfig.Builder builder = LogLevelConfig.newBuilder();
+ builder.rootLevelFor(processId);
+ builder.immutableLevel("rails", Level.WARN);
+ builder.immutableLevel("org.apache.ibatis", Level.WARN);
+ builder.immutableLevel("java.sql", Level.WARN);
+ builder.immutableLevel("java.sql.ResultSet", Level.WARN);
+ builder.immutableLevel("org.sonar.MEASURE_FILTER", Level.WARN);
+ builder.immutableLevel("org.elasticsearch", Level.INFO);
+ builder.immutableLevel("org.elasticsearch.node", Level.INFO);
+ builder.immutableLevel("org.elasticsearch.http", Level.INFO);
+ builder.immutableLevel("ch.qos.logback", Level.WARN);
+ builder.immutableLevel("org.apache.catalina", Level.INFO);
+ builder.immutableLevel("org.apache.coyote", Level.INFO);
+ builder.immutableLevel("org.apache.jasper", Level.INFO);
+ builder.immutableLevel("org.apache.tomcat", Level.INFO);
+
+ extendLogLevelConfiguration(builder);
+
+ return builder.build();
}
public LoggerContext configure(Props props) {
@@ -56,13 +81,20 @@ public abstract class ServerProcessLogging {
helper.enableJulChangePropagation(ctx);
configureRootLogger(props);
+ helper.apply(logLevelConfig, props);
- extendConfiguration(helper, props);
+ extendConfigure();
return ctx;
}
- protected abstract void extendConfiguration(LogbackHelper helper, Props props);
+ public LogLevelConfig getLogLevelConfig() {
+ return this.logLevelConfig;
+ }
+
+ protected abstract void extendLogLevelConfiguration(LogLevelConfig.Builder logLevelConfigBuilder);
+
+ protected abstract void extendConfigure();
private void configureRootLogger(Props props) {
RootLoggerConfig config = newRootLoggerConfigBuilder()
@@ -73,9 +105,6 @@ public abstract class ServerProcessLogging {
helper.configureGlobalFileLog(props, config, logPattern);
helper.configureForSubprocessGobbler(props, logPattern);
-
- helper.configureRootLogLevel(props, processId);
- ServerLogging.configureHardcodedLevels(helper);
}
}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/app/WebServerProcessLogging.java b/server/sonar-server/src/main/java/org/sonar/server/app/WebServerProcessLogging.java
index 202b9e1f5ce..da2a4f6ad48 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/app/WebServerProcessLogging.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/app/WebServerProcessLogging.java
@@ -21,11 +21,10 @@ package org.sonar.server.app;
import java.util.logging.LogManager;
import org.slf4j.bridge.SLF4JBridgeHandler;
-import org.sonar.process.logging.LogbackHelper;
+import org.sonar.process.logging.LogLevelConfig;
import org.sonar.process.ProcessId;
-import org.sonar.process.Props;
-import static org.sonar.process.logging.LogbackHelper.LogDomain;
+import org.sonar.process.logging.LogDomain;
import static org.sonar.server.platform.web.requestid.RequestIdMDCStorage.HTTP_REQUEST_ID_MDC_KEY;
/**
@@ -38,13 +37,16 @@ public class WebServerProcessLogging extends ServerProcessLogging {
}
@Override
- protected void extendConfiguration(LogbackHelper helper, Props props) {
+ protected void extendLogLevelConfiguration(LogLevelConfig.Builder logLevelConfigBuilder) {
+ logLevelConfigBuilder.levelByDomain("sql", ProcessId.WEB_SERVER, LogDomain.SQL);
+ logLevelConfigBuilder.levelByDomain("es", ProcessId.WEB_SERVER, LogDomain.ES_CLIENT);
+ JMX_RMI_LOGGER_NAMES.forEach(loggerName -> logLevelConfigBuilder.levelByDomain(loggerName, ProcessId.WEB_SERVER, LogDomain.JMX));
+ }
+
+ @Override
+ protected void extendConfigure() {
// Configure java.util.logging, used by Tomcat, in order to forward to slf4j
LogManager.getLogManager().reset();
SLF4JBridgeHandler.install();
-
- helper.configureLoggerLogLevelFromDomain("sql", props, ProcessId.WEB_SERVER, LogDomain.SQL);
- helper.configureLoggerLogLevelFromDomain("es", props, ProcessId.WEB_SERVER, LogDomain.ES_CLIENT);
- helper.configureLoggersLogLevelFromDomain(JMX_RMI_LOGGER_NAMES, props, ProcessId.WEB_SERVER, LogDomain.JMX);
}
}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/ServerLogging.java b/server/sonar-server/src/main/java/org/sonar/server/platform/ServerLogging.java
index 9dd8c447592..08feeea802c 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/platform/ServerLogging.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/platform/ServerLogging.java
@@ -31,6 +31,7 @@ import org.sonar.api.utils.log.LoggerLevel;
import org.sonar.api.utils.log.Loggers;
import org.sonar.process.logging.LogbackHelper;
import org.sonar.process.ProcessProperties;
+import org.sonar.server.app.ServerProcessLogging;
@ServerSide
@ComputeEngineSide
@@ -49,10 +50,9 @@ public class ServerLogging {
this.settings = settings;
}
- public void changeLevel(LoggerLevel level) {
+ public void changeLevel(ServerProcessLogging serverProcessLogging, LoggerLevel level) {
Level logbackLevel = Level.toLevel(level.name());
- helper.configureRootLogLevel(logbackLevel);
- configureHardcodedLevels(helper);
+ helper.changeRoot(serverProcessLogging.getLogLevelConfig(), logbackLevel);
LoggerFactory.getLogger(ServerLogging.class).info("Level of logs changed to {}", level);
}
@@ -60,22 +60,6 @@ public class ServerLogging {
return Loggers.get(Logger.ROOT_LOGGER_NAME).getLevel();
}
- public static void configureHardcodedLevels(LogbackHelper helper) {
- helper.configureLogger("rails", Level.WARN);
- helper.configureLogger("org.apache.ibatis", Level.WARN);
- helper.configureLogger("java.sql", Level.WARN);
- helper.configureLogger("java.sql.ResultSet", Level.WARN);
- helper.configureLogger("org.sonar.MEASURE_FILTER", Level.WARN);
- helper.configureLogger("org.elasticsearch", Level.INFO);
- helper.configureLogger("org.elasticsearch.node", Level.INFO);
- helper.configureLogger("org.elasticsearch.http", Level.INFO);
- helper.configureLogger("ch.qos.logback", Level.WARN);
- helper.configureLogger("org.apache.catalina", Level.INFO);
- helper.configureLogger("org.apache.coyote", Level.INFO);
- helper.configureLogger("org.apache.jasper", Level.INFO);
- helper.configureLogger("org.apache.tomcat", Level.INFO);
- }
-
/**
* The directory that contains log files. May not exist.
*/
diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel1.java b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel1.java
index 6fecd6991dc..7bc4c362646 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel1.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel1.java
@@ -40,6 +40,7 @@ import org.sonar.db.version.DatabaseVersion;
import org.sonar.process.logging.LogbackHelper;
import org.sonar.server.app.ProcessCommandWrapperImpl;
import org.sonar.server.app.RestartFlagHolderImpl;
+import org.sonar.server.app.WebServerProcessLogging;
import org.sonar.server.issue.index.IssueIndex;
import org.sonar.server.platform.DatabaseServerCompatibility;
import org.sonar.server.platform.LogServerVersion;
@@ -87,6 +88,7 @@ public class PlatformLevel1 extends PlatformLevel {
UrlSettings.class,
EmbeddedDatabaseFactory.class,
LogbackHelper.class,
+ WebServerProcessLogging.class,
DefaultDatabase.class,
DatabaseChecker.class,
// must instantiate deprecated class in 5.2 and only this one (and not its replacement)
diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/ws/ChangeLogLevelAction.java b/server/sonar-server/src/main/java/org/sonar/server/platform/ws/ChangeLogLevelAction.java
index be2d3602580..1ccb94afe40 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/platform/ws/ChangeLogLevelAction.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/platform/ws/ChangeLogLevelAction.java
@@ -25,6 +25,7 @@ import org.sonar.api.server.ws.WebService;
import org.sonar.api.utils.log.LoggerLevel;
import org.sonar.ce.http.CeHttpClient;
import org.sonar.db.Database;
+import org.sonar.server.app.WebServerProcessLogging;
import org.sonar.server.platform.ServerLogging;
import org.sonar.server.user.UserSession;
@@ -38,12 +39,14 @@ public class ChangeLogLevelAction implements SystemWsAction {
private final ServerLogging logging;
private final Database db;
private final CeHttpClient ceHttpClient;
+ private final WebServerProcessLogging webServerProcessLogging;
- public ChangeLogLevelAction(UserSession userSession, ServerLogging logging, Database db, CeHttpClient ceHttpClient) {
+ public ChangeLogLevelAction(UserSession userSession, ServerLogging logging, Database db, CeHttpClient ceHttpClient, WebServerProcessLogging webServerProcessLogging) {
this.userSession = userSession;
this.logging = logging;
this.db = db;
this.ceHttpClient = ceHttpClient;
+ this.webServerProcessLogging = webServerProcessLogging;
}
@Override
@@ -67,7 +70,7 @@ public class ChangeLogLevelAction implements SystemWsAction {
LoggerLevel level = LoggerLevel.valueOf(wsRequest.mandatoryParam(PARAM_LEVEL));
db.enableSqlLogging(level.equals(LoggerLevel.TRACE));
- logging.changeLevel(level);
+ logging.changeLevel(webServerProcessLogging, level);
ceHttpClient.changeLogLevel(level);
wsResponse.noContent();
}
diff --git a/server/sonar-server/src/test/java/org/sonar/ce/log/CeProcessLoggingTest.java b/server/sonar-server/src/test/java/org/sonar/ce/log/CeProcessLoggingTest.java
index f16620049ef..0058688f056 100644
--- a/server/sonar-server/src/test/java/org/sonar/ce/log/CeProcessLoggingTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/ce/log/CeProcessLoggingTest.java
@@ -36,9 +36,9 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
-import org.sonar.process.logging.LogbackHelper;
import org.sonar.process.ProcessProperties;
import org.sonar.process.Props;
+import org.sonar.process.logging.LogbackHelper;
import static org.assertj.core.api.Assertions.assertThat;
import static org.slf4j.Logger.ROOT_LOGGER_NAME;
@@ -385,6 +385,31 @@ public class CeProcessLoggingTest {
underTest.configure(props);
}
+ @Test
+ public void configure_defines_hardcoded_levels() {
+ LoggerContext context = underTest.configure(props);
+
+ verifyImmutableLogLevels(context);
+ }
+
+ @Test
+ public void configure_defines_hardcoded_levels_unchanged_by_global_property() {
+ props.set("sonar.log.level", "TRACE");
+
+ LoggerContext context = underTest.configure(props);
+
+ verifyImmutableLogLevels(context);
+ }
+
+ @Test
+ public void configure_defines_hardcoded_levels_unchanged_by_ce_property() {
+ props.set("sonar.log.level.ce", "TRACE");
+
+ LoggerContext context = underTest.configure(props);
+
+ verifyImmutableLogLevels(context);
+ }
+
private void verifyRootLogLevel(LoggerContext ctx, Level expected) {
assertThat(ctx.getLogger(ROOT_LOGGER_NAME).getLevel()).isEqualTo(expected);
}
@@ -408,4 +433,20 @@ public class CeProcessLoggingTest {
assertThat(ctx.getLogger("sun.rmi.server.call").getLevel()).isEqualTo(expected);
assertThat(ctx.getLogger("sun.rmi.dgc").getLevel()).isEqualTo(expected);
}
+
+ private void verifyImmutableLogLevels(LoggerContext ctx) {
+ assertThat(ctx.getLogger("rails").getLevel()).isEqualTo(Level.WARN);
+ assertThat(ctx.getLogger("org.apache.ibatis").getLevel()).isEqualTo(Level.WARN);
+ assertThat(ctx.getLogger("java.sql").getLevel()).isEqualTo(Level.WARN);
+ assertThat(ctx.getLogger("java.sql.ResultSet").getLevel()).isEqualTo(Level.WARN);
+ assertThat(ctx.getLogger("org.sonar.MEASURE_FILTER").getLevel()).isEqualTo(Level.WARN);
+ assertThat(ctx.getLogger("org.elasticsearch").getLevel()).isEqualTo(Level.INFO);
+ assertThat(ctx.getLogger("org.elasticsearch.node").getLevel()).isEqualTo(Level.INFO);
+ assertThat(ctx.getLogger("org.elasticsearch.http").getLevel()).isEqualTo(Level.INFO);
+ assertThat(ctx.getLogger("ch.qos.logback").getLevel()).isEqualTo(Level.WARN);
+ assertThat(ctx.getLogger("org.apache.catalina").getLevel()).isEqualTo(Level.INFO);
+ assertThat(ctx.getLogger("org.apache.coyote").getLevel()).isEqualTo(Level.INFO);
+ assertThat(ctx.getLogger("org.apache.jasper").getLevel()).isEqualTo(Level.INFO);
+ assertThat(ctx.getLogger("org.apache.tomcat").getLevel()).isEqualTo(Level.INFO);
+ }
}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/app/WebServerProcessLoggingTest.java b/server/sonar-server/src/test/java/org/sonar/server/app/WebServerProcessLoggingTest.java
index 05b05b77f30..02dba16937a 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/app/WebServerProcessLoggingTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/app/WebServerProcessLoggingTest.java
@@ -385,6 +385,31 @@ public class WebServerProcessLoggingTest {
underTest.configure(props);
}
+ @Test
+ public void configure_defines_hardcoded_levels() {
+ LoggerContext context = underTest.configure(props);
+
+ verifyImmutableLogLevels(context);
+ }
+
+ @Test
+ public void configure_defines_hardcoded_levels_unchanged_by_global_property() {
+ props.set("sonar.log.level", "TRACE");
+
+ LoggerContext context = underTest.configure(props);
+
+ verifyImmutableLogLevels(context);
+ }
+
+ @Test
+ public void configure_defines_hardcoded_levels_unchanged_by_ce_property() {
+ props.set("sonar.log.level.ce", "TRACE");
+
+ LoggerContext context = underTest.configure(props);
+
+ verifyImmutableLogLevels(context);
+ }
+
private void verifyRootLogLevel(LoggerContext ctx, Level expected) {
Logger rootLogger = ctx.getLogger(ROOT_LOGGER_NAME);
assertThat(rootLogger.getLevel()).isEqualTo(expected);
@@ -410,4 +435,20 @@ public class WebServerProcessLoggingTest {
assertThat(ctx.getLogger("sun.rmi.dgc").getLevel()).isEqualTo(expected);
}
+ private void verifyImmutableLogLevels(LoggerContext ctx) {
+ assertThat(ctx.getLogger("rails").getLevel()).isEqualTo(Level.WARN);
+ assertThat(ctx.getLogger("org.apache.ibatis").getLevel()).isEqualTo(Level.WARN);
+ assertThat(ctx.getLogger("java.sql").getLevel()).isEqualTo(Level.WARN);
+ assertThat(ctx.getLogger("java.sql.ResultSet").getLevel()).isEqualTo(Level.WARN);
+ assertThat(ctx.getLogger("org.sonar.MEASURE_FILTER").getLevel()).isEqualTo(Level.WARN);
+ assertThat(ctx.getLogger("org.elasticsearch").getLevel()).isEqualTo(Level.INFO);
+ assertThat(ctx.getLogger("org.elasticsearch.node").getLevel()).isEqualTo(Level.INFO);
+ assertThat(ctx.getLogger("org.elasticsearch.http").getLevel()).isEqualTo(Level.INFO);
+ assertThat(ctx.getLogger("ch.qos.logback").getLevel()).isEqualTo(Level.WARN);
+ assertThat(ctx.getLogger("org.apache.catalina").getLevel()).isEqualTo(Level.INFO);
+ assertThat(ctx.getLogger("org.apache.coyote").getLevel()).isEqualTo(Level.INFO);
+ assertThat(ctx.getLogger("org.apache.jasper").getLevel()).isEqualTo(Level.INFO);
+ assertThat(ctx.getLogger("org.apache.tomcat").getLevel()).isEqualTo(Level.INFO);
+ }
+
}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/ServerLoggingTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/ServerLoggingTest.java
index 4092a0cce72..9dc813ddaf5 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/platform/ServerLoggingTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/platform/ServerLoggingTest.java
@@ -20,24 +20,32 @@
package org.sonar.server.platform;
import ch.qos.logback.classic.Level;
+import com.tngtech.java.junit.dataprovider.DataProvider;
+import com.tngtech.java.junit.dataprovider.DataProviderRunner;
+import com.tngtech.java.junit.dataprovider.UseDataProvider;
import java.io.File;
import java.io.IOException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
+import org.junit.runner.RunWith;
import org.sonar.api.config.MapSettings;
import org.sonar.api.config.Settings;
import org.sonar.api.utils.log.LogTester;
import org.sonar.api.utils.log.LoggerLevel;
import org.sonar.process.ProcessProperties;
+import org.sonar.process.logging.LogLevelConfig;
import org.sonar.process.logging.LogbackHelper;
+import org.sonar.server.app.ServerProcessLogging;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+@RunWith(DataProviderRunner.class)
public class ServerLoggingTest {
@Rule
@@ -67,66 +75,39 @@ public class ServerLoggingTest {
}
@Test
- public void configureHardcodedLevels() {
- LogbackHelper logbackHelper = mock(LogbackHelper.class);
- ServerLogging.configureHardcodedLevels(logbackHelper);
+ @UseDataProvider("supportedSonarApiLevels")
+ public void changeLevel_calls_changeRoot_with_LogLevelConfig_and_level_converted_to_logback_class_then_log_INFO_message(LoggerLevel level) {
+ ServerProcessLogging serverProcessLogging = mock(ServerProcessLogging.class);
+ LogLevelConfig logLevelConfig = LogLevelConfig.newBuilder().build();
+ when(serverProcessLogging.getLogLevelConfig()).thenReturn(logLevelConfig);
- verifyHardcodedLevels(logbackHelper);
- }
-
- @Test
- public void changeLevel_throws_IAE_if_level_is_WARN() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("WARN log level is not supported (allowed levels are [");
+ underTest.changeLevel(serverProcessLogging, level);
- underTest.changeLevel(LoggerLevel.WARN);
+ verify(logbackHelper).changeRoot(logLevelConfig, Level.valueOf(level.name()));
}
- @Test
- public void changeLevel_throws_IAE_if_level_is_ERROR() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("ERROR log level is not supported (allowed levels are [");
-
- underTest.changeLevel(LoggerLevel.ERROR);
+ @DataProvider
+ public static Object[][] supportedSonarApiLevels() {
+ return new Object[][] {
+ {LoggerLevel.INFO},
+ {LoggerLevel.DEBUG},
+ {LoggerLevel.TRACE}
+ };
}
@Test
- public void changeLevel_changes_root_logger_level_to_INFO() {
- underTest.changeLevel(LoggerLevel.INFO);
-
- verify(logbackHelper).configureRootLogLevel(Level.INFO);
- verifyHardcodedLevels(logbackHelper);
- }
-
- @Test
- public void changeLevel_changes_root_logger_level_to_DEBUG() {
- underTest.changeLevel(LoggerLevel.DEBUG);
+ public void changeLevel_fails_with_IAE_when_level_is_ERROR() {
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage("ERROR log level is not supported (allowed levels are [TRACE, DEBUG, INFO])");
- verify(logbackHelper).configureRootLogLevel(Level.DEBUG);
- verifyHardcodedLevels(logbackHelper);
+ underTest.changeLevel(mock(ServerProcessLogging.class), LoggerLevel.ERROR);
}
@Test
- public void changeLevel_changes_root_logger_level_to_TRACE() {
- underTest.changeLevel(LoggerLevel.TRACE);
-
- verify(logbackHelper).configureRootLogLevel(Level.TRACE);
- verifyHardcodedLevels(logbackHelper);
- }
+ public void changeLevel_fails_with_IAE_when_level_is_WARN() {
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage("WARN log level is not supported (allowed levels are [TRACE, DEBUG, INFO])");
- private void verifyHardcodedLevels(LogbackHelper logbackHelper) {
- verify(logbackHelper).configureLogger("rails", Level.WARN);
- verify(logbackHelper).configureLogger("org.apache.ibatis", Level.WARN);
- verify(logbackHelper).configureLogger("java.sql", Level.WARN);
- verify(logbackHelper).configureLogger("java.sql.ResultSet", Level.WARN);
- verify(logbackHelper).configureLogger("org.sonar.MEASURE_FILTER", Level.WARN);
- verify(logbackHelper).configureLogger("org.elasticsearch", Level.INFO);
- verify(logbackHelper).configureLogger("org.elasticsearch.node", Level.INFO);
- verify(logbackHelper).configureLogger("org.elasticsearch.http", Level.INFO);
- verify(logbackHelper).configureLogger("ch.qos.logback", Level.WARN);
- verify(logbackHelper).configureLogger("org.apache.catalina", Level.INFO);
- verify(logbackHelper).configureLogger("org.apache.coyote", Level.INFO);
- verify(logbackHelper).configureLogger("org.apache.jasper", Level.INFO);
- verify(logbackHelper).configureLogger("org.apache.tomcat", Level.INFO);
+ underTest.changeLevel(mock(ServerProcessLogging.class), LoggerLevel.WARN);
}
}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/ws/ChangeLogLevelActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/ws/ChangeLogLevelActionTest.java
index bdf1f6abf0f..ca30fa204e7 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/platform/ws/ChangeLogLevelActionTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/platform/ws/ChangeLogLevelActionTest.java
@@ -25,6 +25,7 @@ import org.junit.rules.ExpectedException;
import org.sonar.api.utils.log.LoggerLevel;
import org.sonar.ce.http.CeHttpClient;
import org.sonar.db.Database;
+import org.sonar.server.app.WebServerProcessLogging;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.platform.ServerLogging;
import org.sonar.server.tester.UserSessionRule;
@@ -43,7 +44,8 @@ public class ChangeLogLevelActionTest {
private ServerLogging serverLogging = mock(ServerLogging.class);
private Database db = mock(Database.class);
private CeHttpClient ceHttpClient = mock(CeHttpClient.class);
- private ChangeLogLevelAction underTest = new ChangeLogLevelAction(userSession, serverLogging, db, ceHttpClient);
+ private WebServerProcessLogging webServerProcessLogging = new WebServerProcessLogging();
+ private ChangeLogLevelAction underTest = new ChangeLogLevelAction(userSession, serverLogging, db, ceHttpClient, webServerProcessLogging);
private WsActionTester actionTester = new WsActionTester(underTest);
@Test
@@ -71,7 +73,7 @@ public class ChangeLogLevelActionTest {
.setMethod("POST")
.execute();
- verify(serverLogging).changeLevel(LoggerLevel.DEBUG);
+ verify(serverLogging).changeLevel(webServerProcessLogging, LoggerLevel.DEBUG);
verify(ceHttpClient).changeLogLevel(LoggerLevel.DEBUG);
verify(db).enableSqlLogging(false);
}
@@ -85,7 +87,7 @@ public class ChangeLogLevelActionTest {
.setMethod("POST")
.execute();
- verify(serverLogging).changeLevel(LoggerLevel.TRACE);
+ verify(serverLogging).changeLevel(webServerProcessLogging, LoggerLevel.TRACE);
verify(ceHttpClient).changeLogLevel(LoggerLevel.TRACE);
verify(db).enableSqlLogging(true);
}