Monday, August 13, 2012

Log4j Tutorial : How to Create Multiple Appenders

Step 1: Create a dynamic web application in eclipse
Step 2: Create web.xml file as fallows:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>CMIS_NEW</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>TestLog4j</servlet-name>
<servlet-class>com.cims.TestLog4jServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>TestLog4j</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>



</web-app>

Step 3 :Create Servlet to initialize the Log4j properties through xml file

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);
}

}

}

Step 4: Create your Log4j configuratioin xml and place into the classpath

package com.cims;<br/> <br/>import javax.servlet.ServletException;<br/>import javax.servlet.http.HttpServlet;<br/> <br/>import org.apache.log4j.PropertyConfigurator;<br/>import org.apache.log4j.xml.DOMConfigurator;<br/> <br/>public class Log4JServlet extends HttpServlet {<br/>  <br/> public void init() throws ServletException{<br/>   <br/>  String log4jfile = getInitParameter(&quot;log4j-init-file&quot;);<br/>  if(log4jfile != null){<br/>   String propfile = getServletContext().getRealPath(log4jfile);<br/>   //PropertyConfigurator.configure(propfile);<br/>   DOMConfigurator.configure(propfile);<br/>  }<br/>   <br/> }<br/> <br/>}

Step 5:Now create a servlet to test the configuration

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");

}

}

Here is the output at console




No comments: