logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jacob Kjome <h...@visi.com>
Subject Re: log4j implementation for web module
Date Fri, 24 Nov 2006 23:01:22 GMT
At 01:15 AM 11/24/2006, you wrote:
 >Hi folks,
 >         I am very new log4j and i want to implement log4j in my
 >module. I had gone through material in net.
 >          In my module i am having 5 to 6 servlets. For writting log4j
 >code in web application, we have to provide the properties file like this.
 >  <servlet-name>BackupServlet</servlet-name>
 > <servlet-class>BackupServlet</servlet-class>
 >    <init-param>
 >          <param-name>props</param-name>
 >    </init-param>
 >  </servlet>
 >              And in each servlet init() method, we have to write 
the code like
 >String props = config.getInitParameter("props");
 >            if(props == null || props.length() == 0 ||
 >                    !(new File(props)).isFile()){
 >                    System.err.println(
 >                    "ERROR: Cannot read the configuration file. " +
 >                    "Please check the path of the config init param in
 >                    throw new ServletException();
 >                }
 >            PropertyConfigurator.configure(props);
 >           log = Logger.getLogger(BackupServlet.class);
 > I have few questions here,
 >1. how can i to write some genralized method, so that it can be used
 >in any servlet class.
 >           i.e i dont want to write  "log =
 >for each servlet.
 >  2.I am having so many classes in my module, for each class i cant
 >write        Logger.getLogger(xxxx.class)   .
 >                      Does any body knows please help me out.

1.  Never use File I/O in a web application unless you have a *very* 
good, well though out, reason.  Load resources using the classloader 
or servlet context as input streams or as URL's.  Sticking to this as 
much as possible will keep your code from becoming platform specific 
and brittle.

2.  Use non-static logger instances if you want to avoid having to 
write getLogger(MyClass.class).  A non-static logger can be written 
as getLogger(this.getClass()).  Watch out in cases where the logger 
instance is a member of a Serializable class.  You will need to mark 
it transient (you should make a practice of this in any case) because 
the logger instance itself is non-serializable.

3.  If you have a base class that has a protected logger instance 
member and write it as getLogger(this.getClass()), then you only have 
to write this in the base class.

In any case, you should really read up on a Log4j a bit before 
posting.  I'm pretty sure you would have found this information with 
the simplest of web searches.


To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org

View raw message