Hi There

We use multiple web applications running inside Tomcat and need a painless way for each app to write to its own log file.

In our scenario, each web application has its own log4j.xml file and the corresponding log file for each instance is stored in a unique location
for that application, namely:

WEBAPP/WEB-INF/logs

(so that each web application has its own log information)

Our log4j.xml file has an appender that looks as follows:

<appender name="debugAppender" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${ROOT}/WEB-INF/logs/debug.log"/>
<param name="MaxFileSize" value="100MB"/>
<param name="MaxBackupIndex" value="1"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%5p %d{MMM/dd HH:mm:ss} - %m%n"/>
</layout>
</appender>

You'll notice above, we refer to the system property {$ROOT} in the log4j.xml file. This Java system variable is set when the web application is first initialized. The variable during startup as follows:

public class AppServletContext implements ServletContextListener{
    ServletContext context;

    public void contextInitialized(ServletContextEvent event) {
        ServletContext context = event.getServletContext();
        String path = context.getRealPath(File.separator);
        if (path.endsWith(File.separator))
            path = path.substring(0,path.length()-1);
        int i = path.lastIndexOf(File.separator);
        String name = path.substring(i+1,path.length());
        System.setProperty(name,path);
    }

    public void contextDestroyed(ServletContextEvent event ) {
    }
}

The effect of the above is to set a Java System Property with the name of the instance (e.g. ROOT) and value of the context path on startup. In this way, we are able to refer to the context path, from within the log4j.xml file. Thus, if the name of the web application being loaded is appname... then the Java system system variable APPNAME will be set, and we would create a log4j.xml with the variable ${APPNAME} to refer to the application path.

While this approach does work, it requires us to update the log4j.xml for each instance. We need to change ${ROOT} to ${webappname}. This is a pain when there are alot of web applications and each of their log4j.xml files need to be updated. Are there any other ideas on how to refer to the current Tomcat web application path from with the log4j.xml without having to customize a log4j.xml file for each web application. I need a simpler more generic way, such as <param name="File" value="./WEB-INF/logs/debug.log"/>. Perhaps, there is a simpler way that I missed.

Thanks in advance for your consideration

Jamie



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to