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 java.io.IOException;
20  import java.util.concurrent.ConcurrentHashMap;
21  import java.util.concurrent.ConcurrentMap;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogConfigurationException;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.logging.log4j.spi.LoggerAdapter;
27  
28  /**
29   * Log4j binding for Commons Logging.
30   * {@inheritDoc}
31   */
32  public class LogFactoryImpl extends LogFactory {
33  
34      private final LoggerAdapter<Log> adapter = new LogAdapter();
35  
36      private final ConcurrentMap<String, Object> attributes = new ConcurrentHashMap<String, Object>();
37  
38      @Override
39      public Log getInstance(final String name) throws LogConfigurationException {
40          return adapter.getLogger(name);
41      }
42  
43      @Override
44      public Object getAttribute(final String name) {
45          return attributes.get(name);
46      }
47  
48      @Override
49      public String[] getAttributeNames() {
50          return attributes.keySet().toArray(new String[attributes.size()]);
51      }
52  
53      @Override
54      public Log getInstance(@SuppressWarnings("rawtypes") final Class clazz) throws LogConfigurationException {
55          return getInstance(clazz.getName());
56      }
57  
58      /**
59       * This method is supposed to clear all loggers. In this implementation it will clear all the logger
60       * wrappers but the loggers managed by the underlying logger context will not be.
61       */
62      @Override
63      public void release() {
64          try {
65              adapter.close();
66          } catch (final IOException ignored) {
67          }
68      }
69  
70      @Override
71      public void removeAttribute(final String name) {
72          attributes.remove(name);
73      }
74  
75      @Override
76      public void setAttribute(final String name, final Object value) {
77          if (value != null) {
78              attributes.put(name, value);
79          } else {
80              removeAttribute(name);
81          }
82      }
83  
84  }