Logging

Logging Dependencies and Implementations

DWR currently depends on Apache Commons Logging. Because we use commons-logging you are free to choose your logging implementation (Log4j is a common choice).

Our Default Logging Policy

Out of the box, DWR is silent about "application" exceptions/errors (when DWR's debug mode is disabled). "Application" exceptions are exceptions/errors thrown from your application:

// Suppose the remoteMe method is exposed to DWR, and called
// through the browser. Also assume this method invocation results
// in a MyBusinessException being thrown.

public String remoteMe() throws MyBusinessException {
// Whatever is here throws MyBusinessException
}
In this situation MyBusinessException is an "application" exception and DWR will not log the exception/error regardless of the severity.

Contrary to "application" exceptions/errors DWR will always log exceptions thrown/caught by DWR internals (Conversion exceptions, Creator exceptions, etc.) at a level dependent of their severity (DEBUG, INFO, WARN, etc.).

Logging Configuration

DWR's default logging policy can be configured by an init-param (accessLogLevel) on the DWR servlet (declared in your web.xml).
...
<servlet>
    <display-name>DWR Servlet</display-name>
    <servlet-name>dwr-invoker</servlet-name>    
    <description>Direct Web Remoter Servlet</description>
    <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>    
    <init-param>
      <param-name>accessLogLevel</param-name>
      <param-value>EXCEPTION</param-value>
    </init-param>
...

The accessLogLevel parameter may have the following values (case insensitive). Similar to other logging frameworks logging requests are enabled if the level is higher than or equal to the accessLogLevel specified (e.g. An accessLogLevel of CALL will result in the logging of all levels):

  1. CALL - Most verbose level. Will log incoming and outgoing DWR calls.
  2. EXCEPTION - Logging of checked Exceptions will be enabled.
  3. RUNTIMEEXCEPTION - Logging of RuntimeExceptions will be enabled
  4. ERROR - Logging of Errors will be enabled.
All access logging is written to the same logger - org.directwebremoting.log.accessLog. This allows an application to easily control and separate DWR logging from the rest of the system.

Debug Mode and Logging

When debug mode is enabled DWR will set the accessLogLevel to EXCEPTION by default. This behavior can be modified by specifying a valid accessLogLevel.

When debug mode is disabled and no accessLogLevel has been specified or an invalid accessLogLevel has been specified all logging will be disabled.

Configuring Log4J for DWR

Using Log4j requires that the Log4j.jar and the Log4j configuration file be in your application's classpath (WEB-INF/lib and WEB-INF/classes). Here is a log4j.xml file that can help get you started (this should be placed in your WEB-INF/classes directory):

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
  <appender name="console" class="org.apache.log4j.ConsoleAppender"> 
    <param name="Target" value="System.out"/> 
    <layout class="org.apache.log4j.PatternLayout"> 
      <param name="ConversionPattern" value="%-5p %c{1} - %m%n"/> 
    </layout> 
  </appender> 

  <appender name="dwrLogFile" class="org.apache.log4j.FileAppender">
     <param name="File" value="C:/Program Files/Apache Software Foundation/Tomcat 6.0/webapps/dwr/log/dwrAccess.log"/>
     <param name="Append" value="true"/>
     <param name="Threshold" value="DEBUG"/>
     <layout class="org.apache.log4j.PatternLayout">
       <param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
     </layout>	    
  </appender>
  
  <appender name="otherFile" class="org.apache.log4j.FileAppender">
     <param name="File" value="C:/Program Files/Apache Software Foundation/Tomcat 6.0/webapps/dwr/log/other.log"/>
     <param name="Append" value="true"/>
     <param name="Threshold" value="DEBUG"/>
     <layout class="org.apache.log4j.PatternLayout">
       <param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
     </layout>	    
  </appender>
  
  <!-- All application exceptions/errors will be written here -->  
  <category name="org.directwebremoting.log.accessLog">
     <priority value="INFO"/>
     <appender-ref ref="dwrLogFile" />
  </category>
  
  <!-- All DWR startup information will be written here --> 
  <category name="org.directwebremoting.log.startup">
     <priority value="DEBUG"/>
     <appender-ref ref="dwrLogFile" />
  </category>
  
  <!-- All DWR script information will be written here --> 
  <category name="org.directwebremoting.log.scripts">
     <priority value="DEBUG"/>
     <appender-ref ref="dwrLogFile" />
  </category>
  
  <!-- All DWR session information will be written here --> 
  <category name="org.directwebremoting.log.session">
     <priority value="DEBUG"/>
     <appender-ref ref="dwrLogFile" />
  </category>
  
  <!-- All other messages will be written here, including exceptions internal to DWR -->
  <root>
    <priority value="DEBUG" /> 
    <appender-ref ref="otherFile" />
  </root>

</log4j:configuration>

See the log4j documentation for more details.

Configuring java.util.logging for DWR

See the java.util.logging documentation for more details.