We call log4j from JSP and from EJB.
We use a servlet to startup log4j: a servlet parameter passes the
configuration file name.

Here is the code:

public class InitLog4J extends HttpServlet {
    private static final String PARAM_NAME_CONFIG_FILE =
"configuration.file";

    public void init(ServletConfig sconf) throws ServletException {
        super.init(sconf);
        //retrieve configuration file name
        String cfg_file = sconf.getInitParameter(PARAM_NAME_CONFIG_FILE);
        //check init parameter exists, else return
        if (cfg_file == null) {
            System.err.println("missing configuration parameter in web.xml:
"
                               + PARAM_NAME_CONFIG_FILE);
            System.err.println("Log4J will not be initialized.");
            return;
        }
        //configure log4j
        PropertyConfigurator.configure(cfg_file);
    }
}

Add this to your web.xml
        <servlet>
                <servlet-name>foo.bar.servlet.InitLog4J</servlet-name>
                <display-name>Initialization Log4J</display-name>
                <servlet-class>foo.bar.servlet.InitLog4J</servlet-class>
                <init-param>
                        <param-name>configuration.file</param-name>
                        <param-value>c:\path\logging.cfg</param-value>
                </init-param>
                <load-on-startup>1</load-on-startup>
        </servlet>

In our case, logging.cfg is a PropertyConfigurator file.


Have fun,


Yves Bossel

Reply via email to