# Add context value into web.xml

K

#### KineticUncertainty

##### Guest
I have an application where i defined the location to the external log4j2.properties. I have defined the Log4jServletContextListener in the web.xml and if i provide a path to the file it works. I am trying to figure out how to use a place holder and get the value of the context into it. Can any one help?

Here is my tomcat context entry

Code:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Environment name="demo/log4j2.location" type="java.lang.String" value="C:/conf/log4j2.xml"/>
</Context>

I have a bean setup that's grabbing the context entry. This is working fine.

Code:
@Bean("Logging")
public String getConfig(
@Value("${demo/log4j2.location}") String log4jConfigLocation ){ validate(log4jConfigLocation, not(equalTo("${demo/log4j2.location}")));
return log4jConfigLocation;
}

I can also get the context value this way if its easier to use.

Code:
@Component

String location;

public  Log4jLoader(@Value("${construct-demo/log4j2.location}") String location){ this.location = location; loadLog4jConfig(); } private void loadLog4jConfig() { in my web.xml Code: <context-param> <param-name>isLog4jAutoInitializationDisabled</param-name> <param-value>true</param-value> </context-param> <listener> <listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class> </listener> <context-param> <param-name>log4jContextName</param-name> <param-value>demo</param-value> </context-param> <context-param> <param-name>log4jConfiguration</param-name> <param-value>file:$(location)</param-value>
</context-param>

THIS WORKS
<!--<context-param>-->
<!--<param-name>log4jConfiguration</param-name>-->
<!--<param-value>file:///C:/conf/log4j2.xml</param-value>-->
<!--</context-param>-->