I have built a sample web application using struts 1.3. I am trying to print the logs using log4j2 but i am unable to get the struts logs in the log file
I have tried creating a logger for struts in log4j2. properties file as below
status = info
log4j2.debug = true
appender.file.type = File
appender.file.name = LOGFILE
appender.file.fileName=/test/logs/struts.log
appender.file.layout.type=PatternLayout
appender.file.layout.pattern= %d{yyyy-MM-dd HH:mm:ss.SSS} <%t>(%-6X{sys:user}) %2.2x - %msg%n
loggers = org.apache.struts,com.myapp.struts
logger.org.apache.struts.name = org.apache.struts
logger.org.apache.struts.level = trace
logger.org.apache.struts.appenderRef.file.ref = LOGFILE
logger.org.apache.struts.additivity = false
logger.com.myapp.struts.name = com.myapp.struts
logger.com.myapp.struts.level = trace
logger.com.myapp.struts.appenderRef.file.ref = LOGFILE
logger.com.myapp.struts.additivity = false
rootLogger.level = trace
i am still not getting the logs. The below is my project directory
The loginform class is as below
package com.myapp.struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class loginform extends org.apache.struts.action.Action
{
static final Logger log = LogManager.getLogger(loginform.class);
private static final String SUCCESS = "success";
private static final String FAILURE = "failure";
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception
{
log.info("Inside the loginform class");
log.info("Execute method called");
loginbean lb = (loginbean)form;
if(lb.getUname().equals("abc")&& lb.getUpass().equals("xyz"))
{
log.info("Login successful...redirecting to success page");
log.info("Exiting execute");
return mapping.findForward(SUCCESS);
}
else
{
log.info("Login failed...redirecting to failure page");
log.info("Exiting execute");
return mapping.findForward(FAILURE);
}
}
}
I get the logs printed in the above class but i dont get any logs from the struts even though i have configured a logger for it. Basically i am looking for logs like.
Initializing, config='org.apache.struts.taglib.html.LocalStrings', returnNull=true
Struts 1.x uses commons-logging
as logging api. In order to use log4j2, you need to include log4j-jcl
jar to your classpath. Use the following link to download the jar.
log4j-jcl dependency
If you still don't see the log message after adding log4j-jcl
jar, then replace the log4j2.properties file with log42.xml file and add below code to the xml file and see if you see struts log messages in console. If yes, your log42.properties configuration might have some issues. (I've never used properties file to configure log4j and it's a bit confusing to me compared to xml config)
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%m%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="DEBUG">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>