Tomcat only reads Log4J.properties when rebooted

4

Until recently I made changes to the log4j.properties ex file: change the level of the TestMB class) and just do the redeploy the application. Now, the changes only take effect when I restart Tomcat.

What will happen?

Log4j.properties

log4j.rootCategory = WARN, console
log4j.category.console = WARN
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{dd-MM-yyyy HH:mm:ss} %p %c:%L (%M) %m%n 

log4j.logger.br.com.nilson.control.TesteMB=INFO
  • Tomcat7.0.47
  • Log4j 1.2.16
asked by anonymous 20.01.2014 / 14:46

2 answers

1

Apparently, I was able to solve my problem using a Log4J boot

public class Log4jConfigLoader extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    public void init() throws ServletException {
        super.init();
        try {
            URL url = Loader.getResource("log4j.properties");
            PropertyConfigurator.configure(url);
        } catch (Exception e) {
            System.out.println("Could not start log4.properties monitor");
        }
    }
}

WEB.INF

<servlet>
    <servlet-name>log4j-init</servlet-name>
    <servlet-class>br.com.uniondata.servlet.Log4jConfigLoader</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
    
10.02.2014 / 12:45
1

You can do this as follows:

To make Log4j reload your configuration file automatically when it changes, you can do this with the configureAndWatch method. But the default boot procedure does not use configureAndWatch (if you just put the .properties file in the classpath). That way you can write a bit of code to Log4j to do this. The solution I found was the implementation of a Tomcat LifecycleListener.

import org.apache.catalina.Lifecycle;
import org.apache.catalina.LifecycleEvent;
import org.apache.catalina.LifecycleListener;
import org.apache.log4j.LogManager;
import org.apache.log4j.PropertyConfigurator;

public class Log4JInitializer implements LifecycleListener
{
    private String propertiesFile;

    public String getPropertiesFile()
    {
        return this.propertiesFile;
    }

    public void setPropertiesFile(String propertiesFile)
    {
        this.propertiesFile = propertiesFile;
    }

    @Override
    public void lifecycleEvent(LifecycleEvent event)
    {
        if (Lifecycle.BEFORE_START_EVENT.equals(event.getType()))
            initializeLog4j();
    }

    private void initializeLog4j()
    {
        //Log4j monitora o arquivo para mudanças
        PropertyConfigurator.configureAndWatch(propertiesFile);

        // shutdown log4j (e na thread monitor) no shutdown
        Runtime.getRuntime().addShutdownHook(new Thread()
        {
            @Override
            public void run()
            {
                LogManager.shutdown();
            }
        });
    }
}

EDIT

monitor "BEFORE_START_EVENT", and when this happens (which is once per Tomcat boot) I start Log4j using the configureAndWatch method. Also be sure to set up a Shutdown Hook cleaning of the Log4j thread created to query the configuration file for changes (I could also have chosen to listen to the "AFTER_STOP_EVENT" from Tomcat instead).

This package is in a jar. Put it in the Tomcat classpath, and now you can set it up in your Tomcat serverl.xml.

like this:

<Server>
   <Listener className="Log4JInitializer" propertiesFile="/path/to/log4j.properties"/>
</Server>
    
03.02.2014 / 11:24