In debugging my Velocity web-app I've been frustrated in that there
seemed to be no way to get velocity to log the stack trace when
an error happens. Setting of VelocityEngine.RUNTIME_LOG_ERROR_STACKTRACE
or even VelocityEngine.RUNTIME_LOG_WARN_STACKTRACE seemed
to not do anything. After a few hours of poking around, I discovered
that in fact the code was not there to log stack traces.

There were two problems:
1) RuntimeInstance.showStackTrace() first checks
configuration.isInitialized(), which
always seems to return false. :(
2) Parse.render did not pass the exception to the logging system, it just
constructed a message with catch(Exception e) { log("...message.."
+e.getMessage());}

I propose the following fixes :
1) In RuntimeInstance.showStackTrace() do not check
configuration.isInitialized().
2) To RuntimeLogger interface add a error(String message, Throwable t)
method.

I have implemented the above, please consider the attached patches, I think
they will
greatly help anyone debugging their Velocity applications.


Also note that as far as I can tell the
VelocityEngine.RUNTIME_LOG_ERROR_STACKTRACE
setting is not referenced anywhere. I would suggest replacing
RUNTIME_LOG_ERROR_STACKTRACE
and RUNTIME_LOG_WARN_STACKTRACE, with RUNTIME_LOG_STACKTRACE, since that
seems to be the actual behaviour. I have not implemented this.

Regards,


Victor Wiewiorowski
ValueCommerce Co., Ltd.
Index: java/org/apache/velocity/runtime/RuntimeInstance.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-velocity/src/java/org/apache/velocity/runtime/RuntimeInstance.java,v
retrieving revision 1.17
diff -u -r1.17 RuntimeInstance.java
--- java/org/apache/velocity/runtime/RuntimeInstance.java       9 Aug 2002 00:54:53 
-0000       1.17
+++ java/org/apache/velocity/runtime/RuntimeInstance.java       3 Apr 2003 01:46:00 
-0000
@@ -1011,7 +1011,24 @@
     {
         log(LogSystem.ERROR_ID, message);
     }
-    
+
+    /**
+     * Log an error message.
+     *
+     * @param Object message to log
+     */
+    public void error(String message, Throwable t)
+    {
+        String msg = message;
+        if(showStackTrace()) {
+            msg += "Exception : ";
+            msg += t.getMessage();
+            msg += "\n";
+            msg += StringUtils.stackTrace(t);
+        }
+        log(LogSystem.ERROR_ID, msg);
+    }
+
     /**
      * Log a debug message.
      *
Index: java/org/apache/velocity/runtime/RuntimeLogger.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-velocity/src/java/org/apache/velocity/runtime/RuntimeLogger.java,v
retrieving revision 1.1
diff -u -r1.1 RuntimeLogger.java
--- java/org/apache/velocity/runtime/RuntimeLogger.java 21 Apr 2002 18:35:18 -0000     
 1.1
+++ java/org/apache/velocity/runtime/RuntimeLogger.java 3 Apr 2003 01:46:00 -0000
@@ -84,6 +84,14 @@
     public void error(Object message);
 
     /**
+     * Log an error caused by an exception.
+     *
+     * @param Object message to log
+     * @param Throweable t the exception that caused this error
+     */
+    public void error(String message, Throwable t);
+
+    /**
      * Log a debug message.
      *
      * @param Object message to log
Index: java/org/apache/velocity/runtime/directive/Parse.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-velocity/src/java/org/apache/velocity/runtime/directive/Parse.java,v
retrieving revision 1.26
diff -u -r1.26 Parse.java
--- java/org/apache/velocity/runtime/directive/Parse.java       10 Oct 2002 17:10:30 
-0000      1.26
+++ java/org/apache/velocity/runtime/directive/Parse.java       3 Apr 2003 01:46:00 
-0000
@@ -209,7 +209,7 @@
         } 
         catch ( Exception e)
         {      
-               rsvc.error("#parse() : arg = " + arg + ".  Exception : " + e);
+               rsvc.error("#parse() : arg = " + arg + ".", e);
             return false;
         }
     
@@ -232,7 +232,7 @@
                 throw (MethodInvocationException) e;
             }
 
-            rsvc.error( "Exception rendering #parse( " + arg + " )  : " + e );
+            rsvc.error( "Exception rendering #parse( " + arg + " )  : ", e );
             return false;
         }
         finally

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to