RootCause Exception


Tracing Your Code

One of the ways you can determine what is happening in a servlet is to produce milestone indicators to either the client browser or the console. Resin allows this type of tracing, but you should use it only with smaller projects because of its inefficiency. Consider the following piece of JSP code with milestones written to System.err:

 <%  System.err.println("Handling username");     String username = null;     out.print("The user's name is " + username + "<BR>"); System. err.println("Doing calculation");     out.print("Calculation of 5 / 1 = " + 5/1 + "<BR>"); System.err.println("Doing comparison");     if (1 < 2)       out.print("1 > 2");     else       out.print("1 < 2"); %> 

This code example contains milestones that use the System. err.println() statement. When the system encounters these statements, it prints the text to the current location for System.err. On Resin, the default path for System.err is the console window where the server was started. Depending on the output received on the console, you can match the last text with its location in the source code. Unfortunately, you should limit your use of System.err because the code might be executing on a shared server, and the output will go to the primary console window.

A better solution is to use a log file, which can be incorporated in the source code and turned on or off based on the deployment of the code. In production, the log is turned off. In the Resin server, logging is accomplished with a combination of a stream, entries in the config file, and code.

Resin Logging

Resin includes a logging mechanism built into the Server, which provides the ability to write to a stream. You set up the log in the Resin configuration file and write to it in a servlet or JSP page.

First, let's consider the configuration file. The logging element is called <log>, and it includes a number of attributes:

  • name— The name of the debugging section.

  • path— The destination file.

  • timestamp— Format for the timestamp in the file.

  • rollover-count— Number of different log files to keep; default is 2.

  • rollover-period— How often to create new log files, typically using days (D), week(W), or months(M).

The name value can be a custom value such as /testdebug; this value indicates that debugging data from the testdebug class should be logged to the specified file. You could use mycompany.com/jsp to log all debugging data from the JSP files from the mycompany.com domain. Other values specific to the server are as follows:

  • "com.caucho.jsp"— Debug JSP files.

  • "com.caucho.java"— Debug Java compilations.

  • "com.caucho.xsl"— Debug XSL processing.

  • "com.caucho.server.port"— Debug TCP connections.

  • "com.caucho.server.clustering"— Debug clustering.

  • "com.caucho.sql"— Debug all database connection pooling.

  • "com.caucho.http"— Debug all HTTP information.

  • "com.caucho.server.session"— Debug HTTP sessions.

Let's debug the simple JSP application from the beginning of the chapter. This JSP file is called testdebug.jsp, so the configuration file information is as follows:

 <caucho.com>   <log name='/testdebug'        path='logging.txt'        timestamp='[%Y-%m-%d %H:%M:%S.%s]'/> </caucho.com 

This configuration information tells the system that a file called logging.txt should be placed in the root Resin server directory if an application opens the log ID with the name testdebug. Here's what the logging code looks like:

 <HTML> <BODY> <resin xmlns="http://caucho.com/ns/resin">   <log name="testdebug" path="logging" /> </resin> <%@ page import="java.util.logging.*" %> <%!   static Logger debug = Logger.getLogger("testdebug"); %> <% if (debug.isLoggable(Level.FINE))   log.fine("printing name"); if (debug.isLoggable(Level.FINE))   log.fine("doing calculation"); out.print("Calculation of 5/1 = " + 5/1 + "<BR>"); if (debug.isLoggable(Level.FINE))   log.fine("doing comparison"); if (1 < 2)   out.print("1 > 2"); else   out.print("1 < 2"); %> </BODY> </HTML> 

You must include a couple of important pieces of code in either a JSP or servlet application. First, you must import the java.util.logging.* class hierarchy, which imports the JDK 1.4 logging API. First, you must import the java.util.logging.*, which imports the JDK 1.4 logging API. Next, you set up a JSP declaration and instantiate a variable of type Logger through the call to getLogger() of the Logger static variable. The single parameter to the getLogger() method is the log ID, found in the configuration file.

When the code is executing, it checks the isLoggable() method to see if the log is configured for writing. The output from the page should be:

 [2002-09-16 21:06:35.961] printing name [2002-09-16 21:06:35.961] doing calculation [2002-09-16 21:06:35.961] doing comparison 




Mastering Resin
Mastering Resin
ISBN: 0471431036
EAN: 2147483647
Year: 2002
Pages: 180

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net