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.


Configuration Files

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xsi:schemaLocation="" id="WebApp_ID" version="3.0">




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

<project xmlns="" xmlns:xsi=""



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"?>

	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	<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>



Simple struts configuration file with action and result pages mapping.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "">

<log4j:configuration xmlns:log4j=""
	<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" />

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

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


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(){"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. For posting CODE wrap them inside <pre class="prettyprint"></pre>. Required fields are marked *