diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2014-07-25 14:15:33 +0200 |
---|---|---|
committer | Stephane Gamard <stephane.gamard@searchbox.com> | 2014-07-25 15:44:19 +0200 |
commit | 83d4e04cfd57d992e0f67da2cfe06260e413408f (patch) | |
tree | 65113dc1cb03dbc7347a9ed65f12781096cd3e04 | |
parent | 94ca842bc165eeb9001b04bc540e229df34163b2 (diff) | |
download | sonarqube-83d4e04cfd57d992e0f67da2cfe06260e413408f.tar.gz sonarqube-83d4e04cfd57d992e0f67da2cfe06260e413408f.zip |
SONAR-4898 improve logging configuration
6 files changed, 79 insertions, 82 deletions
diff --git a/server/sonar-process/src/main/java/org/sonar/process/ConfigurationUtils.java b/server/sonar-process/src/main/java/org/sonar/process/ConfigurationUtils.java index 91caca0a2e7..5430aa26173 100644 --- a/server/sonar-process/src/main/java/org/sonar/process/ConfigurationUtils.java +++ b/server/sonar-process/src/main/java/org/sonar/process/ConfigurationUtils.java @@ -19,8 +19,12 @@ */ package org.sonar.process; +import org.apache.commons.io.IOUtils; import org.apache.commons.lang.text.StrSubstitutor; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; import java.util.Enumeration; import java.util.Map; import java.util.Properties; @@ -42,4 +46,28 @@ public final class ConfigurationUtils { } return result; } + + public static Props loadPropsFromCommandLineArgs(String[] args) { + if (args.length != 1) { + throw new IllegalStateException("Only a single command-line argument is accepted " + + "(absolute path to configuration file)"); + } + + File propertyFile = new File(args[0]); + if (!propertyFile.exists()) { + throw new IllegalStateException("Property file '" + args[0] + "' does not exist! "); + } + + Properties properties = new Properties(); + FileReader reader = null; + try { + reader = new FileReader(propertyFile); + properties.load(reader); + } catch (IOException e) { + throw new IllegalStateException("Could not read properties from file '" + args[0] + "'", e); + } finally { + IOUtils.closeQuietly(reader); + } + return new Props(properties); + } } diff --git a/server/sonar-process/src/main/java/org/sonar/process/Process.java b/server/sonar-process/src/main/java/org/sonar/process/Process.java index 2c0b865bf0c..ddec37d7590 100644 --- a/server/sonar-process/src/main/java/org/sonar/process/Process.java +++ b/server/sonar-process/src/main/java/org/sonar/process/Process.java @@ -19,7 +19,6 @@ */ package org.sonar.process; -import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -30,12 +29,7 @@ import javax.management.MBeanServer; import javax.management.MalformedObjectNameException; import javax.management.NotCompliantMBeanException; import javax.management.ObjectName; - -import java.io.File; -import java.io.FileReader; -import java.io.IOException; import java.lang.management.ManagementFactory; -import java.util.Properties; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; @@ -70,32 +64,7 @@ public abstract class Process implements ProcessMXBean { } }; - public Process(String[] args) { - // loading arguments from file and system. - if (args.length < 1) { - throw new IllegalStateException("Process is missing argument!"); - } - - File propertyFile = new File(args[0]); - if (!propertyFile.exists()) { - throw new IllegalStateException("Property file '" + args[0] + "' does not exist! "); - } - - Properties properties = new Properties(); - FileReader reader = null; - try { - reader = new FileReader(propertyFile); - properties.load(reader); - } catch (IOException e) { - throw new IllegalStateException("Could not read properties from file '" + args[0] + "'", e); - } finally { - IOUtils.closeQuietly(reader); - } - props = new Props(properties); - init(); - } - - public Process(Props props) { + protected Process(Props props) { this.props = props; init(); } diff --git a/server/sonar-search/src/main/java/org/sonar/search/ElasticSearch.java b/server/sonar-search/src/main/java/org/sonar/search/ElasticSearch.java index 2b71844183c..1ea426d399e 100644 --- a/server/sonar-search/src/main/java/org/sonar/search/ElasticSearch.java +++ b/server/sonar-search/src/main/java/org/sonar/search/ElasticSearch.java @@ -25,6 +25,7 @@ import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.node.Node; import org.elasticsearch.node.NodeBuilder; import org.slf4j.LoggerFactory; +import org.sonar.process.ConfigurationUtils; import org.sonar.process.Process; import org.sonar.process.Props; import org.sonar.search.script.ListUpdate; @@ -39,11 +40,7 @@ public class ElasticSearch extends Process { private Node node; - public ElasticSearch(String... args) { - super(args); - } - - public ElasticSearch(Props props) { + ElasticSearch(Props props) { super(props); } @@ -56,7 +53,6 @@ public class ElasticSearch extends Process { .get() .getStatus() != ClusterHealthStatus.RED); } catch (Exception e) { - //LOGGER.warn("ES is not ready yet.", e); return false; } } @@ -177,8 +173,7 @@ public class ElasticSearch extends Process { } public static void main(String... args) throws InterruptedException { - final ElasticSearch elasticSearch = new ElasticSearch(args); - elasticSearch.start(); - LOGGER.info("ElasticSearch is done."); + Props props = ConfigurationUtils.loadPropsFromCommandLineArgs(args); + new ElasticSearch(props).start(); } } diff --git a/server/sonar-search/src/test/resources/logback-test.xml b/server/sonar-search/src/test/resources/logback-test.xml new file mode 100644 index 00000000000..ff2270cc122 --- /dev/null +++ b/server/sonar-search/src/test/resources/logback-test.xml @@ -0,0 +1,38 @@ +<?xml version="1.0" encoding="UTF-8" ?> + +<!-- + Configuration for default logger. Only used while embedded server is starting, + before proper logging configuration is loaded. + + See http://logback.qos.ch/manual/configuration.html +--> +<configuration debug="false"> + <contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator"/> + + <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> + <filter class="ch.qos.logback.classic.filter.LevelFilter"> + <level>INFO</level> + <onMatch>ACCEPT</onMatch> + <onMismatch>DENY</onMismatch> + </filter> + <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> + <pattern> + %d{yyyy.MM.dd HH:mm:ss} %-5level %msg%n + </pattern> + </encoder> + </appender> + + <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> + <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> + <pattern> + %d{yyyy.MM.dd HH:mm:ss} %-5level %msg%n + </pattern> + </encoder> + </appender> + + <root> + <level value="DEBUG"/> + <appender-ref ref="CONSOLE"/> + </root> + +</configuration> diff --git a/server/sonar-server/src/main/java/org/sonar/server/app/ServerProcess.java b/server/sonar-server/src/main/java/org/sonar/server/app/ServerProcess.java index 863bb03f4fe..130bfa233f1 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/app/ServerProcess.java +++ b/server/sonar-server/src/main/java/org/sonar/server/app/ServerProcess.java @@ -19,13 +19,15 @@ */ package org.sonar.server.app; +import org.sonar.process.ConfigurationUtils; +import org.sonar.process.Props; + public class ServerProcess extends org.sonar.process.Process { private final EmbeddedTomcat tomcat; - public ServerProcess(String[] args) { - super(args); - Logging.init(props); + ServerProcess(Props props) { + super(props); this.tomcat = new EmbeddedTomcat(props); } @@ -45,7 +47,9 @@ public class ServerProcess extends org.sonar.process.Process { } public static void main(String[] args) { - new ServerProcess(args).start(); + Props props = ConfigurationUtils.loadPropsFromCommandLineArgs(args); + Logging.init(props); + new ServerProcess(props).start(); LOGGER.info("ServerProcess is done"); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/PlatformServletContextListener.java b/server/sonar-server/src/main/java/org/sonar/server/platform/PlatformServletContextListener.java index 80177dc82cc..0c615c531cb 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/PlatformServletContextListener.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/PlatformServletContextListener.java @@ -19,39 +19,18 @@ */ package org.sonar.server.platform; -import com.google.common.collect.ImmutableMap; import org.slf4j.LoggerFactory; -import org.sonar.core.config.Logback; -import org.sonar.core.profiling.Profiling; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import java.util.Enumeration; -import java.util.Map; import java.util.Properties; -import static org.apache.commons.lang.StringUtils.defaultIfEmpty; - public final class PlatformServletContextListener implements ServletContextListener { - private static final String CONFIG_LOG_CONSOLE = "sonar.log.console"; - - private static final String LOG_COMMON_PREFIX = "%d{yyyy.MM.dd HH:mm:ss} %-5level "; - private static final String LOG_COMMON_SUFFIX = "%msg%n"; - - private static final String LOG_LOGFILE_SPECIFIC_PART = "[%logger{20}] %X "; - private static final String LOG_FULL_SPECIFIC_PART = "%thread "; - - private static final String LOGFILE_STANDARD_LOGGING_FORMAT = LOG_COMMON_PREFIX + LOG_LOGFILE_SPECIFIC_PART + LOG_COMMON_SUFFIX; - private static final String LOGFILE_FULL_LOGGING_FORMAT = LOG_COMMON_PREFIX + LOG_FULL_SPECIFIC_PART + LOG_LOGFILE_SPECIFIC_PART + LOG_COMMON_SUFFIX; - - private static final String CONSOLE_STANDARD_LOGGING_FORMAT = LOG_COMMON_PREFIX + LOG_COMMON_SUFFIX; - private static final String CONSOLE_FULL_LOGGING_FORMAT = LOG_COMMON_PREFIX + LOG_FULL_SPECIFIC_PART + LOG_COMMON_SUFFIX; - public void contextInitialized(ServletContextEvent event) { try { - //configureLogback(event); Properties props = new Properties(); ServletContext context = event.getServletContext(); Enumeration<String> paramKeys = context.getInitParameterNames(); @@ -85,20 +64,4 @@ public final class PlatformServletContextListener implements ServletContextListe public void contextDestroyed(ServletContextEvent event) { Platform.getInstance().doStop(); } - - /** - * Configure Logback from classpath, with configuration from sonar.properties - */ - private void configureLogback(ServletContextEvent event) { - String configProfilingLevel = defaultIfEmpty( - event.getServletContext().getInitParameter(Profiling.CONFIG_PROFILING_LEVEL), "NONE"); - Profiling.Level profilingLevel = Profiling.Level.fromConfigString(configProfilingLevel); - String consoleEnabled = defaultIfEmpty( - event.getServletContext().getInitParameter(CONFIG_LOG_CONSOLE), "false"); - Map<String, String> variables = ImmutableMap.of( - "LOGFILE_LOGGING_FORMAT", profilingLevel == Profiling.Level.FULL ? LOGFILE_FULL_LOGGING_FORMAT : LOGFILE_STANDARD_LOGGING_FORMAT, - "CONSOLE_LOGGING_FORMAT", profilingLevel == Profiling.Level.FULL ? CONSOLE_FULL_LOGGING_FORMAT : CONSOLE_STANDARD_LOGGING_FORMAT, - "CONSOLE_ENABLED", consoleEnabled); - Logback.configure("/org/sonar/server/platform/logback.xml", variables); - } } |