Skip to content

Commit

Permalink
SONAR-5936 support property sonar.log.level on batch
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Brandhof committed Feb 24, 2015
1 parent 7e89ccd commit b6c33f3
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 36 deletions.
Expand Up @@ -35,16 +35,11 @@
public final class LoggingConfiguration {

public static final String PROPERTY_ROOT_LOGGER_LEVEL = "ROOT_LOGGER_LEVEL";
public static final String PROPERTY_SQL_RESULTS_LOGGER_LEVEL = "SQL_RESULTS_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 LEVEL_SQL_RESULTS_VERBOSE = "DEBUG";
public static final String LEVEL_SQL_RESULTS_DEFAULT = "WARN";

public static final String LEVEL_ROOT_DEFAULT = "INFO";
@VisibleForTesting
static final String FORMAT_DEFAULT = "%d{HH:mm:ss.SSS} %-5level - %msg%n";
@VisibleForTesting
Expand All @@ -66,26 +61,28 @@ static LoggingConfiguration create(@Nullable EnvironmentInformation environment)
}

public LoggingConfiguration setProperties(Map<String, String> properties) {
//TODO
setShowSqlResults(false);
setVerbose("true".equals(properties.get("sonar.verbose")));
String logLevel = properties.get("sonar.log.level");
String deprecatedProfilingLevel = properties.get("sonar.log.profilingLevel");
boolean verbose = "true".equals(properties.get("sonar.verbose")) ||
"DEBUG".equals(logLevel) || "TRACE".equals(logLevel) ||
"BASIC".equals(deprecatedProfilingLevel) || "FULL".equals(deprecatedProfilingLevel);
boolean sql = "TRACE".equals(logLevel) || "FULL".equals(deprecatedProfilingLevel);

setShowSql(sql);
setVerbose(verbose);
return this;
}

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

public LoggingConfiguration setShowSqlResults(boolean showSqlResults) {
return setSqlResultsLevel(showSqlResults ? LEVEL_SQL_RESULTS_VERBOSE : LEVEL_SQL_RESULTS_DEFAULT);
}

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

public LoggingConfiguration setSqlResultsLevel(String level) {
return addSubstitutionVariable(PROPERTY_SQL_RESULTS_LOGGER_LEVEL, level);
public LoggingConfiguration setShowSql(boolean showSql) {
return addSubstitutionVariable(PROPERTY_SQL_LOGGER_LEVEL, showSql ? "TRACE" : "WARN");
}

@VisibleForTesting
Expand Down
Expand Up @@ -46,7 +46,7 @@
<level value="${SQL_LOGGER_LEVEL:-WARN}"/>
</logger>
<logger name="java.sql.ResultSet">
<level value="${SQL_RESULTS_LOGGER_LEVEL:-WARN}"/>
<level value="WARN"/>
</logger>
<logger name="PERSISTIT">
<level value="WARN"/>
Expand All @@ -58,4 +58,4 @@
<appender-ref ref="STDOUT"/>
</root>

</configuration>
</configuration>
4 changes: 2 additions & 2 deletions sonar-batch/src/main/resources/org/sonar/batch/logback.xml
Expand Up @@ -42,7 +42,7 @@
<level value="WARN"/>
</logger>
<logger name="java.sql.ResultSet">
<level value="${SQL_RESULTS_LOGGER_LEVEL:-WARN}"/>
<level value="WARN"/>
</logger>
<logger name="PERSISTIT">
<level value="WARN"/>
Expand All @@ -55,4 +55,4 @@
<appender-ref ref="STDOUT"/>
</root>

</configuration>
</configuration>
Expand Up @@ -31,63 +31,100 @@ public class LoggingConfigurationTest {
@Test
public void testSetVerbose() {
assertThat(LoggingConfiguration.create(null).setVerbose(true)
.getSubstitutionVariable(LoggingConfiguration.PROPERTY_ROOT_LOGGER_LEVEL)).isEqualTo(LoggingConfiguration.LEVEL_ROOT_VERBOSE);
.getSubstitutionVariable(LoggingConfiguration.PROPERTY_ROOT_LOGGER_LEVEL)).isEqualTo(LoggingConfiguration.LEVEL_ROOT_VERBOSE);

assertThat(LoggingConfiguration.create(null).setVerbose(false)
.getSubstitutionVariable(LoggingConfiguration.PROPERTY_ROOT_LOGGER_LEVEL)).isEqualTo(LoggingConfiguration.LEVEL_ROOT_DEFAULT);
.getSubstitutionVariable(LoggingConfiguration.PROPERTY_ROOT_LOGGER_LEVEL)).isEqualTo(LoggingConfiguration.LEVEL_ROOT_DEFAULT);

assertThat(LoggingConfiguration.create(null).setRootLevel("ERROR")
.getSubstitutionVariable(LoggingConfiguration.PROPERTY_ROOT_LOGGER_LEVEL)).isEqualTo("ERROR");
.getSubstitutionVariable(LoggingConfiguration.PROPERTY_ROOT_LOGGER_LEVEL)).isEqualTo("ERROR");
}

