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

rss feed






Apply log4j within jsp.



Information
none

Operating system used
Windows XP Home Edition Version 5.1 SP 2

Software prerequisites
Tomcat
Jakarta log tag library
Log4j 1.2.9


Procedure
  1. Download the Jakarta log tag library: jakarta-taglibs-log-1.0.zip

  2. Extract this archive file, e.g.: C:\Tools\jakarta-taglibs-log-1.0

    The archive contains the following files:

    LICENSE
    log-doc.war
    log-examples.war
    README
    taglibs-log.jar
    taglibs-log.tld

  3. Download Log4j 1.2.9: logging-log4j-1.2.9.zip

  4. Extract this archive file, e.g.: C:\Tools\logging-log4j-1.2.9

    The archive contains serveral files. The most important one is: C:\Tools\logging-log4j-1.2.9\dist\lib\log4j-1.2.9.jar

  5. Install Tomcat. Follow guide "Installing Tomcat 4.1.31".

  6. Create a simple a Tomcat web application called "demo":

    • Create the following directories inside the Tomcat "webapps" directory:

      C:\Tools\Tomcat 4.1\webapps\demo
      C:\Tools\Tomcat 4.1\webapps\demo\WEB-INF
      C:\Tools\Tomcat 4.1\webapps\demo\WEB-INF\classes
      C:\Tools\Tomcat 4.1\webapps\demo\WEB-INF\lib

    • Copy the tag library descriptor file "taglibs-log.tld" into WEB-INF.

    • Copy the tag library JAR file "taglibs-log.jar" into WEB-INF/lib.

    • Copy the log4j JAR file "log4j-1.2.9.jar" into WEB-INF/lib.

    • Create a web.xml file in the WEB-INF directory:

      <?xml version="1.0"?>
      <!DOCTYPE web-app
        PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
        "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

      <web-app>
         <display-name>Demonstration log4j usage in jsp</display-name>
         <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
         </welcome-file-list>
         <taglib>
            <taglib-uri>http://jakarta.apache.org/taglibs/log-1.0</taglib-uri>
            <taglib-location>/WEB-INF/taglibs-log.tld</taglib-location>
         </taglib>
      </web-app>

      Note: You MUST add a <taglib> element in the web.xml file:

      <taglib>
         <taglib-uri>http://jakarta.apache.org/taglibs/log-1.0</taglib-uri>
         <taglib-location>/WEB-INF/log.tld</taglib-location>
      </taglib>


    • Create a log4j.properties file in the WEB-INF/classes directory:

      # ***** Set root logger level to DEBUG and its two appenders to stdout and R.
      log4j.rootLogger=debug, stdout, R

      # ***** stdout is set to be a ConsoleAppender.
      log4j.appender.stdout=org.apache.log4j.ConsoleAppender
      # ***** stdout uses PatternLayout.
      log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
      log4j.appender.stdout.layout.ConversionPattern=%d [%c] %p - %m%n

      # ***** R is set to be a RollingFileAppender.
      log4j.appender.R=org.apache.log4j.RollingFileAppender
      log4j.appender.R.File=${catalina.home}/logs/demo.log
      # ***** Max file size is set to 100KB
      log4j.appender.R.MaxFileSize=100KB
      # ***** Keep one backup file
      log4j.appender.R.MaxBackupIndex=1
      # ***** R uses PatternLayout.
      log4j.appender.R.layout=org.apache.log4j.PatternLayout
      log4j.appender.R.layout.ConversionPattern=%d [%c] %p - %m%n


      Note:
      The location of the log file is set in:
      log4j.appender.R.File=${catalina.home}/logs/demo.log



    • Create two jsp files in the C:\Tools\Tomcat 4.1\webapps\demo directory:

      File 1: index.jsp

      <%@ taglib uri="http://jakarta.apache.org/taglibs/log-1.0" prefix="log" %>

      <html>
      <head>
         <title>Demonstration log4j usage in jsp</title>
      </head>

      <body>

      <!-- Demonstrate different log levels -->
      <log:debug>Show DEBUG message.</log:debug>
      <log:info>Show INFO message.</log:info>
      <log:warn>Show WARN message.</log:warn>
      <log:error>Show ERROR message.</log:error>
      <log:fatal>Show FATAL message.</log:fatal>

      <!-- Demonstrate where to put the log messages -->
      <log:fatal>Message embedded within the open and close tags.</log:fatal>
      <log:fatal message="Message passed as an attribute to the tag" />
      <log:fatal category="com.mobilefish.demo.index">
         Using category attribute.
      </log:fatal>

      <b>
         The log messages are shown in the Tomcat console and in the
         ${catalina.home}/logs/demo.log file.
      </b>

      </body>
      </html>


      File 2: test.jsp

      <%@ page import="org.apache.log4j.Logger" %>

      <html>
      <head>
         <title>Demonstration log4j usage in jsp</title>
      </head>

      <body>

      <%
      Logger log = Logger.getLogger("com.mobilefish.demo.test");
      log.debug("Show DEBUG message");
      log.info("Show INFO message");
      log.warn("Show WARN message");
      log.error("Show ERROR message");
      log.fatal("Show FATAL message");
      %>

      <b>
         The log messages are shown in the Tomcat console and in the
         ${catalina.home}/logs/demo.log file.
      </b>

      </body>
      </html>


  7. (Re)start Tomcat.

  8. Access the index.jsp file:

    http://localhost:8080/demo/index.jsp

  9. The following log messages are shown in the Tomcat console and in the ${catalina.home}/logs/demo.log file.

    2006-06-03 17:28:43,379 [root] DEBUG - Show DEBUG message.
    2006-06-03 17:28:43,409 [root] INFO - Show INFO message.
    2006-06-03 17:28:43,409 [root] WARN - Show WARN message.
    2006-06-03 17:28:43,409 [root] ERROR - Show ERROR message.
    2006-06-03 17:28:43,419 [root] FATAL - Show FATAL message.
    2006-06-03 17:28:43,419 [root] FATAL - Message embedded within the open and close tags.
    2006-06-03 17:28:43,419 [root] FATAL - Message passed as an attribute to the tag
    2006-06-03 17:28:43,419 [com.mobilefish.demo.index] FATAL - Using category attribute.


  10. Access the test.jsp file:

    http://localhost:8080/demo/test.jsp

  11. The following log messages are shown in the Tomcat console and in the ${catalina.home}/logs/demo.log file.

    2006-06-03 17:55:43,379 [com.mobilefish.com.test] DEBUG - Show DEBUG message.
    2006-06-03 17:55:43,409 [com.mobilefish.com.test] INFO - Show INFO message.
    2006-06-03 17:55:43,409 [com.mobilefish.com.test] WARN - Show WARN message.
    2006-06-03 17:55:43,409 [com.mobilefish.com.test] ERROR - Show ERROR message.
    2006-06-03 17:55:43,419 [com.mobilefish.com.test] FATAL - Show FATAL message.


  12. Change the root logger level to WARN in the log4j.properties file and restart Tomcat:

    log4j.rootLogger=warn, stdout, R

    When the index.jsp page is reloaded in your browser only the following messages are shown:

    2006-06-03 17:48:43,409 [root] WARN - Show WARN message.
    2006-06-03 17:48:43,409 [root] ERROR - Show ERROR message.
    2006-06-03 17:48:43,419 [root] FATAL - Show FATAL message.
    2006-06-03 17:48:43,419 [root] FATAL - Message embedded within the open and close tags.
    2006-06-03 17:48:43,419 [root] FATAL - Message passed as an attribute to the tag
    2006-06-03 17:48:43,419 [com.mobilefish.demo.index] FATAL - Using category attribute.