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 026/** 027 * 028 */ 029public class SLF4JLoggerContextFactory implements LoggerContextFactory { 030 private static final StatusLogger LOGGER = StatusLogger.getLogger(); 031 private static LoggerContext context = new SLF4JLoggerContext(); 032 033 public SLF4JLoggerContextFactory() { 034 // LOG4J2-230, LOG4J2-204 (improve error reporting when misconfigured) 035 boolean misconfigured = false; 036 try { 037 LoaderUtil.loadClass("org.slf4j.helpers.Log4jLoggerFactory"); 038 misconfigured = true; 039 } catch (final ClassNotFoundException classNotFoundIsGood) { 040 LOGGER.debug("org.slf4j.helpers.Log4jLoggerFactory is not on classpath. Good!"); 041 } 042 if (misconfigured) { 043 throw new IllegalStateException("slf4j-impl jar is mutually exclusive with log4j-to-slf4j jar " 044 + "(the first routes calls from SLF4J to Log4j, the second from Log4j to SLF4J)"); 045 } 046 } 047 048 @Override 049 public LoggerContext getContext(final String fqcn, final ClassLoader loader, final Object externalContext, 050 final boolean currentContext) { 051 return context; 052 } 053 054 @Override 055 public LoggerContext getContext(final String fqcn, final ClassLoader loader, final Object externalContext, 056 final boolean currentContext, final URI configLocation, final String name) { 057 return context; 058 } 059 060 @Override 061 public void removeContext(final LoggerContext ignored) { 062 } 063}