This example demonstrates how to integrate log4j to your struts 2 project.
- Add log4j.jar file in your project lib folder
- Create log4j.xml file with loggers and appenders. Here I have added file and console appenders. Each log message will be printed in console and a file. Make sure that log4j.xml file is in the classpath.
- Import org.apache.log4j.Logger in your action class and start using the logger methods. In this example I have used logger.info method.
Lets look at the example code.
1. Action Class
package javabeat.net.struts2; import org.apache.log4j.Logger; public class Struts2HelloWorldAction { private static final Logger logger = Logger.getLogger(Struts2HelloWorldAction.class); public String execute(){ logger.info("Executing method"); return "success"; } }
2. Log4j XML Configuration Example – log4j.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false"> <appender name="fileAppenderInfo" class="org.apache.log4j.RollingFileAppender"> <param name="File" value="D:/info.log" /> <param name="MaxBackupIndex" value="10"/> <param name="MaxFileSize" value="5120KB"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d %-5p [%c{1}][%x] %m %n" /> </layout> <filter class="org.apache.log4j.varia.LevelRangeFilter"> <param name="LevelMin" value="INFO" /> <param name="LevelMax" value="INFO" /> </filter> </appender> <appender name="consoleAppenderInfo" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d %-5p [%c{1}][%x] %m %n" /> </layout> <filter class="org.apache.log4j.varia.LevelRangeFilter"> <param name="LevelMin" value="INFO" /> <param name="LevelMax" value="INFO" /> </filter> </appender> <logger name="javabeat.net.struts2"> <level value="INFO" /> <appender-ref ref="fileAppenderInfo" /> <appender-ref ref="consoleAppenderInfo" /> </logger> <logger name="com.opensymphony.xwork2"> <level value="INFO" /> <appender-ref ref="fileAppenderInfo" /> <appender-ref ref="consoleAppenderInfo" /> </logger> <logger name="org.apache.struts2"> <level value="INFO" /> <appender-ref ref="fileAppenderInfo" /> <appender-ref ref="consoleAppenderInfo" /> </logger> </log4j:configuration>
3. Struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="struts2demo" extends="struts-default"> <action name="log4jdemo" class="javabeat.net.struts2.Struts2HelloWorldAction" method="execute"> <result name="success">/</result> </action> </package> </struts>
4. Log Messages
2013-12-16 15:11:59,266 INFO [XmlConfigurationProvider][] Parsing configuration file [struts-default.xml] 2013-12-16 15:11:59,341 INFO [XmlConfigurationProvider][] Unable to locate configuration files of the name struts-plugin.xml, skipping 2013-12-16 15:11:59,342 INFO [XmlConfigurationProvider][] Parsing configuration file [struts-plugin.xml] 2013-12-16 15:11:59,346 INFO [XmlConfigurationProvider][] Parsing configuration file [struts.xml] 2013-12-16 15:11:59,348 INFO [DefaultConfiguration][] Overriding property struts.i18n.reload - old value: false new value: true 2013-12-16 15:11:59,349 INFO [DefaultConfiguration][] Overriding property struts.configuration.xml.reload - old value: false new value: true 2013-12-16 15:09:52,345 INFO [Struts2HelloWorldAction][] Executing method