Setup logging with Log4j using Struts 1.2.4 on Tomcat 4.1.3

  在用中文搜索不到你想要东西时(http://www.learndiary.com/disDiaryContentAction.do?searchDiaryID=1343&goalID=1343&naviStr=a10a60a0167),就用英文吧,有时给你十分满意的答案。昨天,花了许多时间来搜struts下log4j的使用,找到的答案还是没有能解决问题。今天,就用“struts log4j”两个关键字,一下就找到了我要找的东西:)

转自:http://www.mobilefish.com/developer/struts/struts_quickguide_log4j.html

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:

Download and unzip Log4j.

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

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.

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.

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.");

      }

}

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