To use log4j in a Struts application, simply put log4j configuration file (log4j.propertiesor log4j.xml) under the root of the classpath. For a Struts web application, it would be under WEB-INF\classes directory. When developing a Struts project in Eclipse, you can put the file under src directory (and Eclipse will automatically put it into classpath), as shown in the following screenshot:

 log4j config file in Struts2 project

Here’s a sample log4j.properties file:

# LOG4J configuration
log4j.rootLogger=INFO, Appender1, Appender2

log4j.appender.Appender1=org.apache.log4j.ConsoleAppender
log4j.appender.Appender1.layout=org.apache.log4j.PatternLayout
log4j.appender.Appender1.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n

log4j.appender.Appender2=org.apache.log4j.FileAppender
log4j.appender.Appender2.File=D:/Logs/Struts2.log
log4j.appender.Appender2.layout=org.apache.log4j.PatternLayout
log4j.appender.Appender2.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n
This log4j configuration file specifies two appenders: a ConsoleAppender and a FileAppender (the log file will be written to D:/Logs/Struts2.log, so make sure the directory exists).

Then we can use log4j in a Struts action class like this:

package net.codejava.struts;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.ActionSupport;

public class TestAction extends ActionSupport {
	
	static final Logger LOGGER = Logger.getLogger(TestAction.class);
	
	public String execute() {
		
		LOGGER.info("This is a debug log message from Struts2");
		
		return SUCCESS;
	}
}
Output in the server’s console when running this action (http://localhost:8080/Log4jStruts2/test):

 log4j output in server console

And notice the log is also written to the file D:/Logs/Struts2.log.

 

Related Java Logging Tutorials:



 

Other Struts Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Attachments:
Download this file (Log4jStruts2.zip)Log4jStruts2.zip[Eclipse project]3997 kB

Add comment

   


Comments 

#4Mj2020-02-12 20:46
What about log4j 2 and struts integration .

I'm struggling in migrating the below syntax to log4j 2

log4j.logger.com.opensymphony.xwork2.interceptor=error
log4j.logger.com.opensymphony.xwork2.ognl=error
log4j.logger.com.opensymphony.xwork2.util.OgnlUtil=error
log4j.logger.com.opensymphony.xwork2.ognl.OgnlValueStack=error
Quote
#3balaji2015-12-20 23:39
good
Quote
#2Nam2013-11-13 09:11
Hi Gowtham,

No, there isn't any specific configuration for Tomcat. For the sample application, you must invoke the Struts action by typing the URL (localhost:8080/Log4jStruts2/test). Did you do that?
Quote
#1Gowtham2013-11-13 06:46
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.
Quote