If you have a web application which you want to deploy more than once to a single servlet container you have the problem of separating your log files for the different web app instances. This is because the log configuration file is located in your WAR-file and it is not easy to change this file during the build process.
But there is a simple way of defining the log filename based on the context path of your web application. Simply set the contextPath as a system property in a ServletContextListener:
public class ContextDependentInitializer implements ServletContextListener {
public void contextInitialized(ServletContextEvent event) {
ServletContext context = event.getServletContext();
String contextPath = context.getContextPath();
System.setProperty("contextPath", contextPath);
}
}
After that you have to declare the listener in your web.xml as follows:
<web-app>
<listener>
<listener-class>de.oio.logging.ContextDependentInitializer</listener-class>
</listener>
</web-app>
Then you are able to use the context path name to define the log filename in your Log4J configuration file. For example:
log4j.appender.LOG.file=${catalina.base}/logs/${contextPath}.log