Struts 2 + Log4j integration example

struts2 log4j

In this tutorial, we will show you how to integrate the log4j framework with the Struts 2 web application. All you need to do is

  1. Include the log4j.jar as the project dependency
  2. Create a log4j.properties file and put it into the root of the classpath, with Maven, put it into the resources folder.

Technologies and tools used :

  1. Log4j 1.2.17
  2. Struts 2.3.16.3
  3. Maven 3
  4. Tomcat 6
  5. Eclipse Kepler 4.3

1. Project Directory

Review the final project structure.

strruts2-log4j-directory

2. Project Dependencies

Declares Struts 2 and log4j dependencies :

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
	http://maven.apache.org/maven-v4_0_0.xsd">

	<modelVersion>4.0.0</modelVersion>
	<groupId>com.mkyong.common</groupId>
	<artifactId>Struts2</artifactId>
	<packaging>war</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>Struts + Log4j Webapp</name>
	<url>http://maven.apache.org</url>

	<properties>
		<jdk.version>1.7</jdk.version>
		<struts.version>2.3.16.3</struts.version>
		<log4j.version>1.2.17</log4j.version>
	</properties>

	<dependencies>

		<!-- Struts 2 -->
		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-core</artifactId>
			<version>${struts.version}</version>
		</dependency>

		<!-- Log4j -->
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>${log4j.version}</version>
		</dependency>

	</dependencies>

	<build>
	  <finalName>Struts2</finalName>
	  <plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-eclipse-plugin</artifactId>
			<version>2.9</version>
			<configuration>
				<downloadSources>true</downloadSources>
				<downloadJavadocs>false</downloadJavadocs>
				<wtpversion>2.0</wtpversion>
			</configuration>
		</plugin>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-compiler-plugin</artifactId>
			<version>2.3.2</version>
			<configuration>
				<source>${jdk.version}</source>
				<target>${jdk.version}</target>
			</configuration>
		</plugin>
	  </plugins>
	</build>
</project>

3. log4j.properties

Create a log4j properties file, and put it into the resources folder, refer to step #1.

log4j.properties

# Root logger option
log4j.rootLogger=ERROR, stdout, file

# Redirect log messages to console
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{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Redirect log messages to a log file, support rolling backup file.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=${catalina.home}/logs/mystruts2app.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

4. Struts 2 Action and Logging

A simple action to return a page, and shows you how to do message logging with log4j.

WelcomeAction.java

package com.mkyong.common.action;

import org.apache.log4j.Logger;
import com.opensymphony.xwork2.ActionSupport;

public class WelcomeAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	//get log4j
	private static final Logger logger = Logger.getLogger(WelcomeAction.class);

	public String execute() throws Exception {

		// logs debug message
		if (logger.isDebugEnabled()) {
			logger.debug("execute()!");
		}

		// logs exception
		logger.error("This is Error message", new Exception("Testing"));

		return SUCCESS;

	}
}

5. Struts 2 Configuration

Struts 2 configuration and JSP pages, in case you are interested.

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
	<constant name="struts.devMode" value="true" />

	<package name="welcome" namespace="/" extends="struts-default">

		<action name="welcome" class="com.mkyong.common.action.WelcomeAction">
			<result name="success">pages/success.jsp</result>
		</action>

	</package>

</struts>
web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	version="2.5">

	<display-name>Struts 2 Web Application</display-name>

	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>
			org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
		</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

</web-app>
pages/success.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>
 
<body>
<h1>Struts 2 + Log4j integration example</h1>

</body>
</html>

6. Demo

Run the Struts 2 web application, and access the welcome action.

URL : http://localhost:8888/Log4jAndStruts2/welcome

struts 2 log4j demo

6.1 All logging messages will be displayed in the console.

strruts2-log4j-demo-eclipse-console

Figure : Eclipse console

6.2 Furthermore, a logging file will be created in the Tomcat’s logs folder.

strruts2-log4j-demo-file

Figure : D:\apache-tomcat-6.0.37\logs\mystruts2app.log

Download Source Code

Download it – Log4jAndStruts2Example.zip (20 KB)

References

  1. Create Struts 2 Web Application Example
  2. log4j 1.2 official page
  3. log4j hello world example
  4. Struts 2 exceptions and logging

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
3 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
3 Comment authors
WekmentorautumnUser 31 Recent comment authors
newest oldest most voted
Wekmentor
Guest
Wekmentor

Thanks for the example and src code. Gratz!

autumn
Guest
autumn

Is there a specific configuration for struts2 app deployed in a tomcat server? The example doesn’t seem to work for my application. It doesn’t log anything whether on a log file nor the console. No log file is even created.
Thanks in advance.

User 31
Guest
User 31

nice article