Logger Servlet : Log « Servlets « Java

Logger Servlet

//Log4j from Apache is required

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

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

public class LoggerServlet extends HttpServlet {

  private Logger log = null;

  public void init() {

    String realPath = getServletContext().getRealPath("/");
    String fileSep = System.getProperty("file.separator");

    if (realPath != null && (!realPath.endsWith(fileSep)))
      realPath = realPath + fileSep;


    //load the configuration for this application's loggers using the
    // servletLog.properties file
        + "WEB-INF/classes/servletLog.properties");
    //create the logger for this servlet class
    //it will use the configuration for the logger com.java2s.LoggerServlet
    //or inherit from the logger com.java2s if one exists, and so on
    log = Logger.getLogger(LoggerServlet.class);
    log.info("LoggerServlet started.");


  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, java.io.IOException {

    //display a DEBUG level message
    log.debug("Sending a DEBUG message");
    //display an INFO level message
    log.info("Sending an INFO message");

    //better display something
    java.io.PrintWriter out = response.getWriter();
    out.println("<html><head><title>Servlet logging</title></head><body>");
    out.println("<h2>Hello from LoggerServlet</h2>");
    out.println("Your logger name is: " + log.getName() + "<br>");
    out.println("Your logger parent is: " + log.getParent().getName()
        + "<br>");
  } //end doGet

  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, java.io.IOException {

    doGet(request, response);


Related examples in the same category

1.Servlets Logging Filter Demo
2.Logging Filter
3.Context log
4.Logger without configure file
5.Another logger servlet
6.Logger new config
7.Servlet: Root logger
8.Session logger
9.LoggerSkel for Servlet
10.Context logger
11.Log Filter
12.Log in ServletContext
13.Servlet Logging