aboutsummaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@sonarsource.com>2015-10-02 20:41:44 +0200
committerSimon Brandhof <simon.brandhof@sonarsource.com>2015-10-06 15:02:22 +0200
commit248208188e2da2983dc6c615218a1dc32131ca56 (patch)
tree4a6de73810061d62071643d509f4f565ed2d6dd2 /server
parent61388ef867ca83f58636499231105f78e4fbbbcb (diff)
downloadsonarqube-248208188e2da2983dc6c615218a1dc32131ca56.tar.gz
sonarqube-248208188e2da2983dc6c615218a1dc32131ca56.zip
SONAR-6830 add WS api/system/logs and api/system/change_log_level
Diffstat (limited to 'server')
-rw-r--r--server/sonar-process/src/main/java/org/sonar/process/LogbackHelper.java4
-rw-r--r--server/sonar-process/src/test/java/org/sonar/process/LogbackHelperTest.java4
-rw-r--r--server/sonar-search/src/main/java/org/sonar/search/SearchLogging.java2
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/app/EmbeddedTomcat.java5
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/app/ServerProcessLogging.java (renamed from server/sonar-server/src/main/java/org/sonar/server/app/WebLogging.java)19
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/app/TomcatStartupLogs.java (renamed from server/sonar-server/src/main/java/org/sonar/server/app/StartupLogs.java)4
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/app/WebServer.java2
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/platform/ServerLogging.java83
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java6
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/platform/ws/ChangeLogLevelAction.java62
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/platform/ws/LogsAction.java59
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/app/ServerProcessLoggingTest.java (renamed from server/sonar-server/src/test/java/org/sonar/server/app/WebLoggingTest.java)4
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/app/StartupLogsTest.java10
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/platform/ServerLoggingTest.java85
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/platform/ws/ChangeLogLevelActionTest.java83
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/platform/ws/LogsActionTest.java88
16 files changed, 488 insertions, 32 deletions
diff --git a/server/sonar-process/src/main/java/org/sonar/process/LogbackHelper.java b/server/sonar-process/src/main/java/org/sonar/process/LogbackHelper.java
index 0d5d3551872..5ff5e9b4560 100644
--- a/server/sonar-process/src/main/java/org/sonar/process/LogbackHelper.java
+++ b/server/sonar-process/src/main/java/org/sonar/process/LogbackHelper.java
@@ -90,8 +90,8 @@ public class LogbackHelper {
return consoleAppender;
}
- public Logger configureLogger(LoggerContext loggerContext, String loggerName, Level level) {
- Logger logger = loggerContext.getLogger(loggerName);
+ public Logger configureLogger(String loggerName, Level level) {
+ Logger logger = getRootContext().getLogger(loggerName);
logger.setLevel(level);
return logger;
}
diff --git a/server/sonar-process/src/test/java/org/sonar/process/LogbackHelperTest.java b/server/sonar-process/src/test/java/org/sonar/process/LogbackHelperTest.java
index 1cd17d936bf..cf710dcb06e 100644
--- a/server/sonar-process/src/test/java/org/sonar/process/LogbackHelperTest.java
+++ b/server/sonar-process/src/test/java/org/sonar/process/LogbackHelperTest.java
@@ -102,9 +102,7 @@ public class LogbackHelperTest {
@Test
public void configureLogger() {
- LoggerContext ctx = underTest.getRootContext();
-
- Logger logger = underTest.configureLogger(ctx, "my_logger", Level.WARN);
+ Logger logger = underTest.configureLogger("my_logger", Level.WARN);
assertThat(logger.getLevel()).isEqualTo(Level.WARN);
assertThat(logger.getName()).isEqualTo("my_logger");
diff --git a/server/sonar-search/src/main/java/org/sonar/search/SearchLogging.java b/server/sonar-search/src/main/java/org/sonar/search/SearchLogging.java
index 42c49f5c4ad..7909554d342 100644
--- a/server/sonar-search/src/main/java/org/sonar/search/SearchLogging.java
+++ b/server/sonar-search/src/main/java/org/sonar/search/SearchLogging.java
@@ -37,7 +37,7 @@ public class SearchLogging {
ctx.reset();
ConsoleAppender<ILoggingEvent> consoleAppender = helper.newConsoleAppender(ctx, "CONSOLE", LOG_FORMAT, null);
- Logger rootLogger = helper.configureLogger(ctx, Logger.ROOT_LOGGER_NAME, Level.INFO);
+ Logger rootLogger = helper.configureLogger(Logger.ROOT_LOGGER_NAME, Level.INFO);
rootLogger.addAppender(consoleAppender);
return ctx;
}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/app/EmbeddedTomcat.java b/server/sonar-server/src/main/java/org/sonar/server/app/EmbeddedTomcat.java
index 9cff7388191..f893939184f 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/app/EmbeddedTomcat.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/app/EmbeddedTomcat.java
@@ -20,6 +20,7 @@
package org.sonar.server.app;
import com.google.common.base.Throwables;
+import java.io.File;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
@@ -28,8 +29,6 @@ import org.sonar.api.utils.log.Loggers;
import org.sonar.process.ProcessProperties;
import org.sonar.process.Props;
-import java.io.File;
-
class EmbeddedTomcat {
private final Props props;
@@ -60,7 +59,7 @@ class EmbeddedTomcat {
webappContext = Webapp.configure(tomcat, props);
try {
tomcat.start();
- new StartupLogs(props, Loggers.get(getClass())).log(tomcat);
+ new TomcatStartupLogs(props, Loggers.get(getClass())).log(tomcat);
} catch (LifecycleException e) {
Throwables.propagate(e);
}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/app/WebLogging.java b/server/sonar-server/src/main/java/org/sonar/server/app/ServerProcessLogging.java
index 7fda6bd2099..5579a02487a 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/app/WebLogging.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/app/ServerProcessLogging.java
@@ -31,12 +31,13 @@ import org.sonar.process.LogbackHelper;
import org.sonar.process.Props;
import org.sonar.server.computation.log.CeLogDenyFilter;
import org.sonar.server.computation.log.CeLogging;
+import org.sonar.server.platform.ServerLogging;
/**
* Configure logback for web server process. Logs must be written to console, which is
* forwarded to file logs/sonar.log by the app master process.
*/
-class WebLogging {
+class ServerProcessLogging {
private static final String LOG_FORMAT = "%d{yyyy.MM.dd HH:mm:ss} %-5level web[%logger{20}] %msg%n";
public static final String LOG_LEVEL_PROPERTY = "sonar.log.level";
@@ -49,7 +50,7 @@ class WebLogging {
helper.enableJulChangePropagation(ctx);
configureAppender(ctx, props);
- configureLevels(ctx, props);
+ configureLevels(props);
// Configure java.util.logging, used by Tomcat, in order to forward to slf4j
LogManager.getLogManager().reset();
@@ -64,17 +65,7 @@ class WebLogging {
}
- private void configureLevels(LoggerContext ctx, Props props) {
- // override level of some loggers
- helper.configureLogger(ctx, "rails", Level.WARN);
- helper.configureLogger(ctx, "org.apache.ibatis", Level.WARN);
- helper.configureLogger(ctx, "java.sql", Level.WARN);
- helper.configureLogger(ctx, "java.sql.ResultSet", Level.WARN);
- helper.configureLogger(ctx, "org.sonar.MEASURE_FILTER", Level.WARN);
- helper.configureLogger(ctx, "org.elasticsearch", Level.INFO);
- helper.configureLogger(ctx, "org.elasticsearch.node", Level.INFO);
- helper.configureLogger(ctx, "org.elasticsearch.http", Level.INFO);
- helper.configureLogger(ctx, "ch.qos.logback", Level.WARN);
+ private void configureLevels(Props props) {
String levelCode = props.value(LOG_LEVEL_PROPERTY, "INFO");
Level level;
if ("TRACE".equals(levelCode)) {
@@ -86,6 +77,6 @@ class WebLogging {
} else {
throw MessageException.of(String.format("Unsupported log level: %s. Please check property %s", levelCode, LOG_LEVEL_PROPERTY));
}
- helper.configureLogger(ctx, Logger.ROOT_LOGGER_NAME, level);
+ ServerLogging.configureLevels(helper, level);
}
}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/app/StartupLogs.java b/server/sonar-server/src/main/java/org/sonar/server/app/TomcatStartupLogs.java
index abbce664d8e..181b79d5e9e 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/app/StartupLogs.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/app/TomcatStartupLogs.java
@@ -26,12 +26,12 @@ import org.apache.coyote.http11.AbstractHttp11JsseProtocol;
import org.sonar.api.utils.log.Logger;
import org.sonar.process.Props;
-class StartupLogs {
+class TomcatStartupLogs {
private final Logger log;
private final Props props;
- StartupLogs(Props props, Logger log) {
+ TomcatStartupLogs(Props props, Logger log) {
this.props = props;
this.log = log;
}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/app/WebServer.java b/server/sonar-server/src/main/java/org/sonar/server/app/WebServer.java
index cdd7fea2e13..2907102148f 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/app/WebServer.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/app/WebServer.java
@@ -61,7 +61,7 @@ public class WebServer implements Monitored {
public static void main(String[] args) throws Exception {
ProcessEntryPoint entryPoint = ProcessEntryPoint.createForArguments(args);
Props props = entryPoint.getProps();
- new WebLogging().configure(props);
+ new ServerProcessLogging().configure(props);
WebServer server = new WebServer(props);
entryPoint.launch(server);
}
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
new file mode 100644
index 00000000000..8df81ce901b
--- /dev/null
+++ b/server/sonar-server/src/main/java/org/sonar/server/platform/ServerLogging.java
@@ -0,0 +1,83 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.server.platform;
+
+import ch.qos.logback.classic.Level;
+import ch.qos.logback.classic.Logger;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableSet;
+import java.io.File;
+import java.util.Set;
+import org.slf4j.LoggerFactory;
+import org.sonar.api.config.Settings;
+import org.sonar.api.server.ServerSide;
+import org.sonar.process.LogbackHelper;
+import org.sonar.process.ProcessProperties;
+
+@ServerSide
+public class ServerLogging {
+
+ public static final Set<Level> ALLOWED_ROOT_LOG_LEVELS = ImmutableSet.of(Level.TRACE, Level.DEBUG, Level.INFO);
+
+ private final LogbackHelper helper = new LogbackHelper();
+ private final Settings settings;
+
+ public ServerLogging(Settings settings) {
+ this.settings = settings;
+ }
+
+ public void changeLevel(Level level) {
+ configureLevels(helper, level);
+ LoggerFactory.getLogger(ServerLogging.class).info("Level of logs changed to {}", level);
+ }
+
+ 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);
+ helper.configureLogger(Logger.ROOT_LOGGER_NAME, level);
+ 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.
+ */
+ public File getLogsDir() {
+ return new File(settings.getString(ProcessProperties.PATH_LOGS));
+ }
+
+ /**
+ * The file sonar.log, may not exist.
+ */
+ public File getCurrentLogFile() {
+ return new File(getLogsDir(), "sonar.log");
+ }
+
+}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java
index 6f9a1fe6638..b39c1b69986 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java
@@ -166,6 +166,7 @@ import org.sonar.server.permission.PermissionService;
import org.sonar.server.permission.PermissionUpdater;
import org.sonar.server.permission.ws.PermissionsWsModule;
import org.sonar.server.platform.BackendCleanup;
+import org.sonar.server.platform.ServerLogging;
import org.sonar.server.platform.SettingsChangeNotifier;
import org.sonar.server.platform.monitoring.DatabaseMonitor;
import org.sonar.server.platform.monitoring.EsMonitor;
@@ -173,9 +174,11 @@ import org.sonar.server.platform.monitoring.JvmPropertiesMonitor;
import org.sonar.server.platform.monitoring.PluginsMonitor;
import org.sonar.server.platform.monitoring.SonarQubeMonitor;
import org.sonar.server.platform.monitoring.SystemMonitor;
+import org.sonar.server.platform.ws.ChangeLogLevelAction;
import org.sonar.server.platform.ws.DbMigrationStatusAction;
import org.sonar.server.platform.ws.InfoAction;
import org.sonar.server.platform.ws.L10nWs;
+import org.sonar.server.platform.ws.LogsAction;
import org.sonar.server.platform.ws.MigrateDbAction;
import org.sonar.server.platform.ws.RestartAction;
import org.sonar.server.platform.ws.ServerWs;
@@ -668,6 +671,7 @@ public class PlatformLevel4 extends PlatformLevel {
TypeValidationModule.class,
// System
+ ServerLogging.class,
RestartAction.class,
InfoAction.class,
UpgradesAction.class,
@@ -680,6 +684,8 @@ public class PlatformLevel4 extends PlatformLevel {
JvmPropertiesMonitor.class,
DatabaseMonitor.class,
MigrateDbAction.class,
+ LogsAction.class,
+ ChangeLogLevelAction.class,
DbMigrationStatusAction.class,
// Plugins WS
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
new file mode 100644
index 00000000000..59ebcc4fd9b
--- /dev/null
+++ b/server/sonar-server/src/main/java/org/sonar/server/platform/ws/ChangeLogLevelAction.java
@@ -0,0 +1,62 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.server.platform.ws;
+
+import ch.qos.logback.classic.Level;
+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.server.platform.ServerLogging;
+import org.sonar.server.user.UserSession;
+
+public class ChangeLogLevelAction implements SystemWsAction {
+
+ private static final String PARAM_LEVEL = "level";
+
+ private final UserSession userSession;
+ private final ServerLogging logging;
+
+ public ChangeLogLevelAction(UserSession userSession, ServerLogging logging) {
+ this.userSession = userSession;
+ this.logging = logging;
+ }
+
+ @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.")
+ .setSince("5.2")
+ .setHandler(this);
+
+ newAction.createParam(PARAM_LEVEL)
+ .setDescription("The new level. Warning - DEBUG and TRACE have performance impacts.")
+ .setPossibleValues(ServerLogging.ALLOWED_ROOT_LOG_LEVELS)
+ .setRequired(true);
+ }
+
+ @Override
+ public void handle(Request wsRequest, Response wsResponse) {
+ userSession.checkGlobalPermission(UserRole.ADMIN);
+ logging.changeLevel(Level.toLevel(wsRequest.mandatoryParam(PARAM_LEVEL)));
+ wsResponse.noContent();
+ }
+}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/ws/LogsAction.java b/server/sonar-server/src/main/java/org/sonar/server/platform/ws/LogsAction.java
new file mode 100644
index 00000000000..bc76ea9a5cd
--- /dev/null
+++ b/server/sonar-server/src/main/java/org/sonar/server/platform/ws/LogsAction.java
@@ -0,0 +1,59 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.server.platform.ws;
+
+import java.io.File;
+import org.apache.commons.io.FileUtils;
+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.server.platform.ServerLogging;
+import org.sonar.server.plugins.MimeTypes;
+import org.sonar.server.user.UserSession;
+
+public class LogsAction implements SystemWsAction {
+
+ private final UserSession userSession;
+ private final ServerLogging serverLogging;
+
+ public LogsAction(UserSession userSession, ServerLogging serverLogging) {
+ this.userSession = userSession;
+ this.serverLogging = serverLogging;
+ }
+
+ @Override
+ public void define(WebService.NewController controller) {
+ controller.createAction("logs")
+ .setDescription("System logs in plain-text format. Requires system administration permission.")
+ .setSince("5.2")
+ .setHandler(this);
+ }
+
+ @Override
+ public void handle(Request wsRequest, Response wsResponse) throws Exception {
+ userSession.checkGlobalPermission(UserRole.ADMIN);
+ wsResponse.stream().setMediaType(MimeTypes.TXT);
+ File file = serverLogging.getCurrentLogFile();
+ if (file.exists()) {
+ FileUtils.copyFile(file, wsResponse.stream().output());
+ }
+ }
+}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/app/WebLoggingTest.java b/server/sonar-server/src/test/java/org/sonar/server/app/ServerProcessLoggingTest.java
index 80bfad290b0..c05ad6afbca 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/app/WebLoggingTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/app/ServerProcessLoggingTest.java
@@ -38,13 +38,13 @@ import org.sonar.process.Props;
import static org.assertj.core.api.Assertions.assertThat;
-public class WebLoggingTest {
+public class ServerProcessLoggingTest {
@Rule
public TemporaryFolder temp = new TemporaryFolder();
Props props = new Props(new Properties());
- WebLogging underTest = new WebLogging();
+ ServerProcessLogging underTest = new ServerProcessLogging();
/**
* Path to data dir must be set for Compute Engine logging.
diff --git a/server/sonar-server/src/test/java/org/sonar/server/app/StartupLogsTest.java b/server/sonar-server/src/test/java/org/sonar/server/app/StartupLogsTest.java
index fb617c4b64b..f7946c3dfdf 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/app/StartupLogsTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/app/StartupLogsTest.java
@@ -19,6 +19,7 @@
*/
package org.sonar.server.app;
+import java.util.Properties;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.startup.Tomcat;
import org.apache.coyote.http11.AbstractHttp11JsseProtocol;
@@ -27,17 +28,18 @@ import org.mockito.Mockito;
import org.sonar.api.utils.log.Logger;
import org.sonar.process.Props;
-import java.util.Properties;
-
import static org.junit.Assert.fail;
-import static org.mockito.Mockito.*;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.when;
public class StartupLogsTest {
Tomcat tomcat = mock(Tomcat.class, Mockito.RETURNS_DEEP_STUBS);
Logger logger = mock(Logger.class);
Props props = new Props(new Properties());
- StartupLogs underTest = new StartupLogs(props, logger);
+ TomcatStartupLogs underTest = new TomcatStartupLogs(props, logger);
@Test
public void logAjp() {
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
new file mode 100644
index 00000000000..6a0910d3239
--- /dev/null
+++ b/server/sonar-server/src/test/java/org/sonar/server/platform/ServerLoggingTest.java
@@ -0,0 +1,85 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.server.platform;
+
+import ch.qos.logback.classic.Level;
+import ch.qos.logback.classic.Logger;
+import java.io.File;
+import java.io.IOException;
+import org.apache.commons.io.FileUtils;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.rules.TemporaryFolder;
+import org.sonar.api.config.Settings;
+import org.sonar.process.LogbackHelper;
+import org.sonar.process.ProcessProperties;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+public class ServerLoggingTest {
+
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
+ Settings settings = new Settings();
+ ServerLogging underTest = new ServerLogging(settings);
+
+ @Test
+ public void getLogsDir() throws IOException {
+ File dir = temp.newFolder();
+ settings.setProperty(ProcessProperties.PATH_LOGS, dir.getAbsolutePath());
+
+ assertThat(underTest.getLogsDir()).isEqualTo(dir);
+ }
+
+ @Test
+ public void getCurrentLogFile() throws IOException {
+ File dir = temp.newFolder();
+ File logFile = new File(dir, "sonar.log");
+ FileUtils.touch(logFile);
+ settings.setProperty(ProcessProperties.PATH_LOGS, dir.getAbsolutePath());
+
+ assertThat(underTest.getCurrentLogFile()).isEqualTo(logFile);
+ }
+
+ @Test
+ public void configureLevels() {
+ LogbackHelper logbackHelper = mock(LogbackHelper.class);
+ ServerLogging.configureLevels(logbackHelper, Level.TRACE);
+
+ verify(logbackHelper).configureLogger(Logger.ROOT_LOGGER_NAME, Level.TRACE);
+ verify(logbackHelper).configureLogger("java.sql", Level.WARN);
+ }
+
+ @Test
+ public void configureLevels_unsupported_level() {
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage("Allowed log 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
new file mode 100644
index 00000000000..5357574c60f
--- /dev/null
+++ b/server/sonar-server/src/test/java/org/sonar/server/platform/ws/ChangeLogLevelActionTest.java
@@ -0,0 +1,83 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.server.platform.ws;
+
+import ch.qos.logback.classic.Level;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.web.UserRole;
+import org.sonar.server.exceptions.ForbiddenException;
+import org.sonar.server.platform.ServerLogging;
+import org.sonar.server.tester.UserSessionRule;
+import org.sonar.server.ws.WsActionTester;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+public class ChangeLogLevelActionTest {
+
+ @Rule
+ public UserSessionRule userSession = UserSessionRule.standalone();
+
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
+ ServerLogging serverLogging = mock(ServerLogging.class);
+ ChangeLogLevelAction underTest = new ChangeLogLevelAction(userSession, serverLogging);
+ WsActionTester actionTester = new WsActionTester(underTest);
+
+ @Test
+ public void change_level() {
+ userSession.setGlobalPermissions(UserRole.ADMIN);
+
+ actionTester.newRequest()
+ .setParam("level", "DEBUG")
+ .setMethod("POST")
+ .execute();
+ verify(serverLogging).changeLevel(Level.DEBUG);
+ }
+
+ @Test
+ public void fail_if_bad_level() {
+ expectedException.expect(IllegalArgumentException.class);
+ userSession.setGlobalPermissions(UserRole.ADMIN);
+ actionTester.newRequest()
+ .setParam("level", "ERROR")
+ .setMethod("POST")
+ .execute();
+ }
+
+ @Test
+ public void fail_if_missing_level() {
+ expectedException.expect(IllegalArgumentException.class);
+ userSession.setGlobalPermissions(UserRole.ADMIN);
+ actionTester.newRequest()
+ .setMethod("POST")
+ .execute();
+ }
+
+ @Test
+ public void requires_admin_permission() {
+ expectedException.expect(ForbiddenException.class);
+
+ actionTester.newRequest().setMethod("POST").execute();
+ }
+}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/ws/LogsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/ws/LogsActionTest.java
new file mode 100644
index 00000000000..168d0a584b3
--- /dev/null
+++ b/server/sonar-server/src/test/java/org/sonar/server/platform/ws/LogsActionTest.java
@@ -0,0 +1,88 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.server.platform.ws;
+
+import java.io.File;
+import java.io.IOException;
+import org.apache.commons.io.FileUtils;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.rules.TemporaryFolder;
+import org.sonar.api.web.UserRole;
+import org.sonar.server.exceptions.ForbiddenException;
+import org.sonar.server.platform.ServerLogging;
+import org.sonar.server.plugins.MimeTypes;
+import org.sonar.server.tester.UserSessionRule;
+import org.sonar.server.ws.TestResponse;
+import org.sonar.server.ws.WsActionTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class LogsActionTest {
+
+ @Rule
+ public UserSessionRule userSession = UserSessionRule.standalone();
+
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
+ ServerLogging serverLogging = mock(ServerLogging.class);
+ LogsAction underTest = new LogsAction(userSession, serverLogging);
+ WsActionTester actionTester = new WsActionTester(underTest);
+
+ @Test
+ public void get_logs() throws IOException {
+ userSession.setGlobalPermissions(UserRole.ADMIN);
+
+ File file = temp.newFile();
+ FileUtils.write(file, "{logs}");
+ when(serverLogging.getCurrentLogFile()).thenReturn(file);
+
+ TestResponse response = actionTester.newRequest().execute();
+ assertThat(response.getMediaType()).isEqualTo(MimeTypes.TXT);
+ assertThat(response.getInput()).isEqualTo("{logs}");
+ }
+
+ @Test
+ public void get_empty_logs_if_file_does_not_exist() throws IOException {
+ userSession.setGlobalPermissions(UserRole.ADMIN);
+
+ File file = temp.newFile();
+ file.delete();
+ when(serverLogging.getCurrentLogFile()).thenReturn(file);
+
+ TestResponse response = actionTester.newRequest().execute();
+ assertThat(response.getMediaType()).isEqualTo(MimeTypes.TXT);
+ assertThat(response.getInput()).isEqualTo("");
+ }
+
+ @Test
+ public void requires_admin_permission() {
+ expectedException.expect(ForbiddenException.class);
+
+ actionTester.newRequest().execute();
+ }
+}