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.log4j.jcl;
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogConfigurationException;
21  import org.apache.commons.logging.LogFactory;
22  import org.apache.logging.log4j.LogManager;
23  import org.apache.logging.log4j.spi.AbstractLogger;
24  import org.apache.logging.log4j.spi.LoggerContext;
25  
26  import java.util.Map;
27  import java.util.WeakHashMap;
28  import java.util.concurrent.ConcurrentHashMap;
29  import java.util.concurrent.ConcurrentMap;
30  
31  /**
32   *
33   */
34  public class LogFactoryImpl extends LogFactory {
35  
36      private final Map<LoggerContext, ConcurrentMap<String, Log>> contextMap =
37          new WeakHashMap<LoggerContext, ConcurrentMap<String, Log>>();
38  
39      private final ConcurrentMap<String, Object> attributes = new ConcurrentHashMap<String, Object>();
40  
41      @Override
42      public Log getInstance(final String name) throws LogConfigurationException {
43          final LoggerContext context = PrivateManager.getContext();
44          final ConcurrentMap<String, Log> loggers = getLoggersMap();
45          if (loggers.containsKey(name)) {
46              return loggers.get(name);
47          }
48          final org.apache.logging.log4j.Logger logger = PrivateManager.getLogger(name);
49          if (logger instanceof AbstractLogger) {
50              loggers.putIfAbsent(name, new Log4JLog((AbstractLogger) logger, name));
51              return loggers.get(name);
52          }
53          throw new LogConfigurationException(
54              "Commons Logging Adapter requires base logging system to extend Log4J AbstractLogger");
55      }
56  
57      private ConcurrentMap<String, Log> getLoggersMap() {
58          final LoggerContext context = PrivateManager.getContext();
59          synchronized (contextMap) {
60              ConcurrentMap<String, Log> map = contextMap.get(context);
61              if (map == null) {
62                  map = new ConcurrentHashMap<String, Log>();
63                  contextMap.put(context, map);
64              }
65              return map;
66          }
67      }
68  
69      @Override
70      public Object getAttribute(final String name) {
71          return attributes.get(name);
72      }
73  
74      @Override
75      public String[] getAttributeNames() {
76          return attributes.keySet().toArray(new String[attributes.size()]);
77      }
78  
79      @Override
80      public Log getInstance(final Class clazz) throws LogConfigurationException {
81          return getInstance(clazz.getName());
82      }
83  
84      /**
85       * This method is supposed to clear all loggers. In this implementation it will clear all the logger
86       * wrappers but the loggers managed by the underlying logger context will not be.
87       */
88      @Override
89      public void release() {
90          getLoggersMap().clear();
91      }
92  
93      @Override
94      public void removeAttribute(final String name) {
95          attributes.remove(name);
96      }
97  
98      @Override
99      public void setAttribute(final String name, final Object value) {
100         if (value != null) {
101             attributes.put(name, value);
102         } else {
103             removeAttribute(name);
104         }
105     }
106 
107     /**
108      * The real bridge between commons logging and Log4j.
109      */
110     private static class PrivateManager extends LogManager {
111         private static final String FQCN = LogFactory.class.getName();
112 
113         public static LoggerContext getContext() {
114             return getContext(FQCN, false);
115         }
116 
117         public static org.apache.logging.log4j.Logger getLogger(final String name) {
118             return getLogger(FQCN, name);
119         }
120     }
121 
122 }