Pregunta

Using a JVM variable in log4j.xml configuration does not work...

System.setProperty("log_dir", logDirPath);

<param name="file" value="${log_dir}\toto.log" />

It works if I reload my web server while it is running in debug...

** UPDATE

package com.webapp.startup;

import java.io.PrintStream;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import com.webapp.objects.impl.LoggingOutputStream;

public class Log4jStartup implements ServletContextListener{

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
    }

    @Override
    public void contextInitialized(ServletContextEvent event) {
        try {
            ServletContext context = event.getServletContext();

            System.setProperty("log_dir", "C:/path");

            System.setErr(new PrintStream(new LoggingOutputStream(Logger.getRootLogger(), Level.ERROR), true));
            System.setOut(new PrintStream(new LoggingOutputStream(Logger.getRootLogger(), Level.INFO), true));

            String path = context.getRealPath("/WEB-INF/classes/log4j.xml");
        PropertyConfigurator.configure(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Initializing the property before setErr and setOut... Thanks to all !

¿Fue útil?

Solución

You are probably populating that property after the log4j system has been initialized.

In order to have it done before, you can either supply it from the command line when starting the server, or use an initialization servlet like in this example. Scroll to Default Initialization under Tomcat and keep in mind that you only need to specify the property instead of the log4j config file.

Otros consejos

You should tell us when this property is set but nevertheless it will be too late. Log4j is typically initialized very early before this property is set. I recommend you to set this parameter in initialization block of your web server start script. Use -Dlog_dir=path syntax.

PS what server is it? Tomcat?

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top