@Test
public void shouldNotBeVerboseByDefault() {
assertThat(LoggingConfiguration.create(null)
.getSubstitutionVariable(LoggingConfiguration.PROPERTY_ROOT_LOGGER_LEVEL)).isEqualTo(LoggingConfiguration.LEVEL_ROOT_DEFAULT);
.getSubstitutionVariable(LoggingConfiguration.PROPERTY_ROOT_LOGGER_LEVEL)).isEqualTo(LoggingConfiguration.LEVEL_ROOT_DEFAULT);
}

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

properties.put("sonar.verbose", "true");
assertThat(LoggingConfiguration.create(null).setProperties(properties)
.getSubstitutionVariable(LoggingConfiguration.PROPERTY_ROOT_LOGGER_LEVEL)).isEqualTo(LoggingConfiguration.LEVEL_ROOT_VERBOSE);
LoggingConfiguration conf = LoggingConfiguration.create(null).setProperties(properties);
assertThat(conf.getSubstitutionVariable(LoggingConfiguration.PROPERTY_ROOT_LOGGER_LEVEL)).isEqualTo(LoggingConfiguration.LEVEL_ROOT_VERBOSE);
assertThat(conf.getSubstitutionVariable(LoggingConfiguration.PROPERTY_SQL_LOGGER_LEVEL)).isEqualTo("WARN");

properties.put("sonar.verbose", "false");
assertThat(LoggingConfiguration.create(null).setProperties(properties)
.getSubstitutionVariable(LoggingConfiguration.PROPERTY_ROOT_LOGGER_LEVEL)).isEqualTo(LoggingConfiguration.LEVEL_ROOT_DEFAULT);
conf = LoggingConfiguration.create(null).setProperties(properties);
assertThat(conf.getSubstitutionVariable(LoggingConfiguration.PROPERTY_ROOT_LOGGER_LEVEL)).isEqualTo(LoggingConfiguration.LEVEL_ROOT_DEFAULT);
assertThat(conf.getSubstitutionVariable(LoggingConfiguration.PROPERTY_SQL_LOGGER_LEVEL)).isEqualTo("WARN");

properties.put("sonar.verbose", "false");
properties.put("sonar.log.profilingLevel", "FULL");
conf = LoggingConfiguration.create(null).setProperties(properties);
assertThat(conf.getSubstitutionVariable(LoggingConfiguration.PROPERTY_ROOT_LOGGER_LEVEL)).isEqualTo("DEBUG");
assertThat(conf.getSubstitutionVariable(LoggingConfiguration.PROPERTY_SQL_LOGGER_LEVEL)).isEqualTo("TRACE");

