Repository: logging-log4j2 Updated Branches: refs/heads/LOG4J2-608 7be5f6efe -> c3d663694
Add ExternalLoggerContextRegistry. - Used in various bindings. Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/24b05983 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/24b05983 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/24b05983 Branch: refs/heads/LOG4J2-608 Commit: 24b0598317d2faca035e233deea581ca64469d87 Parents: 7be5f6e Author: Matt Sicker <[email protected]> Authored: Mon Sep 1 13:37:20 2014 -0500 Committer: Matt Sicker <[email protected]> Committed: Mon Sep 1 13:37:20 2014 -0500 ---------------------------------------------------------------------- .../AbstractExternalLoggerContextRegistry.java | 63 ++++++++++++++++++++ .../spi/ExternalLoggerContextRegistry.java | 52 ++++++++++++++++ 2 files changed, 115 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/24b05983/log4j-api/src/main/java/org/apache/logging/log4j/spi/AbstractExternalLoggerContextRegistry.java ---------------------------------------------------------------------- diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/spi/AbstractExternalLoggerContextRegistry.java b/log4j-api/src/main/java/org/apache/logging/log4j/spi/AbstractExternalLoggerContextRegistry.java new file mode 100644 index 0000000..9e7a8fc --- /dev/null +++ b/log4j-api/src/main/java/org/apache/logging/log4j/spi/AbstractExternalLoggerContextRegistry.java @@ -0,0 +1,63 @@ +/* + * 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.spi; + +import java.util.Map; +import java.util.WeakHashMap; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +/** + * Provides an abstract base class to use for implementing ExternalLoggerContextRegistry. + * @since 2.1 + */ +public abstract class AbstractExternalLoggerContextRegistry<L> implements ExternalLoggerContextRegistry<L> { + + /** + * A map to store loggers for their given LoggerContexts. + */ + protected final Map<LoggerContext, ConcurrentMap<String, L>> registry = + new WeakHashMap<LoggerContext, ConcurrentMap<String, L>>(); + + @Override + public L getLogger(String name) { + final LoggerContext context = getContext(); + final ConcurrentMap<String, L> loggers = getLoggersInContext(context); + if (loggers.containsKey(name)) { + return loggers.get(name); + } + loggers.putIfAbsent(name, newLogger(name, context)); + return loggers.get(name); + } + + @Override + public ConcurrentMap<String, L> getLoggersInContext(final LoggerContext context) { + synchronized (registry) { + ConcurrentMap<String, L> loggers = registry.get(context); + if (loggers == null) { + loggers = new ConcurrentHashMap<String, L>(); + registry.put(context, loggers); + } + return loggers; + } + } + + @Override + public void stop() { + registry.clear(); + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/24b05983/log4j-api/src/main/java/org/apache/logging/log4j/spi/ExternalLoggerContextRegistry.java ---------------------------------------------------------------------- diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/spi/ExternalLoggerContextRegistry.java b/log4j-api/src/main/java/org/apache/logging/log4j/spi/ExternalLoggerContextRegistry.java new file mode 100644 index 0000000..25d3096 --- /dev/null +++ b/log4j-api/src/main/java/org/apache/logging/log4j/spi/ExternalLoggerContextRegistry.java @@ -0,0 +1,52 @@ +package org.apache.logging.log4j.spi; + +import java.util.concurrent.ConcurrentMap; + +/** + * A basic registry for {@link LoggerContext} objects and their associated external + * Logger classes. This registry should not be used for Log4j Loggers; it is instead used for creating bridges to + * other external log systems. + * + * @param <L> the external logger class for this registry (e.g., {@code org.slf4j.Logger}) + * @since 2.1 + */ +public interface ExternalLoggerContextRegistry<L> { + + /** + * Gets a named logger linked to the {@link LoggerContext} returned by {@link #getContext()}. If no logger of + * the given name exists, then a new logger will be created using {@link #newLogger(String, LoggerContext)}. + * + * @param name the name of the logger to get + * @return the named logger + */ + L getLogger(String name); + + /** + * Creates a new named logger for a given {@link LoggerContext}. + * + * @param name the name of the logger to create + * @param context the LoggerContext this logger will be associated with + * @return the new named logger + */ + L newLogger(String name, LoggerContext context); + + /** + * Gets the {@link LoggerContext} that should be used to look up or create loggers. + * + * @return the LoggerContext to be used for lookup and creation purposes + */ + LoggerContext getContext(); + + /** + * Gets or creates the ConcurrentMap of named loggers for a given LoggerContext. + * + * @param context the LoggerContext to get loggers for + * @return the map of loggers for the given LoggerContext + */ + ConcurrentMap<String, L> getLoggersInContext(LoggerContext context); + + /** + * Shuts down this registry. Implementations should clear out any instance data and perform any relevant clean-up. + */ + void stop(); +}
