diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2014-07-24 15:33:40 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2014-07-24 15:33:40 +0200 |
commit | 3ed2d3a1f48abbed2ff72650c0399d9534bc5601 (patch) | |
tree | 6707421e2cfaafd805b577006096fe5f0ccd0400 /sonar-application/src/main/java/org/sonar/application | |
parent | fd7ca1c8d84fcaf0cf998061e1a313937dcb9b06 (diff) | |
download | sonarqube-3ed2d3a1f48abbed2ff72650c0399d9534bc5601.tar.gz sonarqube-3ed2d3a1f48abbed2ff72650c0399d9534bc5601.zip |
SONAR-4898 improve logging
Diffstat (limited to 'sonar-application/src/main/java/org/sonar/application')
4 files changed, 60 insertions, 25 deletions
diff --git a/sonar-application/src/main/java/org/sonar/application/AppLogging.java b/sonar-application/src/main/java/org/sonar/application/AppLogging.java new file mode 100644 index 00000000000..1d0b576ecab --- /dev/null +++ b/sonar-application/src/main/java/org/sonar/application/AppLogging.java @@ -0,0 +1,44 @@ +/* + * 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.application; + +import ch.qos.logback.classic.LoggerContext; +import ch.qos.logback.classic.joran.JoranConfigurator; +import ch.qos.logback.core.joran.spi.JoranException; +import ch.qos.logback.core.util.StatusPrinter; +import org.slf4j.LoggerFactory; + +class AppLogging { + + void configure(Installation installation) { + LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); + try { + context.putProperty(DefaultSettings.PATH_LOGS_KEY, installation.logsDir().getAbsolutePath()); + JoranConfigurator configurator = new JoranConfigurator(); + configurator.setContext(context); + context.reset(); + configurator.doConfigure(getClass().getResource("/org/sonar/application/logback.xml")); + } catch (JoranException je) { + // StatusPrinter will handle this + } + StatusPrinter.printInCaseOfErrorsOrWarnings(context); + + } +} diff --git a/sonar-application/src/main/java/org/sonar/application/DefaultSettings.java b/sonar-application/src/main/java/org/sonar/application/DefaultSettings.java index 22d31ddef17..eb414f71272 100644 --- a/sonar-application/src/main/java/org/sonar/application/DefaultSettings.java +++ b/sonar-application/src/main/java/org/sonar/application/DefaultSettings.java @@ -28,6 +28,8 @@ class DefaultSettings { // only static stuff } + static final String PATH_LOGS_KEY = "sonar.path.logs"; + static final String ES_PORT_KEY = "sonar.es.port"; private static final int ES_PORT_DEFVAL = 9001; diff --git a/sonar-application/src/main/java/org/sonar/application/Installation.java b/sonar-application/src/main/java/org/sonar/application/Installation.java index 2705a3d58fe..35142b46e11 100644 --- a/sonar-application/src/main/java/org/sonar/application/Installation.java +++ b/sonar-application/src/main/java/org/sonar/application/Installation.java @@ -22,12 +22,11 @@ package org.sonar.application; import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.IOUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.sonar.process.Props; import javax.annotation.CheckForNull; import javax.annotation.Nullable; + import java.io.File; import java.io.FileReader; import java.io.IOException; @@ -35,8 +34,6 @@ import java.net.URISyntaxException; import java.util.Properties; public class Installation { - private static final Logger LOG = LoggerFactory.getLogger(Installation.class); - private final File homeDir; private final File tempDir, logsDir; private final Props props; @@ -50,7 +47,7 @@ public class Installation { // init file system initExistingDir("sonar.path.data", "data"); - initExistingDir("sonar.path.web", "lib/web"); + initExistingDir("sonar.path.web", "web"); this.tempDir = initTempDir("sonar.path.temp", "temp"); this.logsDir = initExistingDir("sonar.path.logs", "logs"); } @@ -73,8 +70,6 @@ public class Installation { } finally { IOUtils.closeQuietly(reader); } - } else { - LOG.info("Configuration file not found: " + propsFile.getAbsolutePath()); } p.putAll(System.getenv()); p.putAll(System.getProperties()); diff --git a/sonar-application/src/main/java/org/sonar/application/StartServer.java b/sonar-application/src/main/java/org/sonar/application/StartServer.java index ebdd4e7fa02..80076761497 100644 --- a/sonar-application/src/main/java/org/sonar/application/StartServer.java +++ b/sonar-application/src/main/java/org/sonar/application/StartServer.java @@ -19,7 +19,6 @@ */ package org.sonar.application; -import com.google.common.annotations.VisibleForTesting; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.process.Monitor; @@ -32,6 +31,7 @@ import javax.management.InstanceAlreadyExistsException; import javax.management.MBeanRegistrationException; import javax.management.MBeanServer; import javax.management.NotCompliantMBeanException; + import java.lang.management.ManagementFactory; public class StartServer implements ProcessMXBean { @@ -45,11 +45,6 @@ public class StartServer implements ProcessMXBean { private static Logger LOGGER = LoggerFactory.getLogger(StartServer.class); - public StartServer() throws Exception { - this(new Installation()); - } - - @VisibleForTesting StartServer(Installation installation) throws Exception { this.installation = installation; @@ -75,17 +70,15 @@ public class StartServer implements ProcessMXBean { } monitor = new Monitor(); - } - private void start(){ - + private void start() { elasticsearch = new ProcessWrapper("ES") .setWorkDir(installation.homeDir()) .setJmxPort(Integer.parseInt(installation.prop(DefaultSettings.ES_JMX_PORT_KEY))) .addJavaOpts(installation.prop(DefaultSettings.ES_JAVA_OPTS_KEY)) - .addJavaOpts("-Djava.io.tmpdir=" + installation.tempDir().getAbsolutePath()) - .addJavaOpts("-Dsonar.path.logs=" + installation.logsDir().getAbsolutePath()) + .addJavaOpts(String.format("-Djava.io.tmpdir=%s", installation.tempDir().getAbsolutePath())) + .addJavaOpts(String.format("-D%s=%s", DefaultSettings.PATH_LOGS_KEY, installation.logsDir().getAbsolutePath())) .setClassName("org.sonar.search.ElasticSearch") .setProperties(installation.props().cryptedProperties()) .addClasspath(installation.starPath("lib/common")) @@ -93,14 +86,13 @@ public class StartServer implements ProcessMXBean { .execute(); monitor.registerProcess(elasticsearch); - server = new ProcessWrapper("SQ") .setWorkDir(installation.homeDir()) .setJmxPort(Integer.parseInt(installation.prop(DefaultSettings.WEB_JMX_PORT_KEY))) .addJavaOpts(installation.prop(DefaultSettings.WEB_JAVA_OPTS_KEY)) .addJavaOpts(DefaultSettings.WEB_JAVA_OPTS_APPENDED_VAL) - .addJavaOpts("-Djava.io.tmpdir=" + installation.tempDir().getAbsolutePath()) - .addJavaOpts("-Dsonar.path.logs=" + installation.logsDir().getAbsolutePath()) + .addJavaOpts(String.format("-Djava.io.tmpdir=%s", installation.tempDir().getAbsolutePath())) + .addJavaOpts(String.format("-D%s=%s", DefaultSettings.PATH_LOGS_KEY, installation.logsDir().getAbsolutePath())) .setClassName("org.sonar.server.app.ServerProcess") .setProperties(installation.props().cryptedProperties()) .addClasspath(installation.starPath("extensions/jdbc-driver/mysql")) @@ -153,10 +145,6 @@ public class StartServer implements ProcessMXBean { } } - public static void main(String[] args) throws Exception { - new StartServer().start(); - } - @Override public boolean isReady() { return monitor.isAlive(); @@ -166,4 +154,10 @@ public class StartServer implements ProcessMXBean { public long ping() { return System.currentTimeMillis(); } + + public static void main(String[] args) throws Exception { + Installation installation = new Installation(); + new AppLogging().configure(installation); + new StartServer(installation).start(); + } } |