You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Kenneth Brooks <ke...@sevensoft.net> on 2003/08/02 03:40:50 UTC

Re[2]: Servlets & JSPs log to different files

I currently do the following.
See the 3 or 4 sections of code below.

I have a servlet that is loaded at the time tomcat starts.
Pay no attention to the crap coding in this file, but you get the idea

//////////////////// BEGIN EimplementLogger.java ///////////////////////
package whateverpackage.log;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

import org.apache.log4j.xml.*;

import javax.naming.NamingException;
import javax.naming.Context;
import javax.naming.InitialContext;

public class EimplementLogger extends HttpServlet {
   DOMConfigurator domConfig;

  public void init() throws ServletException {
     // init code here
     domConfig =     new DOMConfigurator();

     // point to log4j configuration file
     Context ctx = null;
     String log4jxmlfilepath = null;

     try {
        ctx = new InitialContext();
     } catch (NamingException e) {
//       System.out.println("Couldn't build an initial context : " + e);
       return;
     }

     try {
        log4jxmlfilepath = (String)(ctx.lookup("java:/comp/env/log4jxmlfilepath"));
     } catch (NamingException e) {
       System.out.println("JNDI lookup failed : " + e);
     }

     domConfig.configure(log4jxmlfilepath + "log4j.xml");
   }

}
///////////////// END EimplementLogger.java ////////////////////////

In my web.xml i have the following to start this servlet up.
        <servlet>
                <servlet-name>
                        EimplementLogger
                </servlet-name>
                
                <servlet-class>
                        whateverpackage.log.EimplementLogger
                </servlet-class>
                <load-on-startup>5</load-on-startup>
        </servlet>

then in the class (bean) that has no idea about the context i have the
following:
   protected org.apache.log4j.Logger logWriter =
   org.apache.log4j.Logger.getLogger("nameoffiletoappearinlog");

and i use it like so
logWriter.debug("here is a debug statement");

hope that helps

-ken






Friday, August 1, 2003, 8:51:28 PM ---------\][/

AB> G'day,

AB> Can anyone expand on this further and explain how to use a Log4j logger
AB> reference in beans that do not have a notion of what the underlying servlet
AB> context is?  The only way I can see how to get this to work is to pass the
AB> logger reference as an argument in the bean's constructor.  

AB> My query does seem to be out of context for this list so my apologies in
AB> advance.

AB> AB

AB> -----Original Message-----
AB> From: Shapira, Yoav [mailto:Yoav.Shapira@mpi.com] 
AB> Sent: Saturday, 2 August 2003 3:24 AM
AB> To: Tomcat Users List
AB> Subject: RE: Servlets & JSPs log to different files



AB> Howdy,
AB> Then use log4j.

AB> Yoav Shapira
AB> Millennium ChemInformatics


>>-----Original Message-----
>>From: Rick Roberts [mailto:techinfo@ait-web.com]
>>Sent: Friday, August 01, 2003 1:18 PM
>>To: Tomcat Users List
>>Subject: Re: Servlets & JSPs log to different files
>>
>>>
>>>
>>> How are you logging in your servlets?  Are you using 
>>> System.out/System.err?  If so, don't use them, and instead use the
>>> ServletContext#log(..) methods.
>>
>>Actually, they are mostly beans with no ServletContext :(
>>
>>--
>>*******************************************
>>* Rick Roberts                            *
>>* Advanced Information Technologies, Inc. *
>>*******************************************
>>
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
>>For additional commands, e-mail: tomcat-user-help@jakarta.apache.org




AB> This e-mail, including any attachments, is a confidential business
AB> communication, and may contain information that is confidential, proprietary
AB> and/or privileged.  This e-mail is intended only for the individual(s) to
AB> whom it is addressed, and may not be saved, copied, printed, disclosed or
AB> used by anyone else.  If you are not the(an) intended recipient, please
AB> immediately delete this e-mail from your computer system and notify the
AB> sender.  Thank you.


AB> ---------------------------------------------------------------------
AB> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
AB> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org




AB> ---------------------------------------------------------------------
AB> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
AB> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org




/][/---------\][\