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.log4j.jcl; 018 019import java.io.IOException; 020import java.util.concurrent.ConcurrentHashMap; 021import java.util.concurrent.ConcurrentMap; 022 023import org.apache.commons.logging.Log; 024import org.apache.commons.logging.LogConfigurationException; 025import org.apache.commons.logging.LogFactory; 026import org.apache.logging.log4j.spi.LoggerAdapter; 027 028/** 029 * Log4j binding for Commons Logging. 030 * {@inheritDoc} 031 */ 032public class LogFactoryImpl extends LogFactory { 033 034 private final LoggerAdapter<Log> adapter = new LogAdapter(); 035 036 private final ConcurrentMap<String, Object> attributes = new ConcurrentHashMap<>(); 037 038 @Override 039 public Log getInstance(final String name) throws LogConfigurationException { 040 return adapter.getLogger(name); 041 } 042 043 @Override 044 public Object getAttribute(final String name) { 045 return attributes.get(name); 046 } 047 048 @Override 049 public String[] getAttributeNames() { 050 return attributes.keySet().toArray(new String[attributes.size()]); 051 } 052 053 @Override 054 public Log getInstance(@SuppressWarnings("rawtypes") final Class clazz) throws LogConfigurationException { 055 return getInstance(clazz.getName()); 056 } 057 058 /** 059 * This method is supposed to clear all loggers. In this implementation it will clear all the logger 060 * wrappers but the loggers managed by the underlying logger context will not be. 061 */ 062 @Override 063 public void release() { 064 try { 065 adapter.close(); 066 } catch (final IOException ignored) { 067 } 068 } 069 070 @Override 071 public void removeAttribute(final String name) { 072 attributes.remove(name); 073 } 074 075 @Override 076 public void setAttribute(final String name, final Object value) { 077 if (value != null) { 078 attributes.put(name, value); 079 } else { 080 removeAttribute(name); 081 } 082 } 083 084}