Create your Dynamic Web project as fallows:
web.xml
index.jsp
Log4JServlet
com.test.Test2Servlet
com.cims.TestLog4jServlet
Output
web.xml
package com.cims; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.log4j.Logger; public class TestLog4jServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; static Logger logger = Logger.getLogger(TestLog4jServlet.class); public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException{ logger.info("Starting the "+this.getClass().getName()+" doGet() Method"); res.setContentType("text/html"); PrintWriter out = res.getWriter(); out.println("Log4j Debugger Example"); logger.info("Exiting the "+this.getClass().getName()+" doGet() Method"); logger.debug("Debug Logs"); logger.warn("Warn Messages"); logger.error("Error Logs"); logger.fatal("Fatal logs"); } }
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Log4j MultiAppender Example</title> </head> <body> <a href="test"> Test MultiAppender</a> </body> </html>
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'> <appender name="Test2Servlet" class="org.apache.log4j.DailyRollingFileAppender"> <param name="File" value="D:/Test2Servlet.log" /> <param name="Append" value="false" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%-4r [%t] %-5p %c %x - %m%n" /> </layout> </appender> <appender name="TestLog4jServlet" class="org.apache.log4j.DailyRollingFileAppender"> <param name="File" value="D:/TestLog4jServlet.log" /> <param name="Append" value="false" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%-4r [%t] %-5p %c %x - %m%n" /> </layout> </appender> <category name="com.test"> <priority value="info" /> <appender-ref ref="Test2Servlet" /> </category> <category name="com.cims"> <priority value="ERROR" /> <appender-ref ref="TestLog4jServlet" /> </category> <root> <level value="DEBUG" /> </root> </log4j:configuration>
Log4JServlet
package com.cims; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import org.apache.log4j.PropertyConfigurator; import org.apache.log4j.xml.DOMConfigurator; public class Log4JServlet extends HttpServlet { public void init() throws ServletException{ String log4jfile = getInitParameter("log4j-init-file"); if(log4jfile != null){ String propfile = getServletContext().getRealPath(log4jfile); //PropertyConfigurator.configure(propfile); DOMConfigurator.configure(propfile); } } }
com.test.Test2Servlet
package com.test; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.log4j.Logger; public class Test2Servlet extends HttpServlet{ /** * */ private static final long serialVersionUID = 1L; static Logger logger = Logger.getLogger(Test2Servlet.class); public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException{ res.setContentType("text/html"); PrintWriter out = res.getWriter(); out.println("inside the "+this.getClass().getName()); logger.info("Starting "+this.getClass().getName()+".doGet() method"); logger.debug("Debug logs"); logger.warn("warn log"); logger.error("Error logs"); logger.fatal("fatal logs"); logger.info("Exiting "+this.getClass().getName()+".doGet() method"); } }
com.cims.TestLog4jServlet
package com.cims; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.log4j.Logger; public class TestLog4jServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; static Logger logger = Logger.getLogger(TestLog4jServlet.class); public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException{ logger.info("Starting the "+this.getClass().getName()+" doGet() Method"); res.setContentType("text/html"); PrintWriter out = res.getWriter(); out.println("Log4j Debugger Example"); logger.info("Exiting the "+this.getClass().getName()+" doGet() Method"); logger.debug("Debug Logs"); logger.warn("Warn Messages"); logger.error("Error Logs"); logger.fatal("Fatal logs"); RequestDispatcher rd = getServletContext().getRequestDispatcher("/test2"); rd.forward(req, res); } }
Output
No comments:
Post a Comment