You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@logging.apache.org by "ppkarwasz (via GitHub)" <gi...@apache.org> on 2024/02/22 11:49:22 UTC

[PR] Fix handling of `LoggerContextAware` lookups (logging-log4j2)

ppkarwasz opened a new pull request, #2313:
URL: https://github.com/apache/logging-log4j2/pull/2313

   Due to the changes in #2278 `LoggerContextAware` lookups stopped working in `2.23.0`.
   
   This PR:
   
    * fixes the NPE in `Interpolator` that occurs if `Interpolator#setLoggerContext` was **not** called after instantiation.
    * Calls `Interpolator#setConfiguration` and `Interpolator#setLoggerContext` wherever it is possible.
    * Changes the way `Interpolator` propagates `Configuration` and `LoggerContext` to child lookups. Previously this occurred at each evaluation, now it occurs only in the setters.
   
   Closes #2309.
   


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


Re: [PR] Fix handling of `LoggerContextAware` lookups (logging-log4j2)

Posted by "vy (via GitHub)" <gi...@apache.org>.
vy commented on code in PR #2313:
URL: https://github.com/apache/logging-log4j2/pull/2313#discussion_r1500934662


##########
log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java:
##########
@@ -214,11 +209,25 @@ public LookupResult evaluate(final LogEvent event, String var) {
     }
 
     @Override
-    public void setLoggerContext(final LoggerContext loggerContext) {
-        if (loggerContext == null) {
-            return;
+    public void setConfiguration(final Configuration configuration) {
+        super.setConfiguration(configuration);
+        // Propagate
+        for (final StrLookup lookup : strLookupMap.values()) {
+            if (lookup instanceof ConfigurationAware) {
+                ((ConfigurationAware) lookup).setConfiguration(configuration);
+            }
         }
+    }

Review Comment:
   I find the `*Aware` design pattern a footgun. As is visibly the case here, it is not clear where one should check the instance type and on which values. The more such a contract is used, the more the probability that an `instanceof` check is missing.
   
   I sadly need to agree that your usage is acceptable given current state of the things.



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


Re: [PR] Fix handling of `LoggerContextAware` lookups (logging-log4j2)

Posted by "vy (via GitHub)" <gi...@apache.org>.
vy commented on PR #2313:
URL: https://github.com/apache/logging-log4j2/pull/2313#issuecomment-1960944131

   @ppkarwasz, thanks for considering me for the review. Unfortunately I am not much familiar with the parts you touch, hence I doubt if my review will be meaningful. I trust your judgement and suggest to proceed as you wish.


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


Re: [PR] Fix handling of `LoggerContextAware` lookups (logging-log4j2)

Posted by "ppkarwasz (via GitHub)" <gi...@apache.org>.
ppkarwasz commented on code in PR #2313:
URL: https://github.com/apache/logging-log4j2/pull/2313#discussion_r1500493356


##########
log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java:
##########
@@ -214,11 +209,25 @@ public LookupResult evaluate(final LogEvent event, String var) {
     }
 
     @Override
-    public void setLoggerContext(final LoggerContext loggerContext) {
-        if (loggerContext == null) {
-            return;
+    public void setConfiguration(final Configuration configuration) {
+        super.setConfiguration(configuration);
+        // Propagate
+        for (final StrLookup lookup : strLookupMap.values()) {
+            if (lookup instanceof ConfigurationAware) {
+                ((ConfigurationAware) lookup).setConfiguration(configuration);
+            }
         }
+    }

Review Comment:
   @vy,
   
   This is the part I wanted to consult you on. IMHO it is bad practice when methods called `set*` do anything else than set a single value.
   
   I think in this case the usage is acceptable, compared to:
   
   https://github.com/apache/logging-log4j2/blob/f05864d4acf4198ec164e8ddfc2e325ffde21877/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StrSubstitutor.java#L1490-L1495
   
   where the side effect of the call seems surprising.
   
   Nevertheless I am not going to modify the second example, maybe sometimes it is necessary.



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


Re: [PR] Fix handling of `LoggerContextAware` lookups (logging-log4j2)

Posted by "ppkarwasz (via GitHub)" <gi...@apache.org>.
ppkarwasz merged PR #2313:
URL: https://github.com/apache/logging-log4j2/pull/2313


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