LOG4J2-1010 factory to let users specify custom ContextDataInjectors
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/9d055237 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/9d055237 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/9d055237 Branch: refs/heads/LOG4J2-1010&LOG4J2-1447-injectable-contextdata&better-datastructure Commit: 9d0552375af0d8efee2dd13b6a178d3b0d3e757c Parents: 845d4d3 Author: rpopma <[email protected]> Authored: Wed Jul 27 01:12:14 2016 +0900 Committer: rpopma <[email protected]> Committed: Wed Jul 27 01:12:14 2016 +0900 ---------------------------------------------------------------------- .../core/impl/ContextDataInjectorFactory.java | 60 ++++++++++++++++++++ 1 file changed, 60 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/9d055237/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ContextDataInjectorFactory.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ContextDataInjectorFactory.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ContextDataInjectorFactory.java new file mode 100644 index 0000000..ba9ca72 --- /dev/null +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ContextDataInjectorFactory.java @@ -0,0 +1,60 @@ +/* + * 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.core.impl; + +import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.status.StatusLogger; +import org.apache.logging.log4j.util.LoaderUtil; +import org.apache.logging.log4j.util.PropertiesUtil; + +/** + * Factory for ContextDataInjectors. + * + * @see ContextDataInjector + * @see org.apache.logging.log4j.core.ContextData + * @see LogEvent#getContextData() + * @since 2.7 + */ +public class ContextDataInjectorFactory { + + /** + * Returns a new {@code ContextDataInjector} instance based on the value of system property + * {@code log4j.ContextDataInjector}. If not value was specified this method returns a new + * {@link ThreadContextDataInjector}. + * <p> + * Users may use this system property to specify the fully qualified class name of a class that implements the + * {@code ContextDataInjector} interface. + * + * @return a ContextDataInjector that populates the {@code ContextData} of all {@code LogEvent} objects + */ + public static ContextDataInjector getInjector() { + final String className = PropertiesUtil.getProperties().getStringProperty("log4j.ContextDataInjector"); + if (className == null) { + return new ThreadContextDataInjector(); + } + try { + final Class<? extends ContextDataInjector> cls = LoaderUtil.loadClass(className).asSubclass( + ContextDataInjector.class); + return cls.newInstance(); + } catch (final Exception dynamicFailed) { + StatusLogger.getLogger().warn( + "Could not create ContextDataInjector for '{}', using default ThreadContextDataInjector: {}", + className, dynamicFailed); + return new ThreadContextDataInjector(); + } + } +}
