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


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


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


Simple struts configuration file with action and result pages mapping.


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

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.

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

By admin

Leave a Reply

%d bloggers like this: