Log4j est un outil formidable dans le monde Java. Il permet a moindre cout de mettre en place au sein d’une application, un fichier de trace, permettant plusieurs niveau d’information.

Mes collègues javaïstes seront forcément de mon avis. Aussi, pour qu’une application web puisse fournir son propre fichier de log, il est nécessaire de configurer dynamiquement le

moteur de log4j. C’est le propos de cet article.

Pour qu’une application fournisse sa propre configuration de Log4J et son propre fichier de log (et sa propre config) :

(issue de http://www.avajava.com/tutorials/lessons/how-do-i-initialize-log4j-in-a-web-application.html)

La servlet d’initialisation de Log4J lors du démarrage du serveur Tomcat :

package test;

import java.io.File;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.PropertyConfigurator;

public class Log4JInitServlet extends HttpServlet {

         private static final long serialVersionUID = 1L;

         public void init(ServletConfig config) throws ServletException {
                 System.out.println("Log4JInitServlet is initializing log4j");
                 String log4jLocation = config.getInitParameter("log4j-properties-location");

                 ServletContext sc = config.getServletContext();

                 if (log4jLocation == null) {
                          System.err.println("*** No log4j-properties-location init param, so initializing log4j with BasicConfigurator");
                          BasicConfigurator.configure();
                 } else {
                          String webAppPath = sc.getRealPath("/");
                          String log4jProp = webAppPath + log4jLocation;
                          File yoMamaYesThisSaysYoMama = new File(log4jProp);
                          if (yoMamaYesThisSaysYoMama.exists()) {
                                   System.out.println("Initializing log4j with: " + log4jProp);
                                   PropertyConfigurator.configure(log4jProp);
                          } else {
                                   System.err.println("*** " + log4jProp + " file not found, so initializing log4j with BasicConfigurator");
                                   BasicConfigurator.configure();
                          }
                 }
                 super.init(config);
         }
}

Je ne l’ai pas inventé, je l’ai trouvé ici :

http://www.avajava.com/tutorials/lessons/how-do-i-initialize-log4j-in-a-web-application.html

En rouge ci-dessous le tag servlet a insérer dans le fichier web.xml de démarrage de l’application. 

This message contains information that may be privileged or confidential and is the property of the Capgemini Group. It is
intended only for the person to whom it is addressed. If you are not the intended recipient, you are not authorized to
read, print, retain, copy, disseminate, distribute, or use this message or any part thereof. If you receive this message
in error, please notify the sender immediately and delete all copies of this message.

Publicités