Recipe 14.4 Adding an Appender to the Root Logger

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 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 (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 file for creating a root logger appender
 log4j.rootLogger=DEBUG, cons log4j.appender.cons=org.apache.log4j.ConsoleAppender log4j.appender.cons.layout=org.apache.log4j.SimpleLayout 

The third line of the 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 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 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       //WEB-INF/classes/       log = Logger.getRootLogger( );    "LoggerWconfig started.");   }  public void doGet(HttpServletRequest request,      HttpServletResponse response)       throws ServletException, {  //display a DEBUG-level message       log.debug("Sending a DEBUG message");       // display an INFO-level message"Sending an INFO message");  //better display some HTML       response.setContentType("text/html"); out = response.getWriter( );       out.println(         "<html><head><title>Servlet logging</title></head><body>");       out.println(         "<h2>Hello from a Logger with a file</h2>");  out.println("Your logger name is: " + log.getName( )+"<br>");  out.println("</body></html>");      } //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 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:; the log4j Javadoc page:; the log4j project documentation page:

Java Servlet & JSP Cookbook
Java Servlet & JSP Cookbook
ISBN: 0596005725
EAN: 2147483647
Year: 2004
Pages: 326

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: