Home » Enterprise Java » Struts 2 » Struts 2 and Log4J Example

About Mohammad Meraj Zia

I did my Engineering in Information Technology from IET, Lucknow, India. Currently doing MSc in Information Technology from Derby University. I have worked in Java/J2EE domain for the last 10 years. Have good understanding of Payment and Finance domains.

Struts 2 and Log4J Example

In this example we will learn how to integrate Struts2 application with Log4j logging framework. Apache Struts is a free, open-source, MVC framework for creating elegant, modern Java web applications.

It favors convention over configuration, is extensible using a plugin architecture, and ships with plugins to support REST, AJAX and JSON.

Log4J is Apache’s popular logging framework.

Tools and technologies used in this example are java 1.7, Maven, Intellij, Log4j-1.2.16, Struts-, Tomcat-7.0.54

1. Project Structure

Below is the project structure used in this example.

Figure 1. Project Structure

Figure 1. Project Structure

Struts2ExampleAction is the Struts Action class. The resource folder contains the Log4J properties file (log4j.properties) and the Struts configuration file (struts.xml).

2. Maven dependency

Below is the pom file which define the dependencies for Struts 2 and Log4J.


<?xml version="1.0" encoding="UTF-8"?>
  <project xmlns="http://maven.apache.org/POM/4.0.0"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">




3. web.xml

Below in the web.xml file.


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"




4. index.jsp

Below is the index.jsp file which the user will be redirected to.


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
   <title>Java Code Geek Strut2 Log4J example</title>
   <h1>Java Code Geeks - Welcome to Struts 2 Log4j Example</h1>

5. Configuration file

Below are the two configuration files used for this example

5.1 Struts Configuration

Below is the struts.xml configuration file


<?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="example" namespace="/" extends="struts-default">
    <action name="example" class="com.javacodegeeks.Struts2ExampleAction">
      <result name="success">/index.jsp</result>

5.2 Log4j Configuration

Below is the log4j.properties file


# Root logger option
log4j.rootLogger=INFO, file


log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

6. Struts Action file

Below is the Struts action file Struts2ExampleAction.java

package com.javacodegeeks;

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

 * JavaCodeGeeks Example class.
public class Struts2ExampleAction extends ActionSupport {

 private static final long serialVersionUID = 1L;

 private static final Logger LOGGER = Logger.getLogger(Struts2ExampleAction.class);

 * {@inheritDoc}
 * @see com.opensymphony.xwork2.Action
 public String execute(){
   LOGGER.debug("Inside Struts2ExampleAction.execute() method");
   LOGGER.debug("JavaCodeGeeks Struts 2 Example");
   return SUCCESS;

7. Output

Run the tomcat and deploy the application. Once the application is in running state go to URL ‘localhost:8081/struts2-log4j/example’. My tomcat is running on 8081 port. Use the port whereever your tomcat is running.

Figure 2. Output

Figure 2. Output

Since we are using the File appender the log messages will go in the file defined in the log4j.properties file: C:\\javacodegeeks\\struts2\\logger.log

2015-02-08 21:55:11 DEBUG ExampleAction:16 - Inside Struts2ExampleAction.execute() method
2015-02-08 21:55:11 DEBUG ExampleAction:16 - JavaCodeGeeks Struts 2 Example

8. Download the source code

This was an example of Struts 2 and Log4J Integration.

You can download the full source code of this example here : Struts2 Log4J Integration

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area


Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments