[LOG4J2-2351] Fix servlet context resolution in composite configurati…#4070
[LOG4J2-2351] Fix servlet context resolution in composite configurati…#4070ramanathan1504 wants to merge 1 commit intoapache:2.xfrom
Conversation
…on for web lookups
| if (externalContext != null && ctx.getExternalContext() == null) { | ||
| ctx.setExternalContext(externalContext); | ||
| final LoggerContext ctx; | ||
| if (externalContext instanceof Map.Entry) { |
There was a problem hiding this comment.
@ramanathan1504, would you mind pointing me the code calling this method with an externalContext of type Map.Entry, please?
There was a problem hiding this comment.
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.
#2351
Fixed composite web initialization so
${web:servletContextName}resolves correctly whenlog4jConfigurationcontains 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-teston Tomcat 11 withlog4j-jakarta-webusingcomposite.jspandsingle.jsp.Result: composite and single configuration paths now resolve servlet context consistently.