this patch decouples digester logging into an interface and a standard 
implementation (that has the same functionality as the current digester 
logging system). if the user wants to use a different logging system, then 
they can simply create an implementation and set a property.

the cool thing about this approach is that it retains compatibility with 
existing code whilst allow users to plug- in their own favourite logging 
systems if they want to do so.


- robert

Index: digester/src/java/org/apache/commons/digester/Digester.java
===================================================================
RCS file: 
/home/cvs/jakarta-commons/digester/src/java/org/apache/commons/digester/Digester.java,v
retrieving revision 1.13
diff -u -r1.13 Digester.java
--- digester/src/java/org/apache/commons/digester/Digester.java 2001/08/13 18:59:46    
 1.13
+++ digester/src/java/org/apache/commons/digester/Digester.java 2001/08/14 20:18:34
@@ -250,10 +250,9 @@
 
 
     /**
-     * The PrintWriter to which we should send log output, or
-     * <code>null</code> to write to <code>System.out</code>.
+     * Used by digester for logging.
      */
-    protected PrintWriter writer = null;
+    protected DigesterLogger logger = new BaseDigesterLogger();
 
 
     // ----------------------------------------------------------- Properties
@@ -456,27 +455,69 @@
 
 
     /**
-     * Return the logging writer for this Digester.
+     * <p> Return the logging writer for this Digester.
+     *
+     * @deprecated Logging is now decoupled and need not use a 
+<code>PrintWriter</code>
      */
     public PrintWriter getWriter() {
-
-        return (this.writer);
-
+        // yes, it's s bit of a hack
+        // but it's the neatest way to retain compatibility
+        if (logger instanceof BaseDigesterLogger)
+        {
+            return (((BaseDigesterLogger)logger).getWriter());
+        }
+        return null;
     }
 
 
     /**
-     * Set the logging writer for this Digester.
+     * <p> Set the logging writer for this Digester.
+     * This method still works but is depricated.
      *
      * @param writer The new PrintWriter, or <code>null</code> for
      *  <code>System.out</code>.
+     * @deprecated Call {@link #setLogger} with a {@link BaseDigesterLogger} instead.
      */
     public void setWriter(PrintWriter writer) {
-
-        this.writer = writer;
+        // yes, it's s bit of a hack
+        // but it's the neatest way to retain compatibility
+        if (logger instanceof BaseDigesterLogger)
+        {
+        
+            ((BaseDigesterLogger)logger).setWriter(writer);
+        
+        } else {
+        
+            setLogger(new BaseDigesterLogger(writer));
+        
+        }
+    }
 
+    /**
+     * <p> Get the logger for this Digester.
+     *
+     * <p> {@link DigesterLogger} decouples logging.
+     * {@link BaseDigesterLogger} is the default implementation 
+     * and has the same functionality as the original digester logging system.
+     */
+    public DigesterLogger getLogger()
+    {
+        return logger;
     }
 
+    /**
+     * <p> Set the logger for this Digester.
+     *
+     * <p> {@link DigesterLogger} decouples logging.
+     * {@link BaseDigesterLogger} is the default implementation 
+     * and has the same functionality as the original digester logging system.
+     *
+     * @param logger the new logger or <code>null</code> to disable logging 
+     */    
+    public void setLogger(DigesterLogger logger)
+    {
+        this.logger=logger;
+    }
 
     /**
      * Return the boolean as to whether the context classloader should be used.
@@ -897,36 +938,37 @@
 
 
     /**
-     * Log a message to the log writer associated with this context.
+     * Log a message and associated exception.
+     * Log to the {@link DigesterLogger}
+     * associated with this digester.
      *
      * @param message The message to be logged
      */
     public void log(String message) {
-
-        if (writer == null)
-            System.out.println(message);
-        else
-            writer.println(message);
-
+        // support null loggers
+        if (logger!=null)
+        {
+            logger.log(message);
+        }
     }
 
 
     /**
-     * Log a message and associated exception to the log writer
-     * associated with this context.
+     * Log a message and associated exception.
+     * Log to the {@link DigesterLogger}
+     * associated with this digester.
      *
      * @param message The message to be logged
      * @param exception The associated exception to be logged
      */
     public void log(String message, Throwable exception) {
 
-        if (writer == null) {
-            System.out.println(message);
-            exception.printStackTrace(System.out);
-        } else {
-            writer.println(message);
-            exception.printStackTrace(writer);
+        // support null loggers
+        if (logger!=null)
+        {
+            logger.log(message,exception);
         }
+
 
     }
 

DigesterLogger.java

BaseDigesterLogger.java

Reply via email to