[ Team LiB ] Previous Section Next Section

Recipe 14.3 Using a Logger Without a Configuration File

Problem

You want to use a logger in a servlet without setting up your own configuration file.

Solution

Create the logger in the servlet and use the org.apache.log4j.BasicConfigurator class to configure the logger.

Discussion

log4j allows the configuration of a logger without a provided configuration or properties file. Example 14-3 is a servlet that instantiates a logger in its init( ) method, which the servlet container calls when the servlet instance is created. The static BasicConfigurator.configure( ) method creates a ConsoleAppender; in other words, the logger will log its messages to the console using a default format.

Example 14-3. A servlet uses BasicConfigurator to configure a logger
package com.jspservletcookbook;           

import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;

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

public class LoggerNconfig extends HttpServlet {

private Logger log = null;

  public void init( ){
  
      //use the root logger
      log = Logger.getRootLogger( );
      
      //this logger will log to the console with a default message format
      BasicConfigurator.configure( );

  
  }

  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 some HTML
        response.setContentType("text/html");

        java.io.PrintWriter out = response.getWriter( );
        out.println(
          "<html><head><title>Servlet logging</title></head><body>");
        out.println("<h2>Hello from a Logger with no Config file</h2>");

        //This logger's parent is the root logger
        out.println(
            "Your logger name is: " + log.getName( )+"<br>");
        out.println(
          "Your logger parent is: " + log.getParent( ).getName( )+"<br>");

        out.println("</body></html>");

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

Example 14-4 shows an example message from the servlet. The message is based on a default format that includes the thread name (Thread-5), the level name (DEBUG), the logger name (com.jspservletcookbook.LoggerNconfig), and the actual message ("Sending a DEBUG message"). Recipe 14.5 shows how to create a format pattern for logging messages, so that you can customize the type of information that the logger sends.

Example 14-4. Example of a logged message using BasicConfigurator
4061660 [Thread-5] DEBUG com.jspservletcookbook.LoggerNconfig  - Sending a DEBUG message
4061660 [Thread-5] INFO com.jspservletcookbook.LoggerNconfig  - Sending an INFO message

Here is the pattern used for the layout associated with BasicConfigurator:

%-4r [%t] %-5p %c %x - %m%n

See Recipe 14.5 for details on the org.apache.log4j.PatternLayout class.

See Also

Recipe 14.2 on downloading and setting up log4j; Recipe 14.4-Recipe 14.8 on using log4j to design your own custom logging mechanism; the log4j download site: http://jakarta.apache.org/log4j/docs/download.html; the log4j Javadoc page: http://jakarta.apache.org/log4j/docs/api/index.html; the log4j project documentation page: http://jakarta.apache.org/log4j/docs/documentation.html.

    [ Team LiB ] Previous Section Next Section