001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2011 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.core.config;
021    
022    import ch.qos.logback.classic.LoggerContext;
023    import ch.qos.logback.classic.joran.JoranConfigurator;
024    import ch.qos.logback.core.joran.spi.JoranException;
025    import ch.qos.logback.core.util.StatusPrinter;
026    import org.apache.commons.io.FileUtils;
027    import org.apache.commons.io.IOUtils;
028    import org.slf4j.LoggerFactory;
029    
030    import java.io.File;
031    import java.io.FileInputStream;
032    import java.io.IOException;
033    import java.io.InputStream;
034    
035    /**
036     * Configure Logback
037     *
038     * @since 2.12
039     */
040    public final class Logback {
041    
042      private Logback() {
043        // only static methods
044      }
045    
046      public static void configure(String classloaderPath) {
047        InputStream input = Logback.class.getResourceAsStream(classloaderPath);
048        if (input == null) {
049          throw new IllegalArgumentException("Logback configuration not found in classloader: " + classloaderPath);
050        }
051        configure(input);
052      }
053    
054      public static void configure(File logbackFile) {
055        try {
056          FileInputStream input = FileUtils.openInputStream(logbackFile);
057          configure(input);
058        } catch (IOException e) {
059          throw new IllegalArgumentException("Fail to load the Logback configuration: " + logbackFile, e);
060        }
061      }
062    
063      /**
064       * Note that this method closes the input stream
065       */
066      private static void configure(InputStream input) {
067        LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
068        try {
069          JoranConfigurator configurator = new JoranConfigurator();
070          configurator.setContext(lc);
071          lc.reset();
072          configurator.doConfigure(input);
073        } catch (JoranException e) {
074          // StatusPrinter will handle this
075        } finally {
076          IOUtils.closeQuietly(input);
077        }
078        StatusPrinter.printInCaseOfErrorsOrWarnings(lc);
079      }
080    }