View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.slf4j;
18  
19  import java.net.URI;
20  
21  import org.apache.logging.log4j.spi.LoggerContext;
22  import org.apache.logging.log4j.spi.LoggerContextFactory;
23  import org.apache.logging.log4j.status.StatusLogger;
24  
25  /**
26   *
27   */
28  public class SLF4JLoggerContextFactory implements LoggerContextFactory {
29      private static final StatusLogger LOGGER = StatusLogger.getLogger();
30      private static LoggerContext context = new SLF4JLoggerContext();
31  
32      public SLF4JLoggerContextFactory() {
33          // LOG4J2-230, LOG4J2-204 (improve error reporting when misconfigured)
34          boolean misconfigured = false;
35          try {
36              Class.forName("org.slf4j.helpers.Log4jLoggerFactory");
37              misconfigured = true;
38          } catch (final ClassNotFoundException classNotFoundIsGood) {
39              LOGGER.debug("org.slf4j.helpers.Log4jLoggerFactory is not on classpath. Good!");
40          }
41          if (misconfigured) {
42              throw new IllegalStateException("slf4j-impl jar is mutually exclusive with log4j-to-slf4j jar "
43                      + "(the first routes calls from SLF4J to Log4j, the second from Log4j to SLF4J)");
44          }
45      }
46  
47      @Override
48      public LoggerContext getContext(final String fqcn, final ClassLoader loader, final Object externalContext,
49                                      final boolean currentContext) {
50          return context;
51      }
52  
53      @Override
54      public LoggerContext getContext(final String fqcn, final ClassLoader loader, final Object externalContext,
55                                      final boolean currentContext, final URI configLocation, final String name) {
56          return context;
57      }
58  
59      @Override
60      public void removeContext(final LoggerContext context) {
61      }
62  }