You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@logging.apache.org by GitBox <gi...@apache.org> on 2022/11/21 16:42:55 UTC

[GitHub] [logging-log4j2] rgoers commented on a diff in pull request #1136: Refactor API initialization into common LoggingSystem class

rgoers commented on code in PR #1136:
URL: https://github.com/apache/logging-log4j2/pull/1136#discussion_r1028270272


##########
log4j-api/src/main/java/org/apache/logging/log4j/spi/LoggingSystem.java:
##########
@@ -0,0 +1,363 @@
+/*
+ * 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 org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.ThreadContext;
+import org.apache.logging.log4j.simple.SimpleLoggerContextFactory;
+import org.apache.logging.log4j.util.Constants;
+import org.apache.logging.log4j.util.Lazy;
+import org.apache.logging.log4j.util.LoaderUtil;
+import org.apache.logging.log4j.util.LowLevelLogUtil;
+import org.apache.logging.log4j.util.PropertiesUtil;
+import org.apache.logging.log4j.util.PropertyEnvironment;
+import org.apache.logging.log4j.util.ServiceRegistry;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.invoke.MethodHandles;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.net.URL;
+import java.util.List;
+import java.util.Objects;
+import java.util.Properties;
+import java.util.SortedMap;
+import java.util.TreeMap;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+import java.util.function.Supplier;
+import java.util.stream.Collectors;
+
+/**
+ * Handles initializing the Log4j API through {@link Provider} discovery. This keeps track of which
+ * {@link LoggerContextFactory} to use in {@link LogManager} along with factories for {@link ThreadContextMap}
+ * and {@link ThreadContextStack} to use in {@link ThreadContext}.
+ *
+ * @since 3.0.0
+ */
+public class LoggingSystem {
+    /**
+     * Resource name for a Log4j 2 provider properties file.
+     */
+    private static final String PROVIDER_RESOURCE = "META-INF/log4j-provider.properties";
+    private static final String API_VERSION = "Log4jAPIVersion";
+    private static final String[] COMPATIBLE_API_VERSIONS = {"3.0.0"};
+
+    public static final String THREAD_CONTEXT_MAP_DISABLED = "log4j2.disableThreadContextMap";
+    public static final String THREAD_CONTEXT_STACK_DISABLED = "log4j2.disableThreadContextStack";
+    public static final String THREAD_CONTEXT_DISABLED = "log4j2.disableThreadContext";
+    /**
+     * Property name ({@value} ) for selecting {@code InheritableThreadLocal} (value "true") or plain
+     * {@code ThreadLocal} (value is not "true") in the {@link ThreadContextMap} implementation.
+     */
+    public static final String THREAD_CONTEXT_MAP_INHERITABLE_ENABLED = "log4j2.isThreadContextMapInheritable";
+    public static final String THREAD_CONTEXT_MAP_CLASS_NAME = "log4j2.threadContextMap";
+    public static final String THREAD_CONTEXT_INITIAL_CAPACITY = "log4j2.threadContextInitialCapacity";
+    public static final int THREAD_CONTEXT_DEFAULT_INITIAL_CAPACITY = 16;
+    public static final String THREAD_CONTEXT_GARBAGE_FREE_ENABLED = "log4j2.garbagefreeThreadContextMap";
+
+    private static final Lazy<LoggingSystem> SYSTEM = Lazy.relaxed(LoggingSystem::new);
+
+    private final Lock initializationLock = new ReentrantLock();
+    private volatile LoggingProvider provider;
+    private final Lazy<LoggerContextFactory> loggerContextFactorySupplier = Lazy.lazy(() -> getProvider().createLoggerContextFactory());
+    private Supplier<ThreadContextMap> threadContextMapSupplier = () -> getProvider().createContextMap();
+    private Supplier<ThreadContextStack> threadContextStackSupplier = () -> getProvider().createContextStack();
+
+    /**
+     * Acquires a lock on the initialization of locating a logging system provider. This lock should be
+     * {@linkplain #releaseInitializationLock() released} once the logging system provider is loaded. This lock is
+     * provided to allow for lazy initialization via frameworks like OSGi to wait for a provider to be installed
+     * before allowing initialization to continue.
+     *
+     * @see <a href="https://issues.apache.org/jira/browse/LOG4J2-373">LOG4J2-373</a>
+     */
+    public void acquireInitializationLock() {

Review Comment:
   This class is not marked internal in any way. Exposing locks publicly like this is very dangerous.  It would make more sense to me to provide a method that accepts a Lambda to performed while the lock is held.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org