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.util.Map;
20  import java.util.WeakHashMap;
21  import java.util.concurrent.ConcurrentHashMap;
22  import java.util.concurrent.ConcurrentMap;
23  
24  import org.apache.logging.log4j.LogManager;
25  import org.apache.logging.log4j.spi.LoggerContext;
26  import org.apache.logging.log4j.spi.ExtendedLogger;
27  import org.slf4j.ILoggerFactory;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  /**
32   *
33   */
34  public class Log4jLoggerFactory implements ILoggerFactory {
35  
36      private static final String FQCN = Log4jLoggerFactory.class.getName();
37      private static final String PACKAGE = "org.slf4j";
38  
39      private final Map<LoggerContext, ConcurrentMap<String, Logger>> contextMap =
40          new WeakHashMap<LoggerContext, ConcurrentMap<String, Logger>>();
41  
42      @Override
43      public Logger getLogger(final String name) {
44          final LoggerContext context = getContext();
45          final ConcurrentMap<String, Logger> loggers = getLoggersMap(context);
46  
47          if (loggers.containsKey(name)) {
48              return loggers.get(name);
49          }
50          final String key = Logger.ROOT_LOGGER_NAME.equals(name) ? LogManager.ROOT_LOGGER_NAME : name;
51          loggers.putIfAbsent(name, new Log4jLogger(context.getLogger(key), name));
52          return loggers.get(name);
53      }
54  
55      private ConcurrentMap<String, Logger> getLoggersMap(final LoggerContext context) {
56          synchronized (contextMap) {
57              ConcurrentMap<String, Logger> map = contextMap.get(context);
58              if (map == null) {
59                  map = new ConcurrentHashMap<String, Logger>();
60                  contextMap.put(context, map);
61              }
62              return map;
63          }
64      }
65      private LoggerContext getContext() {
66          final Throwable t = new Throwable();
67          boolean next = false;
68          boolean pkg = false;
69          String fqcn = LoggerFactory.class.getName();
70          for (final StackTraceElement element : t.getStackTrace()) {
71              if (FQCN.equals(element.getClassName())) {
72                  next = true;
73                  continue;
74              }
75              if (next && element.getClassName().startsWith(PACKAGE)) {
76                  fqcn = element.getClassName();
77                  pkg = true;
78                  continue;
79              }
80              if (pkg) {
81                  break;
82              }
83          }
84          return PrivateManager.getContext(fqcn);
85      }
86  
87      /**
88       * The real bridge between SLF4J and Log4j.
89       */
90      private static class PrivateManager extends LogManager {
91          private static final String FQCN = LoggerFactory.class.getName();
92  
93          public static LoggerContext getContext() {
94              return getContext(FQCN, false);
95          }
96  
97          public static LoggerContext getContext(final String fqcn) {
98              return getContext(fqcn, false);
99          }
100 
101         public static ExtendedLogger getLogger(final String name) {
102             return getContext(FQCN).getLogger(name);
103         }
104     }
105 }