[LOGBack-dev] svn commit: r578 - in logback/trunk/logback-classic/src: main/java/ch/qos/logback/classic/html test/java/ch/qos/logback/classic/html

noreply.seb at qos.ch noreply.seb at qos.ch
Wed Sep 13 19:40:25 CEST 2006


Author: seb
Date: Wed Sep 13 19:40:25 2006
New Revision: 578

Added:
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/html/DefaultThrowableRenderer.java
      - copied, changed from r576, /logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/html/ThrowableRenderer.java
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/html/IThrowableRenderer.java
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/html/NOPThrowableRenderer.java
Removed:
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/html/ThrowableRenderer.java
Modified:
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/html/HTMLLayout.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/html/HTMLLayoutTest.java

Log:

- added IThrowableRenderer, an interface to allow multiple implementations of ThrowableRenderer to be easily used
- added NOPThrowableRenderer, a no op implementation of the IThrowableRenderer interface
- the HTMLLayout now uses the DefaultThrowableRenderer by default.
- updated tests
- on going work on the HTMLLayout javadoc


Copied: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/html/DefaultThrowableRenderer.java (from r576, /logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/html/ThrowableRenderer.java)
==============================================================================
--- /logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/html/ThrowableRenderer.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/html/DefaultThrowableRenderer.java	Wed Sep 13 19:40:25 2006
@@ -3,14 +3,15 @@
 import static ch.qos.logback.core.Layout.LINE_SEP;
 import ch.qos.logback.classic.helpers.Transform;
 import ch.qos.logback.classic.spi.LoggingEvent;
+import ch.qos.logback.classic.spi.ThrowableInformation;
 
-public class ThrowableRenderer {
+public class DefaultThrowableRenderer implements IThrowableRenderer {
   
   static final String TRACE_PREFIX = "<br />&nbsp;&nbsp;&nbsp;&nbsp;";
   
   Throwable throwable;
   
-  public ThrowableRenderer() {
+  public DefaultThrowableRenderer() {
     
   }
   
@@ -24,6 +25,7 @@
       if (len == 0) {
         return;
       }
+      sbuf.append("<tr><td class=\"Exception\" colspan=\"6\">");
       sbuf.append(Transform.escapeTags(s[0]));
       sbuf.append(LINE_SEP);
       for (int i = 1; i < len; i++) {
@@ -31,10 +33,14 @@
         sbuf.append(Transform.escapeTags(s[i]));
         sbuf.append(LINE_SEP);
       }
-    }    
+      sbuf.append("</td></tr>");
+    }
   }
   
   public void render(StringBuffer sbuf, LoggingEvent event) {
-    render(sbuf, event.getThrowableInformation().getThrowableStrRep());
+    ThrowableInformation ti = event.getThrowableInformation();
+    if (ti != null) {
+      render(sbuf, ti.getThrowableStrRep());
+    }
   }
 }

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/html/HTMLLayout.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/html/HTMLLayout.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/html/HTMLLayout.java	Wed Sep 13 19:40:25 2006
@@ -28,19 +28,25 @@
  * The content of the table columns are specified using a conversion pattern. 
  * See {@link ch.qos.logback.classic.PatternLayout} for documentation on the
  * available patterns.
+ * 
  * Note that the pattern <em>%ex</em> used to display an Exception is not the only way
  * to display an Exception with this layout. 
- * An internal {@link ch.qos.logback.classic.html.ThrowableRenderer} can be called 
- * to render the throwable. If a ThrowableRenderer is specified, it is used to render
- * the Exception on a new line. 
- * If no such object is specified to the HTMLLayout, then one must add <em>%ex</em> 
+ * 
+ * An internal {@link ch.qos.logback.classic.html.IThrowableRenderer} can be called 
+ * to render the throwable. 
+ * By default, a {@link ch.qos.logback.classic.html.DefaultThrowableRenderer} 
+ * is assigned to the HTMLLayout. It writes the Exception on a new table row. 
+ * 
+ * FIXME WRITE MORE ABOUT THIS.
+ * 
+ * In that case, then one must add <em>%ex</em> 
  * to the pattern to display Exceptions.
  * <p>
- * A user-specified external CSS file can be link to the html page. 
- * In case one does not want to custom the html output, an internal CSS
+ * A user-specified external CSS file can be linked to the html page. 
+ * In case one does not want to customize the html output, an internal CSS style
  * is used.
  * 
