You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Laurent Blume <la...@infores.com> on 2003/11/13 09:44:01 UTC

Re: log4j losing log files SOLVED

Hello all,

A bit late, I'm afraid, but since I just got an off-list question about 
it, and that it was a problem for me, maybe it can help others in the 
future.
Hope it helps you, Tim, if not, don't hesitate to subscribe and post to 
the list, there are many people that can help you better than me there!

So, here is how I solved my problem of log4j losing its log files when 
rotating them. Maybe there are better ways, but that one seems to work 
nicely so far for me.

I created a very small class implementing ServletContextListener:

package com.infores.fr.ihs;

import java.util.*;
import javax.servlet.*;
import org.apache.log4j.LogManager;

/**
  * This class is used to shutdown log4j properly.
  * When the context of the application is destroyed, log4j should
  * be shut down to avoid keeping locked log files.
  */
public class IHScontextListener implements ServletContextListener {

   /**
    * Do nothing.
    */
   public void contextInitialized(ServletContextEvent sce) {
     // Do nothing here...
   }

   /**
    * Shuts log4j down.
    * This is necessary to avoid lost log files: when the app
    * is reloaded, the JVM might keep a lock on the last log file
    * otherwise, and will not be able to rename it before
    * restarting it.
    */
   public void contextDestroyed(ServletContextEvent sce) {
     LogManager.shutdown();
   }
}

This class  is put in a JAR file after compilation, and the JAR file 
itself is in the WEB-INF/lib directory of my webapp.

Then, I configured my webapp so it uses the newly implemented listener.
I edited the WEB-INF/web.xml file, and added:

   <!-- ServletContextListener called when the application ends -->
   <listener>
   <listener-class>com.infores.fr.ihs.IHScontextListener</listener-class>
   </listener>

Hope this helps others as I was helped myself :-)

Laurent


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: log4j-user-help@jakarta.apache.org