From 97fad28fd91b49710bd39217ad7f3929e0c2661e Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Mon, 16 Feb 2015 21:32:40 +0100 Subject: [PATCH] Support property sonar.log.debug (boolean) --- .../java/org/sonar/server/app/WebLogging.java | 12 +++++++----- .../main/java/org/sonar/server/app/WebServer.java | 5 +++-- .../java/org/sonar/server/app/WebLoggingTest.java | 15 ++++++++++++++- .../src/main/assembly/conf/sonar.properties | 2 ++ 4 files changed, 26 insertions(+), 8 deletions(-) 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/WebLogging.java index fa1d251f5c9..f2333792413 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/WebLogging.java @@ -25,6 +25,7 @@ import ch.qos.logback.classic.LoggerContext; import ch.qos.logback.core.ConsoleAppender; import org.slf4j.bridge.SLF4JBridgeHandler; import org.sonar.process.LogbackHelper; +import org.sonar.process.Props; import java.util.logging.LogManager; @@ -38,13 +39,13 @@ class WebLogging { private final LogbackHelper helper = new LogbackHelper(); - LoggerContext configure() { + LoggerContext configure(Props props) { LoggerContext ctx = helper.getRootContext(); ctx.reset(); helper.enableJulChangePropagation(ctx); configureAppender(ctx); - configureLevels(ctx); + configureLevels(ctx, props); // Configure java.util.logging, used by Tomcat, in order to forward to slf4j LogManager.getLogManager().reset(); @@ -54,11 +55,10 @@ class WebLogging { private void configureAppender(LoggerContext ctx) { ConsoleAppender consoleAppender = helper.newConsoleAppender(ctx, "CONSOLE", LOG_FORMAT); - Logger rootLogger = helper.configureLogger(ctx, Logger.ROOT_LOGGER_NAME, Level.INFO); - rootLogger.addAppender(consoleAppender); + ctx.getLogger(Logger.ROOT_LOGGER_NAME).addAppender(consoleAppender); } - private void configureLevels(LoggerContext ctx) { + private void configureLevels(LoggerContext ctx, Props props) { // override level of some loggers helper.configureLogger(ctx, "rails", Level.WARN); helper.configureLogger(ctx, "org.hibernate.cache.ReadWriteCache", Level.ERROR); @@ -72,5 +72,7 @@ class WebLogging { helper.configureLogger(ctx, "org.elasticsearch.node", Level.INFO); helper.configureLogger(ctx, "org.elasticsearch.http", Level.INFO); helper.configureLogger(ctx, "ch.qos.logback", Level.WARN); + boolean debug = props.valueAsBoolean("sonar.log.debug", false); + helper.configureLogger(ctx, Logger.ROOT_LOGGER_NAME, debug ? Level.DEBUG : Level.INFO); } } 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 724a5cdc5ea..cdd7fea2e13 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 @@ -60,8 +60,9 @@ public class WebServer implements Monitored { */ public static void main(String[] args) throws Exception { ProcessEntryPoint entryPoint = ProcessEntryPoint.createForArguments(args); - new WebLogging().configure(); - WebServer server = new WebServer(entryPoint.getProps()); + Props props = entryPoint.getProps(); + new WebLogging().configure(props); + WebServer server = new WebServer(props); entryPoint.launch(server); } } 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/WebLoggingTest.java index 467b58be54d..6aac3ce769e 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/WebLoggingTest.java @@ -27,11 +27,15 @@ import ch.qos.logback.core.ConsoleAppender; import org.junit.AfterClass; import org.junit.Test; import org.sonar.process.LogbackHelper; +import org.sonar.process.Props; + +import java.util.Properties; import static org.assertj.core.api.Assertions.assertThat; public class WebLoggingTest { + Props props = new Props(new Properties()); WebLogging sut = new WebLogging(); @AfterClass @@ -41,13 +45,22 @@ public class WebLoggingTest { @Test public void log_to_console() throws Exception { - LoggerContext ctx = sut.configure(); + LoggerContext ctx = sut.configure(props); Logger root = ctx.getLogger(Logger.ROOT_LOGGER_NAME); Appender appender = root.getAppender("CONSOLE"); assertThat(appender).isInstanceOf(ConsoleAppender.class); + // default level is INFO + assertThat(ctx.getLogger(Logger.ROOT_LOGGER_NAME).getLevel()).isEqualTo(Level.INFO); // change level of some loggers assertThat(ctx.getLogger("java.sql").getLevel()).isEqualTo(Level.WARN); } + + @Test + public void enable_debug_logs() throws Exception { + props.set("sonar.log.debug", "true"); + LoggerContext ctx = sut.configure(props); + assertThat(ctx.getLogger(Logger.ROOT_LOGGER_NAME).getLevel()).isEqualTo(Level.DEBUG); + } } diff --git a/sonar-application/src/main/assembly/conf/sonar.properties b/sonar-application/src/main/assembly/conf/sonar.properties index c2a1b29cf57..ed1499555a9 100644 --- a/sonar-application/src/main/assembly/conf/sonar.properties +++ b/sonar-application/src/main/assembly/conf/sonar.properties @@ -249,6 +249,8 @@ # and FULL (functional and technical details) #sonar.log.profilingLevel=NONE +#sonar.log.debug=false + # Path to log files. Can be absolute or relative to installation directory. # Default is /logs #sonar.path.logs=logs -- 2.39.5