- * The HTMLLayout is often used in conjunction with a SMTPAppender, to
+ * The HTMLLayout is often used in conjunction with SMTPAppender, to
  * send a nicely formatted html email. Of course, it can be used with any
  * other Appender.
  * 
@@ -87,7 +93,7 @@
 
   private CssBuilder cssBuilder;
 
-  ThrowableRenderer throwableRenderer;
+  IThrowableRenderer throwableRenderer = new DefaultThrowableRenderer();
 
   // counter keeping track of the rows output
   private long counter = 0;
@@ -132,6 +138,13 @@
    */
   @Override
   public void start() {
+    int errorCount = 0;
+    
+    if (throwableRenderer == null) {
+      addError("ThrowableRender cannot be null.");
+      errorCount++;
+    }
+    
     try {
       Parser p = new Parser(pattern);
       if (getContext() != null) {
@@ -142,9 +155,12 @@
       DynamicConverter.startConverters(this.head);
     } catch (ScanException ex) {
       addError("Incorrect pattern found", ex);
+      errorCount++;
     }
 
-    started = true;
+    if (errorCount == 0) {
+      started = true;
+    }
   }
 
   /**
@@ -288,10 +304,8 @@
     buf.append("</tr>");
     buf.append(LINE_SEP);
 
-    if (throwableRenderer != null && event.getThrowableInformation() != null) {
-      buf.append("<tr><td class=\"Exception\" colspan=\"6\">");
+    if (event.getThrowableInformation() != null) {
       throwableRenderer.render(buf, event);
-      buf.append("</td></tr>");
     }
     return buf.toString();
   }
@@ -328,11 +342,11 @@
     }
   }
 
-  public ThrowableRenderer getThrowableRenderer() {
+  public IThrowableRenderer getThrowableRenderer() {
     return throwableRenderer;
   }
 
-  public void setThrowableRenderer(ThrowableRenderer throwableRenderer) {
+  public void setThrowableRenderer(IThrowableRenderer throwableRenderer) {
     this.throwableRenderer = throwableRenderer;
   }
   

Added: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/html/IThrowableRenderer.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/html/IThrowableRenderer.java	Wed Sep 13 19:40:25 2006
@@ -0,0 +1,9 @@
+package ch.qos.logback.classic.html;
+
+import ch.qos.logback.classic.spi.LoggingEvent;
+
+public interface IThrowableRenderer {
+  
+  public void render(StringBuffer sbuf, LoggingEvent event);
+  
+}

Added: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/html/NOPThrowableRenderer.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/html/NOPThrowableRenderer.java	Wed Sep 13 19:40:25 2006
@@ -0,0 +1,11 @@
+package ch.qos.logback.classic.html;
+
+import ch.qos.logback.classic.spi.LoggingEvent;
+
+public class NOPThrowableRenderer implements IThrowableRenderer {
+
+  public void render(StringBuffer sbuf, LoggingEvent event) {
+    return;
+  }
+
+}

Modified: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/html/HTMLLayoutTest.java
==============================================================================
--- logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/html/HTMLLayoutTest.java	(original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/html/HTMLLayoutTest.java	Wed Sep 13 19:40:25 2006
@@ -30,6 +30,7 @@
     ListAppender appender = new ListAppender();
     appender.setContext(lc);
     layout = new HTMLLayout();
+    layout.setThrowableRenderer(new DefaultThrowableRenderer());
     layout.setContext(lc);
     layout.setPattern("%level%thread%msg");
     layout.start();
@@ -65,11 +66,12 @@
   public void testAppendThrowable() throws Exception {
     StringBuffer buf = new StringBuffer();
     String[] strArray = { "test1", "test2" };
-    layout.throwableRenderer.render(buf, strArray);
-    // System.out.println(buf.toString());
+    DefaultThrowableRenderer renderer = (DefaultThrowableRenderer)layout.throwableRenderer;
+    renderer.render(buf, strArray);
+    System.out.println(buf.toString());
     String[] result = buf.toString().split(HTMLLayout.LINE_SEP);
-    assertEquals("test1", result[0]);
-    assertEquals(ThrowableRenderer.TRACE_PREFIX + "test2", result[1]);
+    assertEquals("<tr><td class=\"Exception\" colspan=\"6\">test1", result[0]);
+    assertEquals(DefaultThrowableRenderer.TRACE_PREFIX + "test2", result[1]);
   }
 
   public void testDoLayout() throws Exception {



More information about the logback-dev mailing list