aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-server/src
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@sonarsource.com>2015-10-06 00:21:11 +0200
committerSimon Brandhof <simon.brandhof@sonarsource.com>2015-10-06 15:02:48 +0200
commitf9a7660fc36358f43dba067230e4b39736e46f9b (patch)
tree2a629c800724a4026ebe2cdac1f7ab25eff4079f /server/sonar-server/src
parent248208188e2da2983dc6c615218a1dc32131ca56 (diff)
downloadsonarqube-f9a7660fc36358f43dba067230e4b39736e46f9b.tar.gz
sonarqube-f9a7660fc36358f43dba067230e4b39736e46f9b.zip
SONAR-6830 ability to change level of SQL logs on the fly
Diffstat (limited to 'server/sonar-server/src')
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/platform/ServerLogging.java2
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/platform/ws/ChangeLogLevelAction.java15
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/platform/ServerLoggingTest.java2
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/platform/ws/ChangeLogLevelActionTest.java21
4 files changed, 30 insertions, 10 deletions
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 8df81ce901b..e79c1024c8e 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
@@ -49,7 +49,7 @@ public class ServerLogging {
}
public static void configureLevels(LogbackHelper helper, Level level) {
- Preconditions.checkArgument(ALLOWED_ROOT_LOG_LEVELS.contains(level), "Allowed log levels are %s", ALLOWED_ROOT_LOG_LEVELS);
+ Preconditions.checkArgument(ALLOWED_ROOT_LOG_LEVELS.contains(level), "%s log level is not supported (allowed levels are %s)", level, ALLOWED_ROOT_LOG_LEVELS);
helper.configureLogger(Logger.ROOT_LOGGER_NAME, level);
helper.configureLogger("rails", Level.WARN);
helper.configureLogger("org.apache.ibatis", Level.WARN);
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 59ebcc4fd9b..f024ed1bcd7 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
@@ -24,6 +24,7 @@ import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
import org.sonar.api.web.UserRole;
+import org.sonar.db.Database;
import org.sonar.server.platform.ServerLogging;
import org.sonar.server.user.UserSession;
@@ -33,22 +34,24 @@ public class ChangeLogLevelAction implements SystemWsAction {
private final UserSession userSession;
private final ServerLogging logging;
+ private final Database db;
- public ChangeLogLevelAction(UserSession userSession, ServerLogging logging) {
+ public ChangeLogLevelAction(UserSession userSession, ServerLogging logging, Database db) {
this.userSession = userSession;
this.logging = logging;
+ this.db = db;
}
@Override
public void define(WebService.NewController controller) {
WebService.NewAction newAction = controller.createAction("change_log_level")
- .setDescription("Changes temporarily level of logs. New level is not persistent and is lost " +
- "when restarting server.")
+ .setDescription("Temporarily changes level of logs. New level is not persistent and is lost " +
+ "when restarting server. Requires system administration permission.")
.setSince("5.2")
.setHandler(this);
newAction.createParam(PARAM_LEVEL)
- .setDescription("The new level. Warning - DEBUG and TRACE have performance impacts.")
+ .setDescription("The new level. Be cautious: DEBUG, and even more TRACE, may have performance impacts.")
.setPossibleValues(ServerLogging.ALLOWED_ROOT_LOG_LEVELS)
.setRequired(true);
}
@@ -56,7 +59,9 @@ public class ChangeLogLevelAction implements SystemWsAction {
@Override
public void handle(Request wsRequest, Response wsResponse) {
userSession.checkGlobalPermission(UserRole.ADMIN);
- logging.changeLevel(Level.toLevel(wsRequest.mandatoryParam(PARAM_LEVEL)));
+ Level level = Level.toLevel(wsRequest.mandatoryParam(PARAM_LEVEL));
+ db.enableSqlLogging(level.equals(Level.TRACE));
+ logging.changeLevel(level);
wsResponse.noContent();
}
}
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 6a0910d3239..08ed7edee54 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
@@ -77,7 +77,7 @@ public class ServerLoggingTest {
@Test
public void configureLevels_unsupported_level() {
expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Allowed log levels are [TRACE, DEBUG, INFO]");
+ expectedException.expectMessage("ERROR log level is not supported (allowed levels are [TRACE, DEBUG, INFO])");
LogbackHelper logbackHelper = mock(LogbackHelper.class);
ServerLogging.configureLevels(logbackHelper, Level.ERROR);
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 5357574c60f..21f72ce7db6 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
@@ -24,6 +24,7 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.web.UserRole;
+import org.sonar.db.Database;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.platform.ServerLogging;
import org.sonar.server.tester.UserSessionRule;
@@ -41,11 +42,12 @@ public class ChangeLogLevelActionTest {
public ExpectedException expectedException = ExpectedException.none();
ServerLogging serverLogging = mock(ServerLogging.class);
- ChangeLogLevelAction underTest = new ChangeLogLevelAction(userSession, serverLogging);
+ Database db = mock(Database.class);
+ ChangeLogLevelAction underTest = new ChangeLogLevelAction(userSession, serverLogging, db);
WsActionTester actionTester = new WsActionTester(underTest);
@Test
- public void change_level() {
+ public void enable_debug_logs() {
userSession.setGlobalPermissions(UserRole.ADMIN);
actionTester.newRequest()
@@ -53,10 +55,23 @@ public class ChangeLogLevelActionTest {
.setMethod("POST")
.execute();
verify(serverLogging).changeLevel(Level.DEBUG);
+ verify(db).enableSqlLogging(false);
}
@Test
- public void fail_if_bad_level() {
+ public void enable_trace_logs() {
+ userSession.setGlobalPermissions(UserRole.ADMIN);
+
+ actionTester.newRequest()
+ .setParam("level", "TRACE")
+ .setMethod("POST")
+ .execute();
+ verify(serverLogging).changeLevel(Level.TRACE);
+ verify(db).enableSqlLogging(true);
+ }
+
+ @Test
+ public void fail_if_unsupported_level() {
expectedException.expect(IllegalArgumentException.class);
userSession.setGlobalPermissions(UserRole.ADMIN);
actionTester.newRequest()