Struts2 and Log4j Integration Example Project

Sometime back I wrote an article about using log4j in web application and we utilized Servlet Context Listener to configure the log4j in web applications before we can use it.

Today we will learn how to use Log4j logging in Struts2 web application. We will learn it with a simple project whose final structure looks like below image.

Struts2-log4j-example

Configuration Files

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Struts2Log4jExample</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>

Web application deployment descriptor is simple and configured to use Struts2 framework.

<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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>Struts2Log4jExample</groupId>
	<artifactId>Struts2Log4jExample</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>

	<dependencies>
		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-core</artifactId>
			<version>2.3.15.1</version>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.16</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.3</version>
				<configuration>
					<warSourceDirectory>WebContent</warSourceDirectory>
					<failOnMissingWebXml>false</failOnMissingWebXml>
				</configuration>
			</plugin>
		</plugins>
		<finalName>${project.artifactId}</finalName>
	</build>
</project>

Important point to note is the struts2-core and log4j dependency that we will use in the project.

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
	<constant name="struts.convention.result.path" value="/"></constant>
	<package name="user" namespace="/" extends="struts-default">
		<action name="home" class="com.journaldev.struts2.actions.HomeAction">
			<result name="success">/home.jsp</result>
		</action>

	</package>

</struts>

Simple struts configuration file with action and result pages mapping.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"
	debug="false">
	<appender name="log4jexample" class="org.apache.log4j.RollingFileAppender">
		<param name="File" value="${catalina.home}/logs/example.log" />
		<param name="Append" value="true" />
		<param name="ImmediateFlush" value="true" />
		<param name="MaxFileSize" value="20MB" />
		<param name="MaxBackupIndex" value="10" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%-4r [%t] %-5p %c %x - %m%n" />
		</layout>
	</appender>

	<logger name="com.journaldev" additivity="false">
		<level value="DEBUG" />
		<appender-ref ref="log4jexample" />
	</logger>

	<root>
		<level value="debug" />
		<appender-ref ref="log4jexample" />
	</root>

</log4j:configuration>

Simple log4j xml configuration file, it should be in the classpath so that it will be inside WEB-INF/classes directory, similar to struts configuration file.

Action Class using log4j logging

package com.journaldev.struts2.actions;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.ActionSupport;

public class HomeAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	
	private static final Logger logger = Logger.getLogger(HomeAction.class);
	
	public String execute(){
		logger.info("inside HomeAction execute method");
		return SUCCESS;
	}
}

Struts2 automatically configures the log4j for us and we can use directly in the action classes. Notice the Logger initialization in the action class and that we are using in execute method for logging.

Once we export this project and run it, we get following logs in example.log file.

23450 [http-bio-8080-exec-4] INFO  com.journaldev.struts2.actions.HomeAction  - inside HomeAction execute method
26346 [http-bio-8080-exec-6] INFO  com.journaldev.struts2.actions.HomeAction  - inside HomeAction execute method

Thats it, you can see how easy it is to use log4j in Struts2 web application.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Current day month ye@r *