[ Team LiB ] Previous Section Next Section

Recipe 14.4 Adding an Appender to the Root Logger


You want to configure an appender or logging destination for the root logger.


Create a configuration file called log4j.properties and place it in the WEB-INF/classes directory of your web application.


Now our discussion moves on to the log4j configuration file, where developers can customize loggers, appenders, and layouts. Here are the steps for using this recipe's examples:

  1. Create a properties file named log4j.properties (its contents look like Example 14-5).

  2. Place the properties file in the WEB-INF/classes directory of the web application.

  3. Import this class into your servlet: org.apache.log4j.Logger.

  4. In the servlet, get a reference to the root logger with the static Logger.getRootLogger( ) method, and start logging.

Example 14-5 configures the root logger, a kind of "super logger" for your application, with a DEBUG level. The root logger uses an appender named cons. This appender is of a type org.apache.log4j.ConsoleAppender, meaning that it sends its log messages to the console.

Example 14-5. The log4j.properties file for creating a root logger appender
log4j.rootLogger=DEBUG, cons



The third line of the log4j.properties file states that the logger will use a SimpleLayout , which logs the level name (DEBUG), a dash (-), and the message itself. Example 14-6 shows the servlet that is using the logger. log4j will find the log4j.properties file automatically in WEB-INF/classes because the servlet has not otherwise configured the logger with a call to BasicConfigurator.configure( ), as shown in Recipe 14.3.

Example 14-6. Using the root logger configured with the log4j.properties file
package com.jspservletcookbook;           

import org.apache.log4j.Logger;

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

public class LoggerWconfig extends HttpServlet {

  private Logger log = null;

  public void init( ){
      //The root logger will get its configuration from
      log = Logger.getRootLogger( );
      log.info("LoggerWconfig 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 some HTML
      java.io.PrintWriter out = response.getWriter( );
        "<html><head><title>Servlet logging</title></head><body>");

        "<h2>Hello from a Logger with a log4j.properties file</h2>");

      out.println("Your logger name is: " + log.getName( )+"<br>");
     } //end doGet

Example 14-6 logs an INFO message in the servlet's init( ) method, then logs two messages in the servlet's doGet( ) service method. The logger logs all of these messages to the console because this is how the log4j.properties file configures the root logger's appender. This is what the console output looks like:

INFO - LoggerWconfig started.
DEBUG - Sending a DEBUG message
INFO - Sending an INFO message

Figure 14-1 shows the servlet's output in a web browser.

Figure 14-1. The logger displays its name in a servlet

See Also

Recipe 14.2 on downloading and setting up log4j; Recipe 14.3 on using a log4j logger without a properties file; Recipe 14.5-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