001 /*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2014 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * SonarQube is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
019 */
020 package org.sonar.batch.bootstrapper;
021
022 import org.sonar.core.profiling.Profiling.Level;
023
024 import org.sonar.core.profiling.Profiling;
025 import com.google.common.annotations.VisibleForTesting;
026 import com.google.common.collect.Maps;
027 import org.apache.commons.lang.StringUtils;
028 import org.sonar.core.config.Logback;
029
030 import javax.annotation.Nullable;
031
032 import java.io.File;
033 import java.util.Map;
034
035 /**
036 * @since 2.14
037 */
038 public final class LoggingConfiguration {
039
040 public static final String PROPERTY_ROOT_LOGGER_LEVEL = "ROOT_LOGGER_LEVEL";
041 public static final String PROPERTY_SQL_RESULTS_LOGGER_LEVEL = "SQL_RESULTS_LOGGER_LEVEL";
042 public static final String PROPERTY_FORMAT = "FORMAT";
043
044 public static final String LEVEL_ROOT_VERBOSE = "DEBUG";
045 public static final String LEVEL_ROOT_DEFAULT = "INFO";
046 public static final String LEVEL_SQL_VERBOSE = "DEBUG";
047 public static final String LEVEL_SQL_DEFAULT = "WARN";
048 public static final String LEVEL_SQL_RESULTS_VERBOSE = "DEBUG";
049 public static final String LEVEL_SQL_RESULTS_DEFAULT = "WARN";
050
051 @VisibleForTesting
052 static final String FORMAT_DEFAULT = "%d{HH:mm:ss.SSS} %-5level - %msg%n";
053 @VisibleForTesting
054 static final String FORMAT_MAVEN = "[%level] [%d{HH:mm:ss.SSS}] %msg%n";
055
056 private Map<String, String> substitutionVariables = Maps.newHashMap();
057
058 private LoggingConfiguration(@Nullable EnvironmentInformation environment) {
059 setVerbose(false);
060 if (environment != null && "maven".equalsIgnoreCase(environment.getKey())) {
061 setFormat(FORMAT_MAVEN);
062 } else {
063 setFormat(FORMAT_DEFAULT);
064 }
065 }
066
067 static LoggingConfiguration create(@Nullable EnvironmentInformation environment) {
068 return new LoggingConfiguration(environment);
069 }
070
071 public LoggingConfiguration setProperties(Map<String, String> properties) {
072 Profiling.Level profilingLevel = Profiling.Level.fromConfigString(properties.get("sonar.log.profilingLevel"));
073 setShowSqlResults(profilingLevel == Level.FULL);
074 setVerbose("true".equals(properties.get("sonar.verbose")));
075 return this;
076 }
077
078 public LoggingConfiguration setVerbose(boolean verbose) {
079 return setRootLevel(verbose ? LEVEL_ROOT_VERBOSE : LEVEL_ROOT_DEFAULT);
080 }
081
082 public LoggingConfiguration setShowSqlResults(boolean showSqlResults) {
083 return setSqlResultsLevel(showSqlResults ? LEVEL_SQL_RESULTS_VERBOSE : LEVEL_SQL_RESULTS_DEFAULT);
084 }
085
086 public LoggingConfiguration setRootLevel(String level) {
087 return addSubstitutionVariable(PROPERTY_ROOT_LOGGER_LEVEL, level);
088 }
089
090 public LoggingConfiguration setSqlResultsLevel(String level) {
091 return addSubstitutionVariable(PROPERTY_SQL_RESULTS_LOGGER_LEVEL, level);
092 }
093
094 @VisibleForTesting
095 LoggingConfiguration setFormat(String format) {
096 return addSubstitutionVariable(PROPERTY_FORMAT, StringUtils.defaultIfBlank(format, FORMAT_DEFAULT));
097 }
098
099 public LoggingConfiguration addSubstitutionVariable(String key, String value) {
100 substitutionVariables.put(key, value);
101 return this;
102 }
103
104 String getSubstitutionVariable(String key) {
105 return substitutionVariables.get(key);
106 }
107
108 LoggingConfiguration configure(String classloaderPath) {
109 Logback.configure(classloaderPath, substitutionVariables);
110 return this;
111 }
112
113 LoggingConfiguration configure(File logbackFile) {
114 Logback.configure(logbackFile, substitutionVariables);
115 return this;
116 }
117
118 LoggingConfiguration configure() {
119 Logback.configure("/org/sonar/batch/bootstrapper/logback.xml", substitutionVariables);
120 return this;
121 }
122 }