All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alibaba.citrus.logconfig.logback.LogbackConfigurator Maven / Gradle / Ivy

There is a newer version: 1.2
Show newest version
/*
 * Copyright (c) 2002-2012 Alibaba Group Holding Limited.
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.alibaba.citrus.logconfig.logback;

import java.net.URL;
import java.util.Map;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.LogbackException;
import com.alibaba.citrus.logconfig.LogConfigurator;
import org.slf4j.ILoggerFactory;
import org.slf4j.LoggerFactory;

public class LogbackConfigurator extends LogConfigurator {
    @Override
    protected void doConfigure(URL configFile, Map props) throws Exception {
        JoranConfigurator configurator = new JoranConfigurator();

        configurator.setContext(getLoggerContext(props));
        configurator.doConfigure(configFile);
    }

    private LoggerContext getLoggerContext(Map props) {
        ILoggerFactory lcObject = LoggerFactory.getILoggerFactory();

        if (!(lcObject instanceof LoggerContext)) {
            throw new LogbackException(
                    "Expected LOGBACK binding with SLF4J, but another log system has taken the place: "
                    + lcObject.getClass().getSimpleName());
        }

        LoggerContext lc = (LoggerContext) lcObject;

        lc.reset();

        for (Map.Entry entry : props.entrySet()) {
            lc.putProperty(entry.getKey(), entry.getValue());
        }

        return lc;
    }

    @Override
    public void shutdown() {
        LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
        lc.stop();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy