vy commented on code in PR #97:
URL: 
https://github.com/apache/logging-log4j-scala/pull/97#discussion_r2652916255


##########
log4j-api-scala_2.10/src/main/scala/org/apache/logging/log4j/scala/LoggingContext.scala:
##########
@@ -81,4 +81,23 @@ object LoggingContext extends mutable.Map[String, String] {
 
   override def isEmpty: Boolean = ThreadContext.isEmpty
 
+  /**
+   * Runs the given block with the provided context data effectively added to 
the
+   * {@link org.apache.logging.log4j.ThreadContext ThreadContext} and removed 
after the block completes.

Review Comment:
   ```suggestion
      * {@link ThreadContext} and removed after the block completes.
   ```



##########
log4j-api-scala_2.13/src/main/scala/org/apache/logging/log4j/scala/LoggingContext.scala:
##########
@@ -72,4 +72,23 @@ object LoggingContext extends mutable.Map[String, String] {
 
   override def isEmpty: Boolean = ThreadContext.isEmpty
 
+  /**
+   * Runs the given block with the provided context data effectively added to 
the
+   * {@link org.apache.logging.log4j.ThreadContext ThreadContext} and removed 
after the block completes.

Review Comment:
   ```suggestion
      * {@link ThreadContext} and removed after the block completes.
   ```



##########
log4j-api-scala_2.10/src/main/scala/org/apache/logging/log4j/scala/LoggingContext.scala:
##########
@@ -81,4 +81,23 @@ object LoggingContext extends mutable.Map[String, String] {
 
   override def isEmpty: Boolean = ThreadContext.isEmpty
 
+  /**
+   * Runs the given block with the provided context data effectively added to 
the
+   * {@link org.apache.logging.log4j.ThreadContext ThreadContext} and removed 
after the block completes.
+   *
+   * @param context the map of key-value pairs to add to the context
+   * @param body the block of code to execute
+   * @tparam R the return type of the block
+   * @return the result of the block
+   * @since 13.2.0
+   */
+  def withContext[R](context: Map[String, String])(body: => R): R = {
+    ThreadContext.putAll(context.asJava)
+    try {
+      body
+    } finally {
+      ThreadContext.removeAll(context.keys.asJavaCollection)

Review Comment:
   @SpaceLeam, I don't think `putAll(); try { body } finally { removeAll(); }` 
is correct. Consider a key that already existed in the Thread Context before 
`putAll()` invocation and you're overriding it. Just running `removeAll()` will 
not restore the initial state. See how Log4j API achieves the same using 
[CloseableThreadContext](https://github.com/apache/logging-log4j2/blob/2.x/log4j-api/src/main/java/org/apache/logging/log4j/CloseableThreadContext.java).



##########
log4j-api-scala_2.12/src/main/scala/org/apache/logging/log4j/scala/LoggingContext.scala:
##########
@@ -81,4 +81,23 @@ object LoggingContext extends mutable.Map[String, String] {
 
   override def isEmpty: Boolean = ThreadContext.isEmpty
 
+  /**
+   * Runs the given block with the provided context data effectively added to 
the
+   * {@link org.apache.logging.log4j.ThreadContext ThreadContext} and removed 
after the block completes.

Review Comment:
   ```suggestion
      * {@link ThreadContext} and removed after the block completes.
   ```



-- 
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]

Reply via email to