logging-log4j2 icon indicating copy to clipboard operation
logging-log4j2 copied to clipboard

`LoggerContextAware` lookups fail in log4j2 2.23.0

Open bright-k opened this issue 1 year ago • 3 comments

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>

bright-k avatar Feb 22 '24 02:02 bright-k

@bright-k,

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

ppkarwasz avatar Feb 22 '24 06:02 ppkarwasz

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

bright-k avatar Feb 22 '24 06:02 bright-k

@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 avatar Feb 22 '24 11:02 ppkarwasz