how to use log4j using struts appliation..?
2 posts in topic
Flat View  Flat View

Posted By:   Padmanabh_Murthy
Posted On:   Wednesday, April 23, 2003 10:50 PM


how to use log4j using struts appliation..?

what i meant how to do the configuration..?
if possible gie an example.

thankx in advance



Re: how to use log4j using struts appliation..?

Posted By:   Mukesh_Chandnani  
Posted On:   Friday, April 25, 2003 03:05 AM


This is a simple way to use Log4j. The few things of importance are "Logger,PatternLayout and Appender".

There is a Logger Class, which is Central to Log4j. Always you should create a static instance of this. After that you should create an instance of the Layout and then create an instance of Appender passing the Layout as parameter, to this PatternLayout. So you have the layout as to how you want to display your log and appender ready as to where you will append. add this appender to Logger class and rest assured this works.

I got this example from the net..

import org.apache.log4j.Level;

import org.apache.log4j.Logger;

import org.apache.log4j.SimpleLayout;

import org.apache.log4j.FileAppender;

public class simpandfile


static Logger logger = Logger.getLogger(simpandfile.class);

public static void main(String args[])


SimpleLayout layout = new SimpleLayout();

FileAppender appender = null;



appender = new FileAppender(layout,"output1.txt",false);


catch(Exception e) {}


logger.setLevel((Level) Level.DEBUG);

logger.debug("Here is some DEBUG");

logger.info("Here is some INFO");

logger.warn("Here is some WARN");

logger.error("Here is some ERROR");

logger.fatal("Here is some FATAL");



Re: how to use log4j using struts appliation..?

Posted By:   Christopher_Koenigsberg  
Posted On:   Thursday, April 24, 2003 06:41 AM

My way may not be the best way, but it works so far. I have a separate Initializer servlet, in which I only have an "init()" static method, and nothing else. In that servlet's init(), I do the log4j BasicConfigurator. I have this servlet load-on-startup in the web.xml. I never use any URL's referring to this servlet; I just want its init() to run at startup of the web application in the servlet container engine (Tomcat, maybe migrating to JBoss and/or WebLogic).

So, once the init() has Basically Configurated the log4j stuff, then in my regular Struts Action classes and ActionForms (which of course run from the regular Struts ActionServlet as usual), I can just getLogger's and log to them.

One thing I still haven't gotten clear, which I may have to modify my Initializer to fix (probably using something beyond just the BasicConfigurator, I suspect, but have no time to work on it yet), is that if I turn on debugging for my own loggers, it also turns on the flood of internal debugging from Struts including all sorts of Common Digestifier etc. crap. But if I turn off the Struts/Commons debugging, it also turns off my own debugging!

I have a convention where I getLogger(myclassname) e.g. for each of my classes I get a logger under the fully qualified name of the class (including the package prefix). So I had thought I could control debug logging levels etc. by distinguishing the package prefixes (in log4j.properties) since mine all start with "com" and Apache Struts/Commons all start with "org", but it's not working, again maybe because I need something beyond the BasicConfigurator....

About | Sitemap | Contact