In this tutorial, it shows a complete integration example between Struts 2 and Log4j logging library.

1. Log4j

Get the Log4j library, of course.

pom.xml

    <!-- Log4j -->
    <dependency>
          <groupId>log4j</groupId>
	  <artifactId>log4j</artifactId>
	  <version>1.2.9</version>
    </dependency>

2. Log4j Appender

Create a text file named “log4j.properties“, put it at the root of the project classpath.

The log4j.properties or appender file is the Log4j configuration file, it defines how the Log4j logging mechanism work. See more Log4j appender examples.

In this example, it will log all the logging detail and outputs it to an external file “C:\\logfile.log“.

log4j.properties

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\logfile.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
 
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
 
# Root logger option
log4j.rootLogger=debug, file, stdout

3. Action

In action class, just get the Log4j Logger and log it as normal.

package com.mkyong.common.action;
 
import org.apache.log4j.Logger;
 
import com.opensymphony.xwork2.ActionSupport;
 
public class TestingAction extends ActionSupport{
 
    private static final Logger logger = Logger.getLogger(TestingAction.class);
 
    public String execute() throws Exception {
 
	if(logger.isDebugEnabled()){
		logger.debug("Testing .......");
	}
 
	return SUCCESS;
 
    }
}

4. Demo

Now, all the logging details will be logged into the file, named “C:\\logfile.log“. See sample :

13:26:47,322 DEBUG FileUploadInterceptor:57 - Bypassing //testingAction
13:26:47,323 DEBUG StaticParametersInterceptor:57 - Setting static parameters {}
13:26:47,325 DEBUG ParametersInterceptor:57 - Setting params NONE
13:26:47,326 DEBUG ParametersInterceptor:57 - Setting params 
...Invoking validate() on action com.mkyong.common.action.TestingAction@182ef6b
13:26:47,387 DEBUG DefaultActionInvocation:57 - Executing action method = null
13:26:47,387 DEBUG TestingAction:14 - Testing .......
13:26:47,390 DEBUG ServletDispatcherResult:57 - Forwarding to location pages/success.jsp
...Entering nullPropertyValue [target=[com.mkyong.common.action.TestingAction@182ef6b, 
com.opensymphony.xwork2.DefaultTextProvider@52c6b4], property=org]
13:26:47,469 DEBUG I18nInterceptor:57 - after Locale=en_US

Reference

  1. Log4j official website
  2. Log4j properties examples
  3. Struts + Log4j integration examples
  4. Struts 2 exceptions and logging
Any Java questions or problems? please post at this JavaNullPointer.com forum, see you there ~
[ Read More ] You can find more similar articles at Struts 2.x Tutorials