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

LoggerContextAware lookups fail in log4j2 2.23.0 #2309

Closed
bright-k opened this issue Feb 22, 2024 · 3 comments · Fixed by #2313
Closed

LoggerContextAware lookups fail in log4j2 2.23.0 #2309

bright-k opened this issue Feb 22, 2024 · 3 comments · Fixed by #2313
Labels
bug Incorrect, unexpected, or unintended behavior of existing code
Milestone

Comments

@bright-k
Copy link

bright-k commented Feb 22, 2024

Description

After updating to log4j2 version 2.23.0 while using Spring Boot 3.2.2, all settings that use Spring properties fail.

It all worked fine in versions prior to 2.21.1.

Is this an intended change, or is it a bug?

Configuration

Version: 2.23.0

Operating system: mac

JDK: jdk 21

Logs

2024-02-22T02:30:17.734540Z main ERROR Resolver failed to lookup spring:app.id java.lang.NullPointerException: Cannot invoke "java.lang.ref.WeakReference.get()" because "this.loggerContext" is null
	at org.apache.logging.log4j.core.lookup.Interpolator.evaluate(Interpolator.java:198)
	at org.apache.logging.log4j.core.lookup.StrSubstitutor.resolveVariable(StrSubstitutor.java:1227)
	at org.apache.logging.log4j.core.lookup.StrSubstitutor.substitute(StrSubstitutor.java:1138)
	at org.apache.logging.log4j.core.lookup.StrSubstitutor.substitute(StrSubstitutor.java:999)
	at org.apache.logging.log4j.core.lookup.StrSubstitutor.replace(StrSubstitutor.java:513)
	at org.apache.logging.log4j.core.config.plugins.visitors.PluginAttributeVisitor.visit(PluginAttributeVisitor.java:40)
	at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.generateParameters(PluginBuilder.java:294)
	at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:141)
	at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:1163)
	at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:1084)
	at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:1076)
	at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:680)
	at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:264)
	at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:313)
	at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:631)
	at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:292)
	at org.springframework.boot.logging.log4j2.Log4J2LoggingSystem.loadConfiguration(Log4J2LoggingSystem.java:295)
	at org.springframework.boot.logging.log4j2.Log4J2LoggingSystem.load(Log4J2LoggingSystem.java:267)
	at org.springframework.boot.logging.log4j2.Log4J2LoggingSystem.loadConfiguration(Log4J2LoggingSystem.java:259)
	at org.springframework.boot.logging.AbstractLoggingSystem.initializeWithSpecificConfig(AbstractLoggingSystem.java:67)
	at org.springframework.boot.logging.AbstractLoggingSystem.initialize(AbstractLoggingSystem.java:58)
	at org.springframework.boot.logging.log4j2.Log4J2LoggingSystem.initialize(Log4J2LoggingSystem.java:246)
	at org.springframework.boot.context.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:335)
	at org.springframework.boot.context.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:298)
	at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:246)
	at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:223)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:178)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:171)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:149)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:137)
	at org.springframework.boot.context.event.EventPublishingRunListener.multicastInitialEvent(EventPublishingRunListener.java:136)
	at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:81)
	at org.springframework.boot.SpringApplicationRunListeners.lambda$environmentPrepared$2(SpringApplicationRunListeners.java:64)
	at java.base/java.lang.Iterable.forEach(Iterable.java:75)
	at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:118)
	at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:112)
	at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:63)
	at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:369)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:329)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1354)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343)
	at com.tossplace.spring.boot.web.test.TossplaceSpringBootMvcApplicationKt.main(Application.kt:20)

Reproduction

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
    <Properties>
        <Property name="SERVICE_NAME">${spring:app.id}</Property>   <!-- <- All configurations of this type fail. -->
    </Properties>
    <Appenders>
        <Slack
            appId="${spring:app.id}"    <!-- <- All configurations of this type fail. -->
            ...
        >
            ...
        </Slack>
    </Appenders>
    ....
</Configuration>
@ppkarwasz ppkarwasz added the bug Incorrect, unexpected, or unintended behavior of existing code label Feb 22, 2024
@ppkarwasz
Copy link
Contributor

@bright-k,

Thanks for reporting it. It seems to be a bug introduced in #2278. We'll fix it in the next release.

@bright-k
Copy link
Author

@ppkarwasz

It seems that the PR in the link has already been merged, so it might not be exactly the same issue, but can you explain what might be causing the problem that's occurring?

@ppkarwasz
Copy link
Contributor

@bright-k,

The PR #2278 didn't fix the bug, it introduced it. The SpringLookup is LoggerContextAware and a call to LoggerContextAware#setLoggerContext was unintentionally removed, causing the problem.

ppkarwasz added a commit that referenced this issue Feb 22, 2024
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 changed the title Failure of Spring properties configuration in log4j2 2.23.0 LoggerContextAware lookups fail in log4j2 2.23.0 Feb 22, 2024
@ppkarwasz ppkarwasz added this to the 2.23.1 milestone Feb 22, 2024
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 a pull request may close this issue.

2 participants