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


##########
log4j-api/src/main/java/org/apache/logging/log4j/internal/ScopedContextAnchor.java:
##########
@@ -0,0 +1,69 @@
+/*
+ * 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.internal;
+
+import java.util.ArrayDeque;
+import java.util.Deque;
+import java.util.Optional;
+import org.apache.logging.log4j.ScopedContext;
+
+/**
+ * Anchor for the ScopedContext. This class is private and not for public 
consumption.
+ */
+public class ScopedContextAnchor {
+    private static final ThreadLocal<Deque<ScopedContext>> scopedContext = new 
ThreadLocal<>();

Review Comment:
   1. This is NOT meant for only Java 21 and above. It is targeted at Log4j API 
which still supports Java 8. Note that it could be possible to provide a 
different implementation for Java 21 if it makes sense to do so. 
   2. ThreadLocals are NOT risky even with Virtual Threads when they are used 
in this way. I specifically looked for a mention of being able to disable 
ThreadLocals and could not find anything as I recall you previously mentioning 
that. However, they should NOT be used to cache objects as we have done in the 
past. Instead, "normal" object pooling should be used. I will also say that in 
my first incarnation I used a Map where the key was the threadId. But I changed 
that since it is nothing more than a slower version of a ThreadLocal.
   
   As an aside, I looked at the ScopedValue javadoc and some examples of how to 
use it. I am not overly impressed. To be honest I would prefer ScopedContext 
since it is quite a bit simpler and shouldl be more powerful once I have 
finished.



-- 
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-unsubscr...@logging.apache.org

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

Reply via email to