All the files required to use Log4j.
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Vishal_T
Posted On:   Tuesday, June 24, 2003 07:47 AM

			
Hi I'm new to Struts and Log4j. Can someone please tell me
what file(s) I have to include or add references to in order
to use Log4J in my struts applicaiton. Also do I have to use
the commons-logging package provided with Struts. If so are
there any other additional file reqiured. Please let me
know if someone has an example struts application using logging
that they don't mind sharing the code for. I would really appreciate
it as I am new to this all.


thanks,
Vishal


Re: All the files required to use Log4j.

Posted By:   Anonymous  
Posted On:   Wednesday, June 25, 2003 03:50 AM

You can use Log4j within Struts as you would use it within every other java class.

First make sure Log4j.jar is in you're classpath and within you're java class for example a Struts Action class you type:


import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.log4j.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyAction extends Action {

public static final String FORWARD_SUCCESS = "success";

private static final Logger log = Logger.getLogger(MyAction.class);

public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
log.error("This error message shouldn't be here");
// a check if the logger is debug enabled only placed this check here for performance issues
if (log.isDebugEnabled) {
log.debug("A debug message in my MyAction class");
}
return actionMapping.findForward(FORWARD_SUCCESS);
}


If you want to know more about log4j (configuration of log4j , etc) read the following manual.

http://jakarta.apache.org/log4j/docs/manual.html
About | Sitemap | Contact