Skip to content

[LOG4J2-2351] Fix servlet context resolution in composite configurati…#4070

Open
ramanathan1504 wants to merge 1 commit intoapache:2.xfrom
ramanathan1504:issue-2351
Open

[LOG4J2-2351] Fix servlet context resolution in composite configurati…#4070
ramanathan1504 wants to merge 1 commit intoapache:2.xfrom
ramanathan1504:issue-2351

Conversation

@ramanathan1504
Copy link
Copy Markdown
Contributor

#2351
Fixed composite web initialization so ${web:servletContextName} resolves correctly when log4jConfiguration contains multiple files.
Before fix, composite mode (log4j2-foo.xml,log4j2-bar.xml) left the lookup unresolved, while single-file mode worked.
After local patch (2.26.0-SNAPSHOT), composite output now matches the expected servlet context name.
Single-file control behavior remains correct and unchanged.
Validated in log4j2-web-test on Tomcat 11 with log4j-jakarta-web using composite.jsp and single.jsp.
Result: composite and single configuration paths now resolve servlet context consistently.

if (externalContext != null && ctx.getExternalContext() == null) {
ctx.setExternalContext(externalContext);
final LoggerContext ctx;
if (externalContext instanceof Map.Entry) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@ramanathan1504, would you mind pointing me the code calling this method with an externalContext of type Map.Entry, please?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@vy

The externalContext is passed as a Map.Entry<String, Object> from the log4j-web module.
In Log4jWebInitializerImpl.java (lines 177 and 183), it calls:
WebLoggerContextUtils.createExternalEntry(this.servletContext)

This returns the Map.Entry which is then passed through Configurator.initialize(...) down to the Log4jContextFactory.

I used instanceof Map.Entry in the Factory to ensure that when composite configurations are used, the externalContext is correctly unwrapped and set so that WebLookup can find the ServletContext. However, I'm open to moving this logic to Configurator or elsewhere if you feel the Factory should remain agnostic of this wrapper.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

2 participants