properties.put("sonar.verbose", "false");
properties.put("sonar.log.profilingLevel", "BASIC");
conf = LoggingConfiguration.create(null).setProperties(properties);
assertThat(conf.getSubstitutionVariable(LoggingConfiguration.PROPERTY_ROOT_LOGGER_LEVEL)).isEqualTo("DEBUG");
assertThat(conf.getSubstitutionVariable(LoggingConfiguration.PROPERTY_SQL_LOGGER_LEVEL)).isEqualTo("WARN");
}

@Test
public void test_log_level_property() {
Map<String, String> properties = Maps.newHashMap();
LoggingConfiguration conf = LoggingConfiguration.create(null).setProperties(properties);
assertThat(conf.getSubstitutionVariable(LoggingConfiguration.PROPERTY_ROOT_LOGGER_LEVEL)).isEqualTo("INFO");
assertThat(conf.getSubstitutionVariable(LoggingConfiguration.PROPERTY_SQL_LOGGER_LEVEL)).isEqualTo("WARN");

properties.put("sonar.log.level", "INFO");
conf = LoggingConfiguration.create(null).setProperties(properties);
assertThat(conf.getSubstitutionVariable(LoggingConfiguration.PROPERTY_ROOT_LOGGER_LEVEL)).isEqualTo("INFO");
assertThat(conf.getSubstitutionVariable(LoggingConfiguration.PROPERTY_SQL_LOGGER_LEVEL)).isEqualTo("WARN");

properties.put("sonar.log.level", "DEBUG");
conf = LoggingConfiguration.create(null).setProperties(properties);
assertThat(conf.getSubstitutionVariable(LoggingConfiguration.PROPERTY_ROOT_LOGGER_LEVEL)).isEqualTo("DEBUG");
assertThat(conf.getSubstitutionVariable(LoggingConfiguration.PROPERTY_SQL_LOGGER_LEVEL)).isEqualTo("WARN");

properties.put("sonar.log.level", "TRACE");
conf = LoggingConfiguration.create(null).setProperties(properties);
assertThat(conf.getSubstitutionVariable(LoggingConfiguration.PROPERTY_ROOT_LOGGER_LEVEL)).isEqualTo("DEBUG");
assertThat(conf.getSubstitutionVariable(LoggingConfiguration.PROPERTY_SQL_LOGGER_LEVEL)).isEqualTo("TRACE");
}

@Test
public void testDefaultFormat() {
assertThat(LoggingConfiguration.create(null)
.getSubstitutionVariable(LoggingConfiguration.PROPERTY_FORMAT)).isEqualTo(LoggingConfiguration.FORMAT_DEFAULT);
.getSubstitutionVariable(LoggingConfiguration.PROPERTY_FORMAT)).isEqualTo(LoggingConfiguration.FORMAT_DEFAULT);
}

@Test
public void testMavenFormat() {
assertThat(LoggingConfiguration.create(new EnvironmentInformation("maven", "1.0"))
.getSubstitutionVariable(LoggingConfiguration.PROPERTY_FORMAT)).isEqualTo(LoggingConfiguration.FORMAT_MAVEN);
.getSubstitutionVariable(LoggingConfiguration.PROPERTY_FORMAT)).isEqualTo(LoggingConfiguration.FORMAT_MAVEN);
}

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

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

assertThat(LoggingConfiguration.create(null).setFormat("")
.getSubstitutionVariable(LoggingConfiguration.PROPERTY_FORMAT)).isEqualTo(LoggingConfiguration.FORMAT_DEFAULT);
.getSubstitutionVariable(LoggingConfiguration.PROPERTY_FORMAT)).isEqualTo(LoggingConfiguration.FORMAT_DEFAULT);

assertThat(LoggingConfiguration.create(null).setFormat(" ")
.getSubstitutionVariable(LoggingConfiguration.PROPERTY_FORMAT)).isEqualTo(LoggingConfiguration.FORMAT_DEFAULT);
.getSubstitutionVariable(LoggingConfiguration.PROPERTY_FORMAT)).isEqualTo(LoggingConfiguration.FORMAT_DEFAULT);
}
}

0 comments on commit b6c33f3

Please sign in to comment.