]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7168 add property to change log level of sonar-application 709/head
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Mon, 11 Jan 2016 08:34:12 +0000 (09:34 +0100)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Wed, 13 Jan 2016 12:42:44 +0000 (13:42 +0100)
property sonar.app.log.level must either be specifid as a command line argument with -D option or in wrapper.conf as a property such as "wrapper.java.additional.1=-Dsonar.app.log.level=TRACE"

sonar-application/src/main/java/org/sonar/application/AppLogging.java
sonar-application/src/test/java/org/sonar/application/AppLoggingTest.java

index c1c03bd221d7403de67f19ba3eab57c32a35b97e..25c2a3eecddd159dcd9763ea8498f98361daec1d 100644 (file)
@@ -50,7 +50,7 @@ class AppLogging {
     helper.enableJulChangePropagation(ctx);
     configureConsole(ctx);
     configureGobbler(props, ctx);
-    configureRoot(ctx);
+    configureRoot(props, ctx);
     if (props.valueAsBoolean("sonar.log.console", false)) {
       // used by SonarSource testing environment
       copyGobblerToConsole();
@@ -94,10 +94,10 @@ class AppLogging {
     gobblerLogger.addAppender(fileAppender);
   }
 
-  private void configureRoot(LoggerContext loggerContext) {
+  private void configureRoot(Props props, LoggerContext loggerContext) {
     ConsoleAppender<ILoggingEvent> consoleAppender = helper.newConsoleAppender(loggerContext, "ROOT_CONSOLE", APP_PATTERN, null);
     Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
-    rootLogger.setLevel(Level.INFO);
+    rootLogger.setLevel(Level.toLevel(props.value("sonar.app.log.level", Level.INFO.toString()), Level.INFO));
     rootLogger.addAppender(consoleAppender);
   }
 }
index 33686843a1a15cd4cd9f8a227688b05a39f3743b..172e6265a5a331feaf2921bf607dad17f2b8c282 100644 (file)
  */
 package org.sonar.application;
 
+import ch.qos.logback.classic.Level;
 import ch.qos.logback.classic.Logger;
 import ch.qos.logback.classic.LoggerContext;
 import ch.qos.logback.classic.spi.ILoggingEvent;
 import ch.qos.logback.core.Appender;
 import ch.qos.logback.core.FileAppender;
 import ch.qos.logback.core.rolling.RollingFileAppender;
+import java.io.File;
+import java.util.Properties;
 import org.junit.AfterClass;
 import org.junit.Before;
 import org.junit.Rule;
@@ -34,9 +37,6 @@ import org.sonar.process.LogbackHelper;
 import org.sonar.process.ProcessProperties;
 import org.sonar.process.Props;
 
-import java.io.File;
-import java.util.Properties;
-
 import static org.assertj.core.api.Assertions.assertThat;
 
 public class AppLoggingTest {
@@ -91,4 +91,38 @@ public class AppLoggingTest {
     assertThat(gobbler.getAppender(AppLogging.GOBBLER_APPENDER)).isNotNull();
     assertThat(gobbler.getAppender(AppLogging.CONSOLE_APPENDER)).isNotNull();
   }
+
+  @Test
+  public void default_level_for_root_logger_is_INFO() {
+    LoggerContext ctx = underTest.configure(props);
+    Logger rootLogger = ctx.getLogger(Logger.ROOT_LOGGER_NAME);
+    assertThat(rootLogger.getLevel()).isEqualTo(Level.INFO);
+  }
+
+  @Test
+  public void root_logger_level_can_be_changed_with_a_property() {
+    props.set("sonar.app.log.level", "TRACE");
+
+    LoggerContext ctx = underTest.configure(props);
+    Logger rootLogger = ctx.getLogger(Logger.ROOT_LOGGER_NAME);
+    assertThat(rootLogger.getLevel()).isEqualTo(Level.TRACE);
+  }
+
+  @Test
+  public void property_changing_root_logger_level_is_case_insensitive() {
+    props.set("sonar.app.log.level", "trace");
+
+    LoggerContext ctx = underTest.configure(props);
+    Logger rootLogger = ctx.getLogger(Logger.ROOT_LOGGER_NAME);
+    assertThat(rootLogger.getLevel()).isEqualTo(Level.TRACE);
+  }
+
+  @Test
+  public void default_to_INFO_if_property_changing_root_logger_level_has_invalid_value() {
+    props.set("sonar.app.log.level", "DodoDouh!");
+
+    LoggerContext ctx = underTest.configure(props);
+    Logger rootLogger = ctx.getLogger(Logger.ROOT_LOGGER_NAME);
+    assertThat(rootLogger.getLevel()).isEqualTo(Level.INFO);
+  }
 }