Struts

 
 
Struts is an open source framework useful in building web applications with Java Servlet and JavaServer Pages (JSP) technology, based on the Model-View-Controller (MVC) design paradigm.
Struts is copyrighted software available under a "free-to-use-license" by the Apache Software Foundation. The license appears at the head of every source code file.

The latest Struts version can be downloaded from:
http://struts.apache.org/

rss feed






Setup logging with Log4j using Struts 1.2.4 on Tomcat 4.1.3.



Information
none

Operating system used
Windows XP Home Edition Version 5.1 SP 2

Software prerequisites
Log4j 1.2.9
Struts 1.2.4
Tomcat 4.1.3


Procedure
  1. Download and unzip Log4j.

  2. In the unzipped log4j directory copy file C:\Tools\logging-log4j-1.2.9\dist\lib\log4j-1.2.9.jar to your Struts project: <my_project>\WEB-INF\lib\log4j-1.2.9.jar

  3. Create a log4j.properties file.
    As an example you can put the following lines in the log4j.properties file:

    log4j.rootLogger=ERROR, stdout
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=[%5p] %d{mm:ss} (%F:%M:%L)%n%m%n%n


    Remark: The ConversionPattern creates the following message:

    [ERROR] 02:44 (com.mobilefish.DemoAction execute:95)
    This is my message.


  4. Put the log4j.properties file in the following location:

    <my_project>\WEB-INF\classes\log4j.properties

    Note: In some situations the log4j.properties can not be located by the application server. To find out if this is the case set the -Dlog4j.debug=true.

    In the quick guide "Loading the log4j.properties file" serveral solutions are mentioned how to solve this problem.



  5. Add the following lines in your Java code:

    package com.mobilefish;

    // Please note the following: Struts 1.2.4 uses commons-logging
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;


    public class DemoAction extends Action {

          private static Log log = LogFactory.getLog("com.mobilefish.DemoAction");

          public ActionForward execute(
             ActionMapping mapping,
             ActionForm form,
             HttpServletRequest request,
             HttpServletResponse response)
             throws Exception {

             log.debug("This is my debug message.");
             log.info("This is my info message.");
             log.warn("This is my warn message.");
             log.error("This is my error message.");
             log.fatal("This is my fatal message.");

          }
    }

  6. Start the application server and run your web application.
    Because log4j.rootLogger=ERROR (see log4j.properties file), the following messages are displayed in the console:

    [ERROR] 23:35 (DemoAction.java:execute:69)
    This is my error message.

    [FATAL] 23:35 (DemoAction.java:execute:69)
    This is my fatal message.

    Note 1. Loggers can be assigned to the following ORDERD levels:
    ALL < DEBUG < INFO < WARN < ERROR < FATAL < OFF

    Note 2. To enable all log messages:
    log4j.rootLogger=ALL

    Be warned: the struts code and your web applications log messages are logged.

    Note 3. To disable all log messages:
    log4j.rootLogger=OFF