Debugging Servlets - How to write debugging data to a log file
(Page 3 of 4 )
If you can’t print debugging data to the console, you can print debugging data to a log file as shown in figure 5-11. Although each servlet engine uses log files a little differently, you should be able to use these log methods with any servlet engine. However, you may need to check the documentation for your servlet engine to see how it works with log files.
To write data to a log file, you can use the two log methods of the HttpServlet class. If you just want to write a message to a log file, you can use the first log method. But if you want to write a message to the log file along with the stack trace for an exception, you can use the second log method. A stack trace is a series of messages that presents the chain of method calls that precede the current method.
The code in this figure uses the first log method to display the value for the emailAddress variable of the EmailServlet class. Then, it uses the second log method to print a message and a stack trace for an IOException. The data that’s printed by these two log methods is shown in the TextPad window.
Tomcat 4.0 stores all log files in its logs directory. Within this directory, Tomcat stores several types of log files with one file of each type for each date. In the TextPad window, you can see the contents of the localhost_log.2002-10-15.txt file. This log file contains the value of the emailAddress variable as well as the stack trace for an IOException.
Often, the servlet engine automatically writes other information in the same log file that the log methods of the HttpServlet class use. In that case, you can write your debugging information to a separate text file to make it easier to view your debugging messages. To do that, you can create your own class that writes error messages to a file.
To illustrate, the CD that comes with this book includes a LogUtil class in the util package that contains log methods that work like the log methods shown in this figure. However, you can easily modify this class to specify a name and location for the log file. Then, you can use the log methods in this class to write your debugging information to the file.
Two methods of the HttpServlet class used to log errors
MethodMethod | Description |
log(String message) | Writes the specified message to the server’s error log. |
log(String message, Throwable t) | Writes the specified message and stack trace for the exception to the server’s error log. |
Servlet code that prints data to a server specific log file
String emailAddress = request.getParameter("emailAddress");
log("EmailServlet emailAddress: " + emailAddress);
User user = new User(firstName, lastName, emailAddress);
try{
UserIO.addRecord(user, file);
}
catch(IOException ioe){
log("EmailServlet IOException in UserIO", ioe);
}
The location of log files in Tomcat 4.0
\tomcat\logs
A typical log file

Figure 5-11. How to write debugging data to a log file
Description
- You can use the log methods of the HttpServlet class to write debugging information to a log file. Tomcat 4.0 stores its log files in a directory named logs.
- The name and location of the log files for a servlet engine may vary depending on the servlet engine. To find the name and location of your log files, check the documentation for your servlet engine.
- A stack trace is the chain of method calls for any statement that calls a method.
Next: Perspective >>
More Java Articles
More By Murach Publishing
|
This article is excerpted from chapter five of the book Murach's Java Servlets and JSP, written by Andrea Steelman and Joel Murach (Murach; ISBN: 1890774189). Check it out today at your favorite bookstore. Buy this book now.
|
|