Browse Source

SONAR-2959 Add the property sonar.showSql

tags/2.14
Simon Brandhof 12 years ago
parent
commit
7be3485cc1

+ 107
- 0
sonar-batch/src/main/java/org/sonar/batch/bootstrapper/LoggingConfiguration.java View File

@@ -0,0 +1,107 @@
/*
* Sonar, open source software quality management tool.
* Copyright (C) 2008-2012 SonarSource
* mailto:contact AT sonarsource DOT com
*
* Sonar 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.
*
* Sonar 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 Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package org.sonar.batch.bootstrapper;

import org.apache.commons.lang.StringUtils;
import org.sonar.core.config.Logback;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
* @since 2.14
*/
public final class LoggingConfiguration {

public static final String PROPERTY_ROOT_LOGGER_LEVEL = "ROOT_LOGGER_LEVEL";
public static final String PROPERTY_SQL_LOGGER_LEVEL = "SQL_LOGGER_LEVEL";
public static final String PROPERTY_FORMAT = "FORMAT";

public static final String LEVEL_ROOT_VERBOSE = "DEBUG";
public static final String LEVEL_ROOT_DEFAULT = "INFO";
public static final String LEVEL_SQL_VERBOSE = "DEBUG";
public static final String LEVEL_SQL_DEFAULT = "WARN";

public static final String FORMAT_DEFAULT = "%d{HH:mm:ss.SSS} %-5level - %msg%n";
public static final String FORMAT_MAVEN = "[%level] [%d{HH:mm:ss.SSS}] %msg%n";

private Map<String, String> substitutionVariables = new HashMap<String, String>();

private LoggingConfiguration() {
setVerbose(false);
setShowSql(false);
setFormat(FORMAT_DEFAULT);
}

public static LoggingConfiguration create() {
return new LoggingConfiguration();
}

public LoggingConfiguration setProperties(Map<String, String> properties) {
setShowSql("true".equals(properties.get("sonar.showSql")));
setVerbose("true".equals(properties.get("sonar.verbose")));
return this;
}

public LoggingConfiguration setVerbose(boolean verbose) {
return setRootLevel(verbose ? LEVEL_ROOT_VERBOSE : LEVEL_ROOT_DEFAULT);
}

public LoggingConfiguration setShowSql(boolean showSql) {
return setSqlLevel(showSql ? LEVEL_SQL_VERBOSE : LEVEL_SQL_DEFAULT);
}

public LoggingConfiguration setRootLevel(String level) {
return addSubstitutionVariable(PROPERTY_ROOT_LOGGER_LEVEL, level);
}

public LoggingConfiguration setSqlLevel(String level) {
return addSubstitutionVariable(PROPERTY_SQL_LOGGER_LEVEL, level);
}

public LoggingConfiguration setFormat(String format) {
return addSubstitutionVariable(PROPERTY_FORMAT, StringUtils.defaultIfBlank(format, FORMAT_DEFAULT));
}

public LoggingConfiguration addSubstitutionVariable(String key, String value) {
substitutionVariables.put(key, value);
return this;
}

String getSubstitutionVariable(String key) {
return substitutionVariables.get(key);
}

public LoggingConfiguration configure(String classloaderPath) {
Logback.configure(classloaderPath, substitutionVariables);
return this;
}

public LoggingConfiguration configure(File logbackFile) {
Logback.configure(logbackFile, substitutionVariables);
return this;
}

public LoggingConfiguration configure() {
Logback.configure("/org/sonar/batch/bootstrapper/logback.xml", substitutionVariables);
return this;
}
}

sonar-maven-plugin/src/main/resources/org/sonar/maven/logback.xml → sonar-batch/src/main/resources/org/sonar/batch/bootstrapper/logback.xml View File

@@ -1,40 +1,56 @@
<?xml version="1.0" encoding="UTF-8" ?>
<configuration debug="false">

<!--
This file is loaded by bootstrappers like Ant Task and Java Runner.

Reasons to NOT move this configuration to bootstrappers:
- same lifecycle as sonar -> loggers are always up-to-date. No need to think about ascending/descending compatibility.
- parameters can be added without releasing new versions of bootstrappers
- XML format is up-to-date toward the version of Logback.

-->

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>[%level] [%d{HH:mm:ss.SSS}] %msg%n</pattern>
<pattern>${FORMAT}</pattern>
</encoder>
</appender>

<logger name="org.hibernate">
<logger name="net.sf.ehcache">
<level value="WARN"/>
</logger>

