001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2012 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * Sonar 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     * Sonar 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
017     * License along with Sonar; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019     */
020    package org.sonar.batch.bootstrapper;
021    
022    import com.google.common.collect.Maps;
023    import org.apache.commons.lang.StringUtils;
024    import org.sonar.core.config.Logback;
025    
026    import java.io.File;
027    import java.util.Map;
028    
029    /**
030     * @since 2.14
031     */
032    public final class LoggingConfiguration {
033    
034      public static final String PROPERTY_ROOT_LOGGER_LEVEL = "ROOT_LOGGER_LEVEL";
035      public static final String PROPERTY_SQL_LOGGER_LEVEL = "SQL_LOGGER_LEVEL";
036      public static final String PROPERTY_SQL_RESULTS_LOGGER_LEVEL = "SQL_RESULTS_LOGGER_LEVEL";
037      public static final String PROPERTY_FORMAT = "FORMAT";
038    
039      public static final String LEVEL_ROOT_VERBOSE = "DEBUG";
040      public static final String LEVEL_ROOT_DEFAULT = "INFO";
041      public static final String LEVEL_SQL_VERBOSE = "DEBUG";
042      public static final String LEVEL_SQL_DEFAULT = "WARN";
043      public static final String LEVEL_SQL_RESULTS_VERBOSE = "DEBUG";
044      public static final String LEVEL_SQL_RESULTS_DEFAULT = "WARN";
045    
046      public static final String FORMAT_DEFAULT = "%d{HH:mm:ss.SSS} %-5level - %msg%n";
047      public static final String FORMAT_MAVEN = "[%level] [%d{HH:mm:ss.SSS}] %msg%n";
048    
049      private Map<String, String> substitutionVariables = Maps.newHashMap();
050    
051      private LoggingConfiguration() {
052        setVerbose(false);
053        setShowSql(false);
054        setFormat(FORMAT_DEFAULT);
055      }
056    
057      static LoggingConfiguration create() {
058        return new LoggingConfiguration();
059      }
060    
061      public LoggingConfiguration setProperties(Map<String, String> properties) {
062        setShowSql("true".equals(properties.get("sonar.showSql")));
063        setShowSqlResults("true".equals(properties.get("sonar.showSqlResults")));
064        setVerbose("true".equals(properties.get("sonar.verbose")));
065        return this;
066      }
067    
068      public LoggingConfiguration setVerbose(boolean verbose) {
069        return setRootLevel(verbose ? LEVEL_ROOT_VERBOSE : LEVEL_ROOT_DEFAULT);
070      }
071    
072      public LoggingConfiguration setShowSql(boolean showSql) {
073        return setSqlLevel(showSql ? LEVEL_SQL_VERBOSE : LEVEL_SQL_DEFAULT);
074      }
075    
076      public LoggingConfiguration setShowSqlResults(boolean showSqlResults) {
077        return setSqlResultsLevel(showSqlResults ? LEVEL_SQL_RESULTS_VERBOSE : LEVEL_SQL_RESULTS_DEFAULT);
078      }
079    
080      public LoggingConfiguration setRootLevel(String level) {
081        return addSubstitutionVariable(PROPERTY_ROOT_LOGGER_LEVEL, level);
082      }
083    
084      public LoggingConfiguration setSqlLevel(String level) {
085        return addSubstitutionVariable(PROPERTY_SQL_LOGGER_LEVEL, level);
086      }
087    
088      public LoggingConfiguration setSqlResultsLevel(String level) {
089        return addSubstitutionVariable(PROPERTY_SQL_RESULTS_LOGGER_LEVEL, level);
090      }
091    
092      public LoggingConfiguration setFormat(String format) {
093        return addSubstitutionVariable(PROPERTY_FORMAT, StringUtils.defaultIfBlank(format, FORMAT_DEFAULT));
094      }
095    
096      public LoggingConfiguration addSubstitutionVariable(String key, String value) {
097        substitutionVariables.put(key, value);
098        return this;
099      }
100    
101      String getSubstitutionVariable(String key) {
102        return substitutionVariables.get(key);
103      }
104    
105      LoggingConfiguration configure(String classloaderPath) {
106        Logback.configure(classloaderPath, substitutionVariables);
107        return this;
108      }
109    
110      LoggingConfiguration configure(File logbackFile) {
111        Logback.configure(logbackFile, substitutionVariables);
112        return this;
113      }
114    
115      LoggingConfiguration configure() {
116        Logback.configure("/org/sonar/batch/bootstrapper/logback.xml", substitutionVariables);
117        return this;
118      }
119    }