Struts + Log4j integration example
Log4j is the most popular and common used logging mechanism in Java web development. Here’s an example to integrate Log4j with Struts framework.
1. Download Log4j
You can download the Log4j library from the Log4j official website, or Maven central repository
<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.9</version> </dependency>
2. Log4j Appender
Create a Log4j properties file (log4j.properties) and define the appender details (where to log and log file details). And put the log4j.properties into your project classpath.
log4j.properties
# Direct log messages to a log file log4j.appender.file=org.apache.log4j.RollingFileAppender log4j.appender.file.File=C:\\loging.log log4j.appender.file.MaxFileSize=1MB log4j.appender.file.MaxBackupIndex=1 log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n # Direct log messages to stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n # Root logger option log4j.rootLogger=error, file, stdout
3. Struts + Log4j example
To use Log4j in Struts framework, you can get the Log4j logger via Logger.getLogger() and log it directly.
Struts Action class
//... public class UserAction extends Action{ private static final Logger logger = Logger.getLogger(UserAction.class); public ActionForward execute(ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception { if(logger.isDebugEnabled()){ logger.debug("Starting started"); } //...
Struts custom exception handler
//... public class MyCustomExceptionHandler extends ExceptionHandler{ private static final Logger logger = Logger.getLogger(MyCustomExceptionHandler.class); @Override public ActionForward execute(Exception ex, ExceptionConfig ae, ActionMapping mapping, ActionForm formInstance, HttpServletRequest request, HttpServletResponse response) throws ServletException { //log the error message logger.error(ex); return super.execute(ex, ae, mapping, formInstance, request, response); } }
Log4j output file sample- c:\\logging
12:45:52,210 ERROR MyCustomExceptionHandler:25 - java.io.IOException 12:48:27,672 ERROR MyCustomExceptionHandler:25 - ExceptionConfig[type= java.io.IOException,handler= com.mkyong.common.exception.MyCustomExceptionHandler, key=error.global.mesage,path=/pages/error.jsp,scope=request] 12:48:54,102 INFO PropertyMessageResources:209 - Operating in Default mode [null] 12:48:54,134 DEBUG sax:1410 - startElement(,web-app,web-app) 12:48:54,134 DEBUG Digester:1417 - Pushing body text '' 12:48:54,134 DEBUG Digester:1436 - New match='web-app' 12:48:54,134 DEBUG Digester:1464 - No rules found matching 'web-app'.
Reference
- Log4j documentation – http://logging.apache.org/log4j/1.2/manual.html
- log4j.properties examples – http://www.mkyong.com/logging/log4j-log4j-properties-examples/
really helpfull
[...] Struts + Log4j integration examples [...]