<!-- set DEBUG to activate SQL logs. NOT RECOMMENDED -->
<logger name="org.hibernate.SQL">
<logger name="org.hibernate.cache.ReadWriteCache">
<!-- removing "An item was expired by the cache while it was locked (increase your cache timeout)" msg -->
<level value="ERROR"/>
</logger>
<logger name="org.hibernate.cache.EhCacheProvider">
<!-- removing "org.hibernate.cache.EhCacheProvider - Could not find configuratio)" message -->
<level value="ERROR"/>
</logger>
<logger name="org.hibernate">
<level value="WARN"/>
</logger>

<!-- BeanUtils generate to many DEBUG logs when sonar.verbose is set -->
<logger name="org.apache.commons.beanutils.converters">
<level value="WARN"/>
</logger>
<logger name="net.sf.ehcache">
<level value="WARN"/>
</logger>

<logger name="org.hibernate.cache.ReadWriteCache">
<!-- removing "An item was expired by the cache while it was locked (increase your cache timeout)" msg -->
<level value="ERROR"/>
<!-- sonar.showSql -->
<logger name="org.hibernate.SQL">
<level value="${SQL_LOGGER_LEVEL:-ERROR}"/>
</logger>
<logger name="org.hibernate.cache.EhCacheProvider">
<!-- removing "org.hibernate.cache.EhCacheProvider - Could not find configuratio)" message -->
<level value="ERROR"/>
<logger name="java.sql.Statement">
<level value="${SQL_LOGGER_LEVEL:-WARN}"/>
</logger>
<logger name="java.sql.PreparedStatement">
<level value="${SQL_LOGGER_LEVEL:-WARN}"/>
</logger>

<root>
<!-- sonar.verbose -->
<level value="${ROOT_LOGGER_LEVEL}"/>
<appender-ref ref="STDOUT"/>
</root>

+ 17
- 6
sonar-batch/src/main/resources/org/sonar/batch/logback.xml View File

@@ -2,12 +2,15 @@
<configuration debug="false">

<!--
This file is directly loaded by Sonar Ant Task 1.1 and Sonar Runner 1.1.
This file is deprecated. It's replaced by org/sonar/batch/bootstrapper/logback.xml.
It can't be deleted as long as Ant Task and Java Runner do not use org.sonar.batch.bootstrapper.LoggingConfiguration.

-->

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} %-5level %msg%n</pattern>
<pattern>%d{HH:mm:ss.SSS} %-5level %30.30logger{30} - %msg%n</pattern>
</encoder>
</appender>

@@ -22,10 +25,6 @@
<!-- removing "org.hibernate.cache.EhCacheProvider - Could not find configuratio)" message -->
<level value="ERROR"/>
</logger>
<!-- set DEBUG to activate SQL logs. NOT RECOMMENDED -->
<logger name="org.hibernate.SQL">
<level value="ERROR"/>
</logger>
<logger name="org.hibernate">
<level value="WARN"/>
</logger>
@@ -35,7 +34,19 @@
<level value="WARN"/>
</logger>

<!-- sonar.showSql -->
<logger name="org.hibernate.SQL">
<level value="${SQL_LOGGER_LEVEL:-ERROR}"/>
</logger>
<logger name="java.sql.Statement">
<level value="${SQL_LOGGER_LEVEL:-WARN}"/>
</logger>
<logger name="java.sql.PreparedStatement">
<level value="${SQL_LOGGER_LEVEL:-WARN}"/>
</logger>

<root>
<!-- sonar.verbose -->
<level value="${ROOT_LOGGER_LEVEL}"/>
<appender-ref ref="STDOUT"/>
</root>

+ 121
- 0
sonar-batch/src/test/java/org/sonar/batch/bootstrapper/LoggingConfigurationTest.java View File

