Struts2 and Log4j Integration Example Project

Filed Under: Struts 2

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

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


  1. Rakesh Kumar Maharana says:

    Hi, I follow this example, everything is work, but the log file is not generating. what is the problem can u tell me?
    server : Jboss-4.2.4.GA

    Thank you

    1. Pankaj says:

      check for errors in server log, it might be not able to read log4j configuration.

  2. Muskaan says:

    Hi, I follow this example, everything is work, but the log file is not generating. what is the problem can u tell me?

  3. sarath gowd thota says:

    If i want to see my application logs in the browser, how to configure parameters in

  4. Gowtham says:

    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.

    1. Pankaj says:

      Which Tomcat version you are using? Make sure it’s Tomcat 7+

Comments are closed.

Generic selectors
Exact matches only
Search in title
Search in content