Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of LoggerContextAware lookups #2313

Merged
merged 1 commit into from
Mar 4, 2024

Conversation

ppkarwasz
Copy link
Contributor

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.

@ppkarwasz ppkarwasz added the bug Incorrect, unexpected, or unintended behavior of existing code label Feb 22, 2024
@ppkarwasz ppkarwasz added this to the 2.23.1 milestone Feb 22, 2024
@ppkarwasz ppkarwasz requested a review from vy February 22, 2024 11:49
@ppkarwasz ppkarwasz self-assigned this Feb 22, 2024
@ppkarwasz ppkarwasz removed this from the 2.23.1 milestone Feb 22, 2024
@vy
Copy link
Member

vy commented Feb 23, 2024

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

@vy vy added this to the 2.23.1 milestone Feb 23, 2024
Comment on lines +212 to +220
public void setConfiguration(final Configuration configuration) {
super.setConfiguration(configuration);
// Propagate
for (final StrLookup lookup : strLookupMap.values()) {
if (lookup instanceof ConfigurationAware) {
((ConfigurationAware) lookup).setConfiguration(configuration);
}
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

public void setVariableResolver(final StrLookup variableResolver) {
if (variableResolver instanceof ConfigurationAware && this.configuration != null) {
((ConfigurationAware) variableResolver).setConfiguration(this.configuration);
}
this.variableResolver = variableResolver;
}

where the side effect of the call seems surprising.

Nevertheless I am not going to modify the second example, maybe sometimes it is necessary.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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.
@ppkarwasz ppkarwasz force-pushed the fix/2309_logger_context_aware_lookups branch from 9cdb359 to 395a8f4 Compare March 4, 2024 10:22
@ppkarwasz ppkarwasz merged commit 395a8f4 into 2.x Mar 4, 2024
7 checks passed
@ppkarwasz ppkarwasz deleted the fix/2309_logger_context_aware_lookups branch March 4, 2024 10:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Incorrect, unexpected, or unintended behavior of existing code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

LoggerContextAware lookups fail in log4j2 2.23.0
3 participants