@@ -0,0 +1,121 @@
/*
* Sonar, open source software quality management tool.
* Copyright (C) 2008-2012 SonarSource
* mailto:contact AT sonarsource DOT com
*
* Sonar 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.
*
* Sonar 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 Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package org.sonar.batch.bootstrapper;

import com.google.common.collect.Maps;
import org.hamcrest.core.Is;
import org.junit.Test;

import java.util.Map;

import static org.junit.Assert.assertThat;

public class LoggingConfigurationTest {

@Test
public void testSqlLevel() {
assertThat(LoggingConfiguration.create().setShowSql(true)
.getSubstitutionVariable(LoggingConfiguration.PROPERTY_SQL_LOGGER_LEVEL), Is.is(LoggingConfiguration.LEVEL_SQL_VERBOSE));

assertThat(LoggingConfiguration.create().setShowSql(false)
.getSubstitutionVariable(LoggingConfiguration.PROPERTY_SQL_LOGGER_LEVEL), Is.is(LoggingConfiguration.LEVEL_SQL_DEFAULT));

assertThat(LoggingConfiguration.create().setSqlLevel("ERROR")
.getSubstitutionVariable(LoggingConfiguration.PROPERTY_SQL_LOGGER_LEVEL), Is.is("ERROR"));
}

@Test
public void shouldNotShowSqlByDefault() {
assertThat(LoggingConfiguration.create()
.getSubstitutionVariable(LoggingConfiguration.PROPERTY_SQL_LOGGER_LEVEL), Is.is(LoggingConfiguration.LEVEL_SQL_DEFAULT));
}

@Test
public void testSetVerbose() {
assertThat(LoggingConfiguration.create().setVerbose(true)
.getSubstitutionVariable(LoggingConfiguration.PROPERTY_ROOT_LOGGER_LEVEL), Is.is(LoggingConfiguration.LEVEL_ROOT_VERBOSE));

assertThat(LoggingConfiguration.create().setVerbose(false)
.getSubstitutionVariable(LoggingConfiguration.PROPERTY_ROOT_LOGGER_LEVEL), Is.is(LoggingConfiguration.LEVEL_ROOT_DEFAULT));

assertThat(LoggingConfiguration.create().setRootLevel("ERROR")
.getSubstitutionVariable(LoggingConfiguration.PROPERTY_ROOT_LOGGER_LEVEL), Is.is("ERROR"));
}

@Test
public void shouldNotBeVerboseByDefault() {
assertThat(LoggingConfiguration.create()
.getSubstitutionVariable(LoggingConfiguration.PROPERTY_ROOT_LOGGER_LEVEL), Is.is(LoggingConfiguration.LEVEL_ROOT_DEFAULT));
}

@Test
public void testSetVerboseProperty() {
Map<String, String> properties = Maps.newHashMap();
assertThat(LoggingConfiguration.create().setProperties(properties)
.getSubstitutionVariable(LoggingConfiguration.PROPERTY_ROOT_LOGGER_LEVEL), Is.is(LoggingConfiguration.LEVEL_ROOT_DEFAULT));

properties.put("sonar.verbose", "true");
assertThat(LoggingConfiguration.create().setProperties(properties)
.getSubstitutionVariable(LoggingConfiguration.PROPERTY_ROOT_LOGGER_LEVEL), Is.is(LoggingConfiguration.LEVEL_ROOT_VERBOSE));

properties.put("sonar.verbose", "false");
assertThat(LoggingConfiguration.create().setProperties(properties)
.getSubstitutionVariable(LoggingConfiguration.PROPERTY_ROOT_LOGGER_LEVEL), Is.is(LoggingConfiguration.LEVEL_ROOT_DEFAULT));
}

@Test
public void testSetShowSqlProperty() {
Map<String, String> properties = Maps.newHashMap();
assertThat(LoggingConfiguration.create().setProperties(properties)
.getSubstitutionVariable(LoggingConfiguration.PROPERTY_SQL_LOGGER_LEVEL), Is.is(LoggingConfiguration.LEVEL_SQL_DEFAULT));

properties.put("sonar.showSql", "true");
assertThat(LoggingConfiguration.create().setProperties(properties)
.getSubstitutionVariable(LoggingConfiguration.PROPERTY_SQL_LOGGER_LEVEL), Is.is(LoggingConfiguration.LEVEL_SQL_VERBOSE));

properties.put("sonar.showSql", "false");
assertThat(LoggingConfiguration.create().setProperties(properties)
.getSubstitutionVariable(LoggingConfiguration.PROPERTY_SQL_LOGGER_LEVEL), Is.is(LoggingConfiguration.LEVEL_SQL_DEFAULT));
}

@Test
public void testDefaultFormat() {
assertThat(LoggingConfiguration.create()
.getSubstitutionVariable(LoggingConfiguration.PROPERTY_FORMAT), Is.is(LoggingConfiguration.FORMAT_DEFAULT));
}

@Test
public void testSetFormat() {
assertThat(LoggingConfiguration.create().setFormat("%d %level")
.getSubstitutionVariable(LoggingConfiguration.PROPERTY_FORMAT), Is.is("%d %level"));
}

@Test
public void shouldNotSetBlankFormat() {
assertThat(LoggingConfiguration.create().setFormat(null)
.getSubstitutionVariable(LoggingConfiguration.PROPERTY_FORMAT), Is.is(LoggingConfiguration.FORMAT_DEFAULT));

assertThat(LoggingConfiguration.create().setFormat("")
.getSubstitutionVariable(LoggingConfiguration.PROPERTY_FORMAT), Is.is(LoggingConfiguration.FORMAT_DEFAULT));

assertThat(LoggingConfiguration.create().setFormat(" ")
.getSubstitutionVariable(LoggingConfiguration.PROPERTY_FORMAT), Is.is(LoggingConfiguration.FORMAT_DEFAULT));
}
}

+ 15
- 7
sonar-core/src/main/java/org/sonar/core/config/Logback.java View File

@@ -31,6 +31,7 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

/**
* Configure Logback
@@ -43,18 +44,18 @@ public final class Logback {
// only static methods
}

public static void configure(String classloaderPath) {
public static void configure(String classloaderPath, Map<String, String> substitutionVariables) {
InputStream input = Logback.class.getResourceAsStream(classloaderPath);
if (input == null) {
throw new IllegalArgumentException("Logback configuration not found in classloader: " + classloaderPath);
}
configure(input);
configure(input, substitutionVariables);
}

public static void configure(File logbackFile) {
public static void configure(File logbackFile, Map<String, String> substitutionVariables) {
try {
FileInputStream input = FileUtils.openInputStream(logbackFile);
configure(input);
configure(input, substitutionVariables);
} catch (IOException e) {
throw new IllegalArgumentException("Fail to load the Logback configuration: " + logbackFile, e);
}
@@ -63,12 +64,11 @@ public final class Logback {
/**
* Note that this method closes the input stream
*/
private static void configure(InputStream input) {
private static void configure(InputStream input, Map<String, String> substitutionVariables) {
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
try {
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(lc);
lc.reset();
configurator.setContext(configureContext(lc, substitutionVariables));
configurator.doConfigure(input);
} catch (JoranException e) {
// StatusPrinter will handle this
@@ -77,4 +77,12 @@ public final class Logback {
}
StatusPrinter.printInCaseOfErrorsOrWarnings(lc);
}

private static LoggerContext configureContext(LoggerContext context, Map<String, String> substitutionVariables) {
context.reset();
for (Map.Entry<String, String> entry : substitutionVariables.entrySet()) {
context.putProperty(entry.getKey(), entry.getValue());
}
return context;
}
}

+ 15
- 10
sonar-maven-plugin/src/main/java/org/sonar/maven/SonarMojo.java View File

@@ -38,7 +38,7 @@ import org.sonar.api.batch.bootstrap.ProjectReactor;
import org.sonar.batch.Batch;
import org.sonar.batch.MavenProjectConverter;
import org.sonar.batch.bootstrapper.EnvironmentInformation;
import org.sonar.core.config.Logback;
import org.sonar.batch.bootstrapper.LoggingConfiguration;

/**
* @goal sonar
@@ -137,8 +137,13 @@ public final class SonarMojo extends AbstractMojo {
*/
private boolean verbose;

/**
* @parameter expression="${sonar.showSql}" default-value="false"
*/
private boolean showSql;

public void execute() throws MojoExecutionException, MojoFailureException {
configureLogback();
configureLogging();
executeBatch();
}

@@ -147,8 +152,8 @@ public final class SonarMojo extends AbstractMojo {
ProjectReactor reactor = new ProjectReactor(def);

Batch batch = new Batch(reactor, session, getLog(), lifecycleExecutor, pluginManager, artifactFactory,
localRepository, artifactMetadataSource, artifactCollector, dependencyTreeBuilder,
projectBuilder, getEnvironmentInformation(), Maven2PluginExecutor.class);
localRepository, artifactMetadataSource, artifactCollector, dependencyTreeBuilder,
projectBuilder, getEnvironmentInformation(), Maven2PluginExecutor.class);
batch.execute();
}

