K
KineticUncertainty
Guest
KineticUncertainty Asks: Add context value into web.xml
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
I have a bean setup that's grabbing the context entry. This is working fine.
I can also get the context value this way if its easier to use.
in my web.xml
is there a way to get that value into the file:$(location) ?
I have been stuck trying to figure this out for days and exhausting my googling
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
public class Log4jLoader {
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>-->
is there a way to get that value into the file:$(location) ?
I have been stuck trying to figure this out for days and exhausting my googling
SolveForum.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your thoughts here to help others.