Skip to content

Fix servlet context resolution in composite configuration#4070

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

Fix servlet context resolution in composite configuration#4070
ramanathan1504 wants to merge 1 commit intoapache:2.xfrom
ramanathan1504:issue-2351

Conversation

@ramanathan1504
Copy link
Copy Markdown
Contributor

@ramanathan1504 ramanathan1504 commented Mar 19, 2026

Fixes #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.

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.

@ppkarwasz, this entire Map.Entry family existing in LC method footprints appear like an ugly hack to me, yet it is public. That is, we cannot change it. @ramanathan1504's proposal in this PR looks legitimate to me. I'd appreciate a 2nd pair of eyes on this change. Would you mind quickly skimming through it and see if you can spot anything suspicious, please?

@vy vy changed the title [LOG4J2-2351] Fix servlet context resolution in composite configurati… Fix servlet context resolution in composite configuration Apr 30, 2026
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.

Missing servlet context in web lookup when using composite configuration

2 participants