@@ -157,11 +162,11 @@ public final class SonarMojo extends AbstractMojo {
return new EnvironmentInformation("Maven", mavenVersion);
}

private void configureLogback() {
boolean debugMode = (verbose || getLog().isDebugEnabled());
// this system property is required by the logback configuration
System.setProperty("ROOT_LOGGER_LEVEL", debugMode ? "DEBUG" : "INFO");
Logback.configure("/org/sonar/maven/logback.xml");
private void configureLogging() {
LoggingConfiguration.create()
.setVerbose(verbose || getLog().isDebugEnabled())
.setShowSql(showSql)
.setFormat(LoggingConfiguration.FORMAT_MAVEN)
.configure();
}
}

+ 15
- 10
sonar-maven3-plugin/src/main/java/org/sonar/maven3/SonarMojo.java View File

@@ -37,7 +37,7 @@ import org.sonar.api.batch.bootstrap.ProjectReactor;
import org.sonar.batch.Batch;
import org.sonar.batch.MavenProjectConverter;
import org.sonar.batch.bootstrapper.EnvironmentInformation;
import org.sonar.core.config.Logback;
import org.sonar.batch.bootstrapper.LoggingConfiguration;

/**
* @goal sonar
@@ -130,9 +130,14 @@ public final class SonarMojo extends AbstractMojo {
*/
private boolean verbose;

/**
* @parameter expression="${sonar.showSql}" default-value="false"
*/
private boolean showSql;


public void execute() throws MojoExecutionException, MojoFailureException {
configureLogback();
configureLogging();
executeBatch();
}

@@ -141,8 +146,8 @@ public final class SonarMojo extends AbstractMojo {
ProjectReactor reactor = new ProjectReactor(def);

Batch batch = new Batch(reactor, session, getLog(), lifecycleExecutor, artifactFactory,
localRepository, artifactMetadataSource, artifactCollector, dependencyTreeBuilder,
projectBuilder, getEnvironmentInformation(), Maven3PluginExecutor.class);
localRepository, artifactMetadataSource, artifactCollector, dependencyTreeBuilder,
projectBuilder, getEnvironmentInformation(), Maven3PluginExecutor.class);
batch.execute();
}

@@ -151,11 +156,11 @@ public final class SonarMojo extends AbstractMojo {
return new EnvironmentInformation("Maven", mavenVersion);
}

private void configureLogback() {
boolean debugMode = (verbose || getLog().isDebugEnabled());
// this system property is required by the logback configuration
System.setProperty("ROOT_LOGGER_LEVEL", debugMode ? "DEBUG" : "INFO");
Logback.configure("/org/sonar/maven3/logback.xml");
private void configureLogging() {
LoggingConfiguration.create()
.setVerbose(verbose || getLog().isDebugEnabled())
.setShowSql(showSql)
.setFormat(LoggingConfiguration.FORMAT_MAVEN)
.configure();
}
}

+ 0
- 42
sonar-maven3-plugin/src/main/resources/org/sonar/maven3/logback.xml View File

@@ -1,42 +0,0 @@
<?xml version="1.0" encoding="UTF-8" ?>
<configuration debug="false">

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>[%level] [%d{HH:mm:ss.SSS}] %msg%n</pattern>
</encoder>
</appender>

<logger name="org.hibernate">
<level value="WARN"/>
</logger>

<!-- set DEBUG to activate SQL logs. NOT RECOMMENDED -->
<logger name="org.hibernate.SQL">
<level value="ERROR"/>
</logger>

<!-- BeanUtils generate to many DEBUG logs when sonar.verbose is set -->
<logger name="org.apache.commons.beanutils.converters">
<level value="WARN"/>
</logger>

<logger name="net.sf.ehcache">
<level value="WARN"/>
</logger>

<logger name="org.hibernate.cache.ReadWriteCache">
<!-- removing "An item was expired by the cache while it was locked (increase your cache timeout)" msg -->
<level value="ERROR"/>
</logger>
<logger name="org.hibernate.cache.EhCacheProvider">
<!-- removing "org.hibernate.cache.EhCacheProvider - Could not find configuratio)" message -->
<level value="ERROR"/>
</logger>

<root>
<level value="${ROOT_LOGGER_LEVEL}"/>
<appender-ref ref="STDOUT"/>
</root>

</configuration>

+ 2
- 1
sonar-server/src/main/java/org/sonar/server/platform/PlatformLifecycleListener.java View File

@@ -24,6 +24,7 @@ import org.sonar.core.config.Logback;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.io.File;
import java.util.Collections;

public final class PlatformLifecycleListener implements ServletContextListener {

@@ -41,6 +42,6 @@ public final class PlatformLifecycleListener implements ServletContextListener {
* Configure Logback with $SONAR_HOME/conf/logback.xml
*/
private void configureLogback() {
Logback.configure(new File(SonarHome.getHome(), "conf/logback.xml"));
Logback.configure(new File(SonarHome.getHome(), "conf/logback.xml"), Collections.<String, String>emptyMap());
}
}

Loading…
Cancel
Save