You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Seetha Rama Krishna <ra...@yahoo.co.in> on 2006/11/24 08:15:44 UTC

log4j implementation for web module

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>
  <servlet-name>BackupServlet</servlet-name>
 <servlet-class>BackupServlet</servlet-class>
    <init-param>
          <param-name>props</param-name>
       <param-value>D:\www\tomcat5\webapps\ruleengine\WEB-INF\config.properties</param-value>
    </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 web.xml");
                    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 = Logger.getLogger(BackupServlet.class);"
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.

 


          
   



  Thanks & Regards,
  Krishna
    

 				
---------------------------------
 Find out what India is talking about on  - Yahoo! Answers India 
 Send FREE SMS to your friend's mobile from Yahoo! Messenger Version 8. Get it NOW

Re: log4j implementation for web module

Posted by Jacob Kjome <ho...@visi.com>.
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>
 >  <servlet-name>BackupServlet</servlet-name>
 > <servlet-class>BackupServlet</servlet-class>
 >    <init-param>
 >          <param-name>props</param-name>
 >
 ><param-value>D:\www\tomcat5\webapps\ruleengine\WEB-INF\config.properti
 >es</param-value>
 >    </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
 >web.xml");
 >                    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 =
 >Logger.getLogger(BackupServlet.class);"
 >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.

Jake



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