logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From carn...@apache.org
Subject svn commit: r510307 - in /logging/log4j/branches/v1_2-branch: src/java/org/apache/log4j/net/SyslogAppender.java tests/src/java/org/apache/log4j/net/SyslogAppenderTest.java
Date Wed, 21 Feb 2007 23:37:33 GMT
Author: carnold
Date: Wed Feb 21 15:37:32 2007
New Revision: 510307

URL: http://svn.apache.org/viewvc?view=rev&rev=510307
Log:
Bug 40502: SyslogAppender assumes all stack trace lines start with tab

Modified:
    logging/log4j/branches/v1_2-branch/src/java/org/apache/log4j/net/SyslogAppender.java
    logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/net/SyslogAppenderTest.java

Modified: logging/log4j/branches/v1_2-branch/src/java/org/apache/log4j/net/SyslogAppender.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/v1_2-branch/src/java/org/apache/log4j/net/SyslogAppender.java?view=diff&rev=510307&r1=510306&r2=510307
==============================================================================
--- logging/log4j/branches/v1_2-branch/src/java/org/apache/log4j/net/SyslogAppender.java (original)
+++ logging/log4j/branches/v1_2-branch/src/java/org/apache/log4j/net/SyslogAppender.java Wed
Feb 21 15:37:32 2007
@@ -261,12 +261,12 @@
     if (layout.ignoresThrowable()) {
       String[] s = event.getThrowableStrRep();
       if (s != null) {
-        int len = s.length;
-        if(len > 0) {
-          sqw.write(s[0]);
-          for(int i = 1; i < len; i++) {
-            sqw.write(TAB+s[i].substring(1));
-          }
+        for(int i = 0; i < s.length; i++) {
+            if (s[i].startsWith("\t")) {
+               sqw.write(TAB+s[i].substring(1));
+            } else {
+               sqw.write(s[i]);
+            }
         }
       }
     }

Modified: logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/net/SyslogAppenderTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/net/SyslogAppenderTest.java?view=diff&rev=510307&r1=510306&r2=510307
==============================================================================
--- logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/net/SyslogAppenderTest.java
(original)
+++ logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/net/SyslogAppenderTest.java
Wed Feb 21 15:37:32 2007
@@ -25,14 +25,10 @@
 import org.apache.log4j.Logger;
 import org.apache.log4j.PatternLayout;
 import org.apache.log4j.VectorErrorHandler;
-import org.apache.log4j.helpers.SyslogWriter;
 
 import java.util.StringTokenizer;
-import java.io.IOException;
 import java.net.DatagramSocket;
 import java.net.DatagramPacket;
-import java.net.InetAddress;
-import java.net.UnknownHostException;
 
 
 /**
@@ -391,8 +387,9 @@
       appender.setSyslogHost("127.0.0.1:1514");
   }
 
-
-    public void testActualLogging() throws Exception {
+    private static String[] log(final String msg,
+                                final Exception ex,
+                                final int packets) throws Exception {
         DatagramSocket ds = new DatagramSocket();
         ds.setSoTimeout(2000);
         DatagramPacket p = new DatagramPacket(new byte[1000], 0, 1000);
@@ -406,14 +403,63 @@
 
       Logger l = Logger.getRootLogger();
       l.addAppender(appender);
-      l.info("greetings");
+      if (ex == null) {
+        l.info(msg);
+      } else {
+        l.error(msg, ex);
+      }
       appender.close();
-      ds.receive(p);
+      String[] retval = new String[packets];
+      for(int i = 0; i < packets; i++) {
+          ds.receive(p);
+          retval[i] = new String(p.getData(), 0, p.getLength());
+      }
       ds.close();
-      String s = new String(p.getData(), 0, p.getLength());
+      return retval;
+    }
+
+    public void testActualLogging() throws Exception {
+      String s = log("greetings", null, 1)[0];
       StringTokenizer st = new StringTokenizer(s, "<>() ");
       assertEquals("14", st.nextToken());
       assertEquals("greetings", st.nextToken());
+    }
+
+    /**
+     * Exception with printStackTrace that breaks earlier SyslogAppender.
+     */
+    private static class MishandledException extends Exception {
+        /*
+         *   Create new instance.
+         */
+        public MishandledException() {
+        }
+
+        /**
+         * Print stack trace.
+         * @param w print writer, may not be null.
+         */
+        public void printStackTrace(final java.io.PrintWriter w) {
+             w.println("Mishandled stack trace follows:");
+             w.println("");
+             w.println("No tab here");
+             w.println("\ttab here");
+        }
+    }
+
+    /**
+     * Tests fix for bug 40502.
+     * @throws Exception on IOException.
+     */
+    public void testBadTabbing() throws Exception {
+        String[] s = log("greetings", new MishandledException(), 5);
+        StringTokenizer st = new StringTokenizer(s[0], "<>() ");
+        assertEquals("11", st.nextToken());
+        assertEquals("greetings", st.nextToken());
+        assertEquals("<11>Mishandled stack trace follows:", s[1]);
+        assertEquals("<11>", s[2]);
+        assertEquals("<11>No tab here", s[3]);
+        assertEquals("<11>" + SyslogAppender.TAB + "tab here", s[4]);
     }
 
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org


Mime
View raw message