Log4j

 
 
Log4j is a package to output log statements to a variety of output targets.

The log statements can remain in your shipped code without incurring a heavy performance cost. The logging behavior can be controlled by editing a configuration file (log4j.properties), without modifying the application.

One of the distinctive features of log4j is the notion of inheritance in loggers. By using a logger hierarchy it is possible to control which log statements are output at arbitrarily fine granularity. This helps reduce the volume of logged output and minimize the cost of logging.

The target of the log output can be a file, an OutputStream, a java.io.Writer, a remote log4j server, a remote Unix Syslog daemon, or even a NT Event logger among many other output targets.

Log4j has three main components (loggers, appenders and layouts) which works together to enable developers to log messages according to message type and level, and to control at runtime how these messages are formatted and where they are reported.

Log4j is distributed at no charge for commercial or non-commercial use. For more information read the LICENSE.txt file.

More information about log4j can be found at http://logging.apache.org/log4j/docs/index.html

The latest Log4j version can be downloaded from http://logging.apache.org/log4j/docs/download.html







Loading the log4j.properties file.



Information
none

Operating system used
Windows XP Home Edition Version 5.1 SP 2

Software prerequisites
Log4j 1.2.9

Procedure
  1. In a stand alone application the log4j.properties must be put in the directory where you issued the java command. See quick guide "Installing Log4j 1.2.9."

  2. In web applications the log4.properties file can sometimes not be found. You can verify this by setting the log4j.debug parameter. This quick guide provides some solutions:

    • Solution 1:
      Verify if parameter log4j.configuration is set.
      If it is set, change the log4j.properties filename.
      If this does not solve the problem, see the next solution.

    • Solution 2:
      Verify if <my_app>/WEB-INF/classes/log4j.properties exists.
      If this does not solve the problem, see the next solution.

    • Solution 3:
      Create a Log4JServlet to load the log4j.properties file.



      package com.mobilefish.servlets;

      import javax.servlet.http.HttpServlet;
      import javax.servlet.ServletException;
      import org.apache.log4j.PropertyConfigurator;

      public class Log4JServlet extends HttpServlet {

         public void init() throws ServletException{

            String log4jfile = getInitParameter("log4j-init-file");
            if (log4jfile != null) {
               String propfile = getServletContext().getRealPath(log4jfile);
               PropertyConfigurator.configure(propfile);
            }
         }
      }


      Next step is to edit the web.xml file. Make sure this servlet is loaded first, load-on-startup=1.

      <servlet>
         <servlet-name>Log4JServlet</servlet-name>
         <servlet-class>com.mobilefish.servlets.Log4JServlet</servlet-class>
            <init-param>
               <param-name>log4j-init-file</param-name>
               <param-value>/WEB-INF/classes/log4j.properties</param-value>
            </init-param>
         <load-on-startup>1</load-on-startup>
      </servlet>

      <servlet>
      :
      </servlet>


      Next step is to put the log4j.properties in <my_app>/WEB-INF/classes/log4j.properties.
      If this does not solve the problem, see the next solution.

    • Solution 4:
      This solution only applies if you are using the Tomcat 4.x application server. Copy the log4j.properties file in directory C:\Tools\Tomcat 4.1\common\classes