Hi all,
i'm trying to use NDC in my servlets.

I have a logger wrapper[PvnLogger.java] with push()
and pop() methods besides logger methods. The sample
Servlet[sampleServlet.java] makes use of push and pop.



Attached are the files. I'm not sure if i'm doing the
correct thing. Any suggestion will help me greatly.
Thanks,
Krishna



__________________________________________________
Do You Yahoo!?
Yahoo! Health - your guide to health and wellness
http://health.yahoo.com

import org.apache.log4j.*;
import org.apache.log4j.NDC;

/**
* Wrapper class for log4j. Performs basic logging.
*/

public class PvnLogger {

   /**
        * Class name
    */
        private static final String className = PvnLogger.class.getName();

   /**
        * Logger object
        */
        private static Logger log = Logger.getLogger(className);

   /**
    * Default Constructor
    */
        public PvnLogger() {
        }

        public static void push(String message) {
                NDC.push(message);
        }

        public static void pop() {
                NDC.pop();
        }

        public static void debug(Class c, Object message) {
                Category.getInstance(c).log(className, Priority.DEBUG, message, null);
        }

        //more methods here...
        //.
        //.
        //.
}

import javax.servlet.*;
import javax.servlet.http.*;

public class sampleServlet extends HttpServlet {

        protected void processRequest(HttpServletRequest request, HttpServletResponse 
response)
    throws ServletException, java.io.IOException  {

                response.setContentType("text/html");
                java.io.PrintWriter out = response.getWriter();

                Class c = sampleServlet.class;
                HttpSession session = request.getSession();
                if( session != null )
                        PvnLogger.push("#### Session ID: ####" + session.getId());
                try{
                        PvnLogger.debug(c,"this is a test");
                        //do something here
                }catch(Exception e){
                        //do something here
                }
                finally{
                        if(session != null)
                        PvnLogger.pop();
                }

        }
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, java.io.IOException {
                processRequest(request, response);
        }

        protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, java.io.IOException {
                processRequest(request, response);
        }

}
--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to