001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.slf4j;
018
019import java.net.URI;
020
021import org.apache.logging.log4j.spi.LoggerContext;
022import org.apache.logging.log4j.spi.LoggerContextFactory;
023import org.apache.logging.log4j.status.StatusLogger;
024import org.apache.logging.log4j.util.LoaderUtil;
025
026public class SLF4JLoggerContextFactory implements LoggerContextFactory {
027    private static final StatusLogger LOGGER = StatusLogger.getLogger();
028    private static final LoggerContext context = new SLF4JLoggerContext();
029
030    public SLF4JLoggerContextFactory() {
031        // LOG4J2-230, LOG4J2-204 (improve error reporting when misconfigured)
032        boolean misconfigured = false;
033        try {
034            LoaderUtil.loadClass("org.slf4j.helpers.Log4jLoggerFactory");
035            misconfigured = true;
036        } catch (final ClassNotFoundException classNotFoundIsGood) {
037            LOGGER.debug("org.slf4j.helpers.Log4jLoggerFactory is not on classpath. Good!");
038        }
039        if (misconfigured) {
040            throw new IllegalStateException("slf4j-impl jar is mutually exclusive with log4j-to-slf4j jar "
041                    + "(the first routes calls from SLF4J to Log4j, the second from Log4j to SLF4J)");
042        }
043    }
044
045    @Override
046    public LoggerContext getContext(final String fqcn, final ClassLoader loader, final Object externalContext,
047                                    final boolean currentContext) {
048        return context;
049    }
050
051    @Override
052    public LoggerContext getContext(final String fqcn, final ClassLoader loader, final Object externalContext,
053                                    final boolean currentContext, final URI configLocation, final String name) {
054        return context;
055    }
056
057    @Override
058    public void removeContext(final LoggerContext ignored) {
059    }
060
061    @Override
062    public boolean isClassLoaderDependent() {
063        // context is always used
064        return false;
065    }
066}