Update jcl to use ExternalLoggerContextRegistry.
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/c3d66369 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/c3d66369 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/c3d66369 Branch: refs/heads/LOG4J2-608 Commit: c3d6636940d7bf3e5fc9c9600c03d86531ec385a Parents: c2c67cc Author: Matt Sicker <[email protected]> Authored: Mon Sep 1 13:40:49 2014 -0500 Committer: Matt Sicker <[email protected]> Committed: Mon Sep 1 13:40:49 2014 -0500 ---------------------------------------------------------------------- .../logging/log4j/jcl/LogFactoryImpl.java | 48 +++-------------- .../apache/logging/log4j/jcl/LogRegistry.java | 56 ++++++++++++++++++++ 2 files changed, 62 insertions(+), 42 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c3d66369/log4j-jcl/src/main/java/org/apache/logging/log4j/jcl/LogFactoryImpl.java ---------------------------------------------------------------------- diff --git a/log4j-jcl/src/main/java/org/apache/logging/log4j/jcl/LogFactoryImpl.java b/log4j-jcl/src/main/java/org/apache/logging/log4j/jcl/LogFactoryImpl.java index 8ef403a..bcb19b0 100644 --- a/log4j-jcl/src/main/java/org/apache/logging/log4j/jcl/LogFactoryImpl.java +++ b/log4j-jcl/src/main/java/org/apache/logging/log4j/jcl/LogFactoryImpl.java @@ -16,48 +16,27 @@ */ package org.apache.logging.log4j.jcl; -import java.util.Map; -import java.util.WeakHashMap; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogConfigurationException; import org.apache.commons.logging.LogFactory; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.spi.LoggerContext; -import org.apache.logging.log4j.spi.ExtendedLogger; +import org.apache.logging.log4j.spi.ExternalLoggerContextRegistry; /** - * + * Log4j binding for Commons Logging. + * {@inheritDoc} */ public class LogFactoryImpl extends LogFactory { - private final Map<LoggerContext, ConcurrentMap<String, Log>> contextMap = - new WeakHashMap<LoggerContext, ConcurrentMap<String, Log>>(); + private final ExternalLoggerContextRegistry<Log> registry = new LogRegistry(); private final ConcurrentMap<String, Object> attributes = new ConcurrentHashMap<String, Object>(); @Override public Log getInstance(final String name) throws LogConfigurationException { - final ConcurrentMap<String, Log> loggers = getLoggersMap(); - if (loggers.containsKey(name)) { - return loggers.get(name); - } - loggers.putIfAbsent(name, new Log4jLog(PrivateManager.getLogger(name))); - return loggers.get(name); - } - - private ConcurrentMap<String, Log> getLoggersMap() { - final LoggerContext context = PrivateManager.getContext(); - synchronized (contextMap) { - ConcurrentMap<String, Log> map = contextMap.get(context); - if (map == null) { - map = new ConcurrentHashMap<String, Log>(); - contextMap.put(context, map); - } - return map; - } + return registry.getLogger(name); } @Override @@ -81,7 +60,7 @@ public class LogFactoryImpl extends LogFactory { */ @Override public void release() { - getLoggersMap().clear(); + registry.stop(); } @Override @@ -98,19 +77,4 @@ public class LogFactoryImpl extends LogFactory { } } - /** - * The real bridge between commons logging and Log4j. - */ - private static class PrivateManager extends LogManager { - private static final String FQCN = LogFactory.class.getName(); - - public static LoggerContext getContext() { - return getContext(FQCN, false); - } - - public static ExtendedLogger getLogger(final String name) { - return getContext().getLogger(name); - } - } - } http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c3d66369/log4j-jcl/src/main/java/org/apache/logging/log4j/jcl/LogRegistry.java ---------------------------------------------------------------------- diff --git a/log4j-jcl/src/main/java/org/apache/logging/log4j/jcl/LogRegistry.java b/log4j-jcl/src/main/java/org/apache/logging/log4j/jcl/LogRegistry.java new file mode 100644 index 0000000..af44c8b --- /dev/null +++ b/log4j-jcl/src/main/java/org/apache/logging/log4j/jcl/LogRegistry.java @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache license, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ +package org.apache.logging.log4j.jcl; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.spi.AbstractExternalLoggerContextRegistry; +import org.apache.logging.log4j.spi.ExtendedLogger; +import org.apache.logging.log4j.spi.LoggerContext; + +/** + * Commons Logging registry. + */ +public class LogRegistry extends AbstractExternalLoggerContextRegistry<Log> { + + @Override + public Log newLogger(final String name, final LoggerContext context) { + return new Log4jLog(PrivateManager.getLogger(name)); + } + + @Override + public LoggerContext getContext() { + return PrivateManager.getContext(); + } + + /** + * The real bridge between commons logging and Log4j. + */ + private static class PrivateManager extends LogManager { + private static final String FQCN = LogFactory.class.getName(); + + public static LoggerContext getContext() { + return getContext(FQCN, false); + } + + public static ExtendedLogger getLogger(final String name) { + return getContext().getLogger(name); + } + } + +}
