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 */
020package org.sonar.core.config;
021
022import ch.qos.logback.classic.LoggerContext;
023import ch.qos.logback.classic.joran.JoranConfigurator;
024import ch.qos.logback.core.joran.spi.JoranException;
025import ch.qos.logback.core.util.StatusPrinter;
026import org.apache.commons.io.FileUtils;
027import org.apache.commons.io.IOUtils;
028import org.slf4j.LoggerFactory;
029
030import java.io.File;
031import java.io.FileInputStream;
032import java.io.IOException;
033import java.io.InputStream;
034import java.util.Map;
035
036/**
037 * Configure Logback
038 *
039 * @since 2.12
040 */
041public final class Logback {
042
043  private Logback() {
044    // only static methods
045  }
046
047  public static void configure(String classloaderPath, Map<String, String> substitutionVariables) {
048    InputStream input = Logback.class.getResourceAsStream(classloaderPath);
049    if (input == null) {
050      throw new IllegalArgumentException("Logback configuration not found in classloader: " + classloaderPath);
051    }
052    configure(input, substitutionVariables);
053  }
054
055  public static void configure(File logbackFile, Map<String, String> substitutionVariables) {
056    try {
057      FileInputStream input = FileUtils.openInputStream(logbackFile);
058      configure(input, substitutionVariables);
059    } catch (IOException e) {
060      throw new IllegalArgumentException("Fail to load the Logback configuration: " + logbackFile, e);
061    }
062  }
063
064  /**
065   * Note that this method closes the input stream
066   */
067  private static void configure(InputStream input, Map<String, String> substitutionVariables) {
068    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
069    try {
070      JoranConfigurator configurator = new JoranConfigurator();
071      configurator.setContext(configureContext(lc, substitutionVariables));
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
081  private static LoggerContext configureContext(LoggerContext context, Map<String, String> substitutionVariables) {
082    context.reset();
083    for (Map.Entry<String, String> entry : substitutionVariables.entrySet()) {
084      context.putProperty(entry.getKey(), entry.getValue());
085    }
086    return context;
087  }
088}