rgoers commented on code in PR #2438: URL: https://github.com/apache/logging-log4j2/pull/2438#discussion_r1561821021
########## log4j-api/src/main/java/org/apache/logging/log4j/ScopedContext.java: ########## @@ -0,0 +1,225 @@ +/* + * 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; + +import java.util.Map; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Future; +import java.util.function.Supplier; +import org.apache.logging.log4j.spi.ScopedContextProvider; +import org.apache.logging.log4j.util.ProviderUtil; + +/** + * Context that can be used for data to be logged in a block of code. + * <p> + * While this is influenced by ScopedValues from Java 21 it does not share the same API. While it can perform a + * similar function as a set of ScopedValues it is really meant to allow a block of code to include a set of keys and + * values in all the log events within that block. The underlying implementation must provide support for + * logging the ScopedContext for that to happen. + * </p> + * <p> + * The ScopedContext will not be bound to the current thread until either a run or call method is invoked. The + * contexts are nested so creating and running or calling via a second ScopedContext will result in the first + * ScopedContext being hidden until the call is returned. Thus the values from the first ScopedContext need to + * be added to the second to be included. + * </p> + * <p> + * The ScopedContext can be passed to child threads by including the ExecutorService to be used to manage the + * run or call methods. The caller should interact with the ExecutorService as if they were submitting their + * run or call methods directly to it. The ScopedContext performs no error handling other than to ensure the + * ThreadContext and ScopedContext are cleaned up from the executed Thread. + * </p> + * @since 2.24.0 + */ +public final class ScopedContext { + + private static final ScopedContextProvider provider = + ProviderUtil.getProvider().getScopedContextProvider(); + + private ScopedContext() {} + + /** + * Creates a ScopedContext Instance with a key/value pair. + * + * @param key the key to add. + * @param value the value associated with the key. + * @return the Instance constructed if a valid key and value were provided. Otherwise, either the + * current Instance is returned or a new Instance is created if there is no current Instance. + */ + public static Instance where(final String key, final Object value) { + return provider.newScopedContext(key, value); + } + + /** + * Adds a key/value pair to the ScopedContext being constructed. + * + * @param key the key to add. + * @param supplier the function to generate the value. + * @return the ScopedContext being constructed. + */ + public static Instance where(final String key, final Supplier<Object> supplier) { + return where(key, supplier.get()); + } + + /** + * Creates a ScopedContext Instance with a Map of keys and values. + * @param map the Map. + * @return the ScopedContext Instance constructed. + */ + public static Instance where(final Map<String, ?> map) { + return provider.newScopedContext(map); + } + + /** + * Creates a ScopedContext with a single key/value pair and calls a method. + * @param key the key. + * @param value the value associated with the key. + * @param task the Runnable to call. + */ + public static void runWhere(final String key, final Object value, final Runnable task) { + provider.newScopedContext(key, value).run(task); + } + + /** + * Creates a ScopedContext with a single key/value pair and calls a method on a separate Thread. + * @param key the key. + * @param value the value associated with the key. + * @param executorService the ExecutorService to dispatch the work. + * @param task the Runnable to call. + */ + public static Future<Void> runWhere( + final String key, final Object value, final ExecutorService executorService, final Runnable task) { + return provider.newScopedContext(key, value).run(executorService, task); + } + + /** + * Creates a ScopedContext with a Map of keys and values and calls a method. + * @param map the Map. + * @param task the Runnable to call. + */ + public static void runWhere(final Map<String, ?> map, final Runnable task) { + provider.newScopedContext(map).run(task); + } + + /** + * Creates a ScopedContext with a single key/value pair and calls a method. + * @param key the key. + * @param value the value associated with the key. + * @param task the Runnable to call. + */ + public static <R> R callWhere(final String key, final Object value, final Callable<R> task) throws Exception { + return provider.newScopedContext(key, value).call(task); + } + + /** + * Creates a ScopedContext with a single key/value pair and calls a method on a separate Thread. + * @param key the key. + * @param value the value associated with the key. + * @param executorService the ExecutorService to dispatch the work. + * @param task the Callable to call. + */ + public static <R> Future<R> callWhere( + final String key, final Object value, final ExecutorService executorService, final Callable<R> task) { + return provider.newScopedContext(key, value).call(executorService, task); + } + + /** + * Creates a ScopedContext with a Map of keys and values and calls a method. + * @param map the Map. + * @param task the Runnable to call. + */ + public static <R> R callWhere(final Map<String, ?> map, final Callable<R> task) throws Exception { + return provider.newScopedContext(map).call(task); + } + + /** + * Return the object with the specified key from the current context. + * @param key the key. + * @return the value of the key or null. + * @param <T> The type of object expected. + * @throws ClassCastException if the specified type does not match the object stored. + */ + @SuppressWarnings("unchecked") + public static <T> T get(String key) { + return (T) provider.getValue(key); + } Review Comment: @vy with all due respect, get off your high horse. Applications have been using the MDC this way for years and will also use the ScopedContext to do the same, only in a safer manner. They (and I) are not going to stop doing it just because you say so. You are about 20 years too late to argue against the MDC. Arguing against a get method in ScopedContext for this reason is simply silly because all you are accomplishing is saying that users have to continue using something that requires much more care to use safely. I should note that the reason for doing this instead of using a custom ThreadLocal is really, really simple. As a user I WANT to include the data in logs AND I want to be able to access it from my app. I do NOT want to have to store it in the ThreadContext AND in a private ThreadLocal. One other thought. %X{tenantId} doesn't work without a get method. Neither does a Filter. FYI - See - https://issues.apache.org/jira/browse/LOG4J2-1648 from 2016 "Add methods to the ThreadContext facade that allow getting and putting Object values" - https://lists.apache.org/thread/96so2w5k2p3m6pg6hrjq980cowq3cz0n (I would REALLY love to see the requests Volkan. I can assure you they did NOT want to put objects in the ThreadContextMap without being able to retrieve them as there is no point to that). - https://jira.qos.ch/browse/SLF4J-168 - (is only about logging) -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
