





















































We will start by briefly looking at each of the logging frameworks mentioned above, and will then go into how the server logs events and errors and where it logs them to. After examining them, we will look into the different ways in which we can configure application logging.
Each of the loggers may be assigned levels. The set of possible levels in an ascending order are—TRACE, DEBUG, INFO, WARN, ERROR, and FATAL. If a logger is not assigned a level, then it inherits its level from its closest ancestor. A log statement makes a logging request to the log4j subsystem. This request is enabled only if its logging level is higher than or equal to its logger's level. If it is lower than the log, then the message is not output through the configured appenders.
Log4j allows logs to be output to multiple destinations. This is done via different appenders. Currently there are appenders for the console, files, GUI components, JMS destinations, NT, and Unix system event loggers and remote sockets. Log4j is one of the most widely-used logging frameworks for Java applications, especially ones running on application servers. It also provides more features than the other logging framework that we are about to see, that is, the Java Logging API.
Apache Geronimo uses slf4j and log4j for logging. The log4j configuration files can be found in the <GERONIMO_HOME>/var/log directory. There are three configuration files that you will find in this directory, namely:
Just as they are named, these files configure log4j logging for the client container (Java EE application client), deployer system, and the server.
You will also find the corresponding log files—client.log, deployer.log, and server.log. The properties files, listed above, contain the configuration of the various appenders, loggers, and layouts for the server, deployer, and client. As mentioned above, log4j provides a hierarchy of loggers with a granularity ranging from the entire server to each class on the server. Let us examine one of the configuration files: the server-log4j.properties file:
We will be illustrating how applications can log messages in Geronimo by using two logging frameworks, namely, log4j and JUL. We will also illustrate how you can use the slf4j wrapper to log messages with the above two underlying implementations. We will be using a sample application, namely, the HelloWorld web application to illustrate this.
We can use log4j for logging the application log to either a separate logfile or to the geronimo.log file. We will also illustrate how the logs can be written to a separate file in the <GERONIMO_HOME>/var/log directory, by using a GBean.
Logging to the geronimo.log file and the command console is the simplest way to do application logging in Geronimo. For enabling this in your application, you only need to add logging statements to your application code. The HelloWorld sample application has a servlet called HelloWorldServlet, which has the following statements for enabling logging. The servlet is shown below.
package com.packtpub.hello;
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.apache.log4j.Logger;
public class HelloWorldServlet extends HttpServlet
{
Logger logger = Logger.getLogger(HelloWorldServlet.class.getName());
protected void service(HttpServletRequest req, HttpServletResponse
res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.print("<html>");
logger.info("Printing out <html>");
out.print("<head><title>Hello World Application</title></head>");
logger.info("Printing out <head><title>Hello World Application</
title></head>");
out.print("<body>");
logger.info("Printing out <body>");
out.print("<b>Hello World</b><br>");
logger.info("Printing out <b>Hello World</b><br>");
out.print("</body>");
logger.info("Printing out </body>");
out.print("</html>");
logger.info("Printing out </html>");
logger.warn("Sample Warning message");
logger.error("Sample error message");
}
}
Deploy the sample HelloWorld-1.0.war file, and then access http://localhost:8080/HelloWorld/. This servlet will log the following messages in the command console, as shown in the image below:
The geronimo.log file will have the following entries:
2009-02-02 20:01:38,906 INFO [HelloWorldServlet] Printing out <html>
2009-02-02 20:01:38,906 INFO [HelloWorldServlet] Printing out
<head><title>Hello World Application</title></head>
2009-02-02 20:01:38,906 INFO [HelloWorldServlet] Printing out <body>
2009-02-02 20:01:38,906 INFO [HelloWorldServlet] Printing out <b>Hello World
</b><br>
2009-02-02 20:01:38,906 INFO [HelloWorldServlet] Printing out </body>
2009-02-02 20:01:38,906 INFO [HelloWorldServlet] Printing out </html>
2009-02-02 20:01:38,906 WARN [HelloWorldServlet] Sample Warning message
2009-02-02 20:01:38,906 ERROR [HelloWorldServlet] Sample error message
Notice that only the messages with a logging level of greater than or equal to WARN are being logged to the command console, while all the INFO, ERROR, and WARN messages are logged to the geronimo.log file. This is because in server-log4j.properties the CONSOLE appender's threshold is set to the value of the system property, org.apache.geronimo.log.ConsoleLogLevel, as shown below:
log4j.appender.CONSOLE.Threshold=${org.apache.geronimo.log.
ConsoleLogLevel}
The value of this property is, by default, WARN. All of the INFO messages are logged to the logfile because the FILE appender has a lower threshold, of TRACE, as shown below:
log4j.appender.FILE.Threshold=TRACE
Using this method, you can log messages of different severity to the console and logfile to which the server messages are logged. This is done for operator convenience, that is, only high severity log messages, such as warnings and errors, are logged to the console, and they need the operator's attention. The other messages are logged only to a file.