Question

Please help how to handle the below scenario.

Current Web app : I have placed log4j.propertes in /WEB-INF/classes folder of webapp.

Wanted to achieve : Place log4j.properties in an external location and refer from my web application, so that i can modify the log location when ever i want without re building the war.

$CATALINA_HOME/propdirwebapp1/log4j.properties $CATALINA_HOME/propdirwebapp2/log4j.properties

If i have multiple applications deployed please suggest the solution. If only one application is deployed on server please suggest the solution

Was it helpful?

Solution

In my case in web.xml I added Log4jConfigListener and it worked. log4jRefreshInterval is optional here.

<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>file:/<your-path-here>/log4j.properties</param-value>
</context-param>
<context-param>
    <param-name>log4jRefreshInterval</param-name>
    <!-- Refresh log4j configuration every 5 minutes. -->
    <param-value>300000</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

OTHER TIPS

There is no need to rebuild war file, instead of that just directly modify log4j.properties and restart the tomcat.

You could add a directory to shared.loader property in the $CATALINA_BASE/conf/catalina.properties.

shared.loader=conf/myapp/

Then put the log4j.properties in this directory.

But there are two constraints.

1) If there is a log4j.properties file in the WEBINF/classes that one will get loaded first

2) This directory gets shared by all webapps so there can only by one log4j.properties file for all webapps.

I like my attempt, it just deals with log levels for different war applications.

Place a custom properties file in conf, for example

$CATALINA_BASE/conf/my.properties

Create entries like

myapp.loglevel = INFO
myapp2.loglevel = DEBUG

Read the props per request, for example

myProps = new Properties();
myProps.load(new FileInputStream(new File(System.getProperty("catalina.base"), "conf/my.properties")));

Read and use the property like for example in a servlet at the beginning of a request

String myLoglevel = (String)myProps.get("myapp.loglevel");
LogManager.getRootLogger().setLevel(Level.toLevel(myLoglevel));

We're using slf4j-log4j12 and I've read there's no LogManager in log4j2, but I'm sure this mechanism could be applied to log4j2, too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top