logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r1342772 - in /logging/log4j/trunk: src/main/java/org/apache/log4j/net/SyslogAppender.java tests/src/java/org/apache/log4j/net/SyslogAppenderTest.java
Date Fri, 25 May 2012 18:58:21 GMT
Author: ggregory
Date: Fri May 25 18:58:20 2012
New Revision: 1342772

URL: http://svn.apache.org/viewvc?rev=1342772&view=rev
Log:
Bug 46626 - Log4J SyslogAppender does not handle the TAG field. https://issues.apache.org/bugzilla/show_bug.cgi?id=46626

Modified:
    logging/log4j/trunk/src/main/java/org/apache/log4j/net/SyslogAppender.java
    logging/log4j/trunk/tests/src/java/org/apache/log4j/net/SyslogAppenderTest.java

Modified: logging/log4j/trunk/src/main/java/org/apache/log4j/net/SyslogAppender.java
URL: http://svn.apache.org/viewvc/logging/log4j/trunk/src/main/java/org/apache/log4j/net/SyslogAppender.java?rev=1342772&r1=1342771&r2=1342772&view=diff
==============================================================================
--- logging/log4j/trunk/src/main/java/org/apache/log4j/net/SyslogAppender.java (original)
+++ logging/log4j/trunk/src/main/java/org/apache/log4j/net/SyslogAppender.java Fri May 25
18:58:20 2012
@@ -17,19 +17,20 @@
 
 package org.apache.log4j.net;
 
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Locale;
+import java.util.regex.Pattern;
+
 import org.apache.log4j.AppenderSkeleton;
 import org.apache.log4j.Layout;
 import org.apache.log4j.helpers.SyslogQuietWriter;
 import org.apache.log4j.helpers.SyslogWriter;
 import org.apache.log4j.spi.LoggingEvent;
 
-import java.text.SimpleDateFormat;
-import java.util.Date;
-import java.util.Locale;
-import java.net.InetAddress;
-import java.net.UnknownHostException;
-import java.io.IOException;
-
 // Contributors: Yves Bossel <ybossel@opengets.cl>
 //               Christopher Taylor <cstaylor@pacbell.net>
 
@@ -44,6 +45,11 @@ public class SyslogAppender extends Appe
   // copyrighted by the Regents of the University of California
   // I hope nobody at Berkley gets offended.
 
+  /**
+    * Maximum length of a TAG string.
+    */
+  private static final int MAX_TAG_LEN = 32;
+  
   /** Kernel messages */
   final static public int LOG_KERN     = 0;
   /** Random user-level messages */
@@ -93,6 +99,8 @@ public class SyslogAppender extends Appe
 
   static final String TAB = "    ";
 
+  static final Pattern NOT_ALPHANUM = Pattern.compile("[^\\p{Alnum}]");
+
   // Have LOG_USER as default
   int syslogFacility = LOG_USER;
   String facilityStr;
@@ -110,6 +118,13 @@ public class SyslogAppender extends Appe
   private boolean header = false;
   
     /**
+     * The TAG part of the syslog message.
+     * 
+     * @since 1.2.18
+     */
+  private String tag = null;
+  
+    /**
      * Date format used if header = true.
      * @since 1.2.15
      */
@@ -479,6 +494,49 @@ public class SyslogAppender extends Appe
   }
 
     /**
+     * Sets the <b>Tag</b> option.
+     * 
+     * <p>
+     * If non-{@code null}, the printed HEADER will include the specified tag followed by
a colon. If {@code null}, then no tag is printed.
+     * </p>
+     * <p>
+     * The default value is {@code null}.
+     * </p>
+     * 
+     * @param tag
+     *            the TAG to be printed out with the header
+     * @see #getTag()
+     * @since 1.2.18
+     */
+    public void setTag(final String tag) {
+        String newTag = tag;
+        if (newTag != null) {
+            if (newTag.length() > MAX_TAG_LEN) {
+                newTag = newTag.substring(0, MAX_TAG_LEN);
+            }
+            if (NOT_ALPHANUM.matcher(newTag).find()) {
+                throw new IllegalArgumentException("tag contains non-alphanumeric characters");
+            }
+        }
+
+        this.tag = newTag;
+    }
+
+    /**
+     * Gets the TAG to be printed with the HEADER portion of the log message. This will return
{@code null} if no TAG is to be printed.
+     * <p>
+     * The default value is {@code null}.
+     * </p>
+     * 
+     * @return the TAG, max length 32.
+     * @see #setTag(String)
+     * @since 1.2.18
+     */
+  public String getTag() {
+      return this.tag;
+  }
+
+    /**
      * Get the host name used to identify this appender.
      * @return local host name
      * @since 1.2.15
@@ -510,6 +568,10 @@ public class SyslogAppender extends Appe
         }
         buf.append(getLocalHostname());
         buf.append(' ');
+        if(this.tag != null) {
+            buf.append(this.tag);
+            buf.append(": ");
+        }
         return buf.toString();
       }
       return "";

Modified: logging/log4j/trunk/tests/src/java/org/apache/log4j/net/SyslogAppenderTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/trunk/tests/src/java/org/apache/log4j/net/SyslogAppenderTest.java?rev=1342772&r1=1342771&r2=1342772&view=diff
==============================================================================
--- logging/log4j/trunk/tests/src/java/org/apache/log4j/net/SyslogAppenderTest.java (original)
+++ logging/log4j/trunk/tests/src/java/org/apache/log4j/net/SyslogAppenderTest.java Fri May
25 18:58:20 2012
@@ -67,6 +67,7 @@ public class SyslogAppenderTest extends 
     assertEquals(false, appender.getFacilityPrinting());
     assertNull(appender.getLayout());
     assertNull(appender.getSyslogHost());
+    assertNull(appender.getTag());
     assertTrue(appender.requiresLayout());
   }
 
@@ -80,6 +81,7 @@ public class SyslogAppenderTest extends 
     assertEquals(false, appender.getFacilityPrinting());
     assertEquals(layout, appender.getLayout());
     assertNull(appender.getSyslogHost());
+    assertNull(appender.getTag());
     assertTrue(appender.requiresLayout());
   }
 
@@ -93,6 +95,7 @@ public class SyslogAppenderTest extends 
     assertEquals(false, appender.getFacilityPrinting());
     assertEquals(layout, appender.getLayout());
     assertNull(appender.getSyslogHost());
+    assertNull(appender.getTag());
     assertTrue(appender.requiresLayout());
   }
 
@@ -107,6 +110,7 @@ public class SyslogAppenderTest extends 
     assertEquals(false, appender.getFacilityPrinting());
     assertEquals(layout, appender.getLayout());
     assertEquals("syslog.example.org", appender.getSyslogHost());
+    assertNull(appender.getTag());
     assertTrue(appender.requiresLayout());
   }
 
@@ -393,7 +397,48 @@ public class SyslogAppenderTest extends 
       appender.setSyslogHost("127.0.0.1:1514");
   }
 
+  /**
+    *  Tests SyslogAppender with setTag.
+    */
+  public void testTag() {
+      SyslogAppender appender = new SyslogAppender();
+      appender.setTag("testtag");
+      assertEquals("testtag", appender.getTag());
+  }
+
+  /**
+    *  Tests SyslogAppender with null tag.
+    */
+  public void testNullTag() {
+      SyslogAppender appender = new SyslogAppender();
+      appender.setTag(null);
+      assertNull(appender.getTag());
+  }
+
+  /**
+    *  Tests SyslogAppender with long tag.
+    */
+  public void testLongTag() {
+      SyslogAppender appender = new SyslogAppender();
+      appender.setTag("testtagtesttagtesttagtesttagtesttag");
+      assertEquals(appender.getTag(), "testtagtesttagtesttagtesttagtest");
+  }
+
+  /**
+    *  Tests SyslogAppender with non alnum tag.
+    */
+  public void testNonAlnumTag() {
+      SyslogAppender appender = new SyslogAppender();
+      try{
+          appender.setTag("testtag testtag");
+          fail("SyslogAppender.setTag() should have thrown an exception.");
+      } catch (IllegalArgumentException e) {
+          // Correct behavior, ignore and let the test pass.
+      }
+  }
+
     private static String[] log(final boolean header,
+                                final String tag,
                                 final String msg,
                                 final Exception ex,
                                 final int packets) throws Exception {
@@ -404,6 +449,7 @@ public class SyslogAppenderTest extends 
       appender.setSyslogHost("localhost:" + ds.getLocalPort());
       appender.setName("name");
       appender.setHeader(header);
+      appender.setTag(tag);
       PatternLayout pl = new PatternLayout("%m");
       appender.setLayout(pl);
       appender.activateOptions();
@@ -428,7 +474,7 @@ public class SyslogAppenderTest extends 
     }
 
     public void testActualLogging() throws Exception {
-      String s = log(false, "greetings", null, 1)[0];
+      String s = log(false, null, "greetings", null, 1)[0];
       StringTokenizer st = new StringTokenizer(s, "<>() ");
       assertEquals("14", st.nextToken());
       assertEquals("greetings", st.nextToken());
@@ -463,7 +509,7 @@ public class SyslogAppenderTest extends 
      * @throws Exception on IOException.
      */
     public void testBadTabbing() throws Exception {
-        String[] s = log(false, "greetings", new MishandledException(), 6);
+        String[] s = log(false, null, "greetings", new MishandledException(), 6);
         StringTokenizer st = new StringTokenizer(s[0], "<>() ");
         assertEquals("11", st.nextToken());
         assertEquals("greetings", st.nextToken());
@@ -481,7 +527,7 @@ public class SyslogAppenderTest extends 
      */
     public void testHeaderLogging() throws Exception {
       Date preDate = new Date();
-      String s = log(true, "greetings", null, 1)[0];
+      String s = log(true, null, "greetings", null, 1)[0];
       Date postDate = new Date();
       assertEquals("<14>", s.substring(0, 4));
 
@@ -515,6 +561,51 @@ public class SyslogAppenderTest extends 
 
 
     /**
+     * Tests presence of tag if set
+     */
+    public void testHeaderTagLogging() throws Exception {
+      String s = log(true, "testtag", "greetings", null, 1)[0];
+      assertEquals("<14>", s.substring(0, 4));
+
+      StringTokenizer st = new StringTokenizer(s.substring(21), " ");
+
+      // Throw away the hostname
+      st.nextToken();
+
+      assertEquals("testtag:", st.nextToken());
+    }
+
+    /**
+     * Tests presence of tag on every line of the exception
+     */
+    public void testHeaderTagExceptionLogging() throws Exception {
+      String[] s = log(true, "testtag", "greetings", new Exception(), 6);
+      for(int i=0; i < s.length; i++) {
+          System.err.println(s[i]);
+          assertEquals("<11>", s[i].substring(0, 4));
+          StringTokenizer st = new StringTokenizer(s[i].substring(21), " ");
+          // Throw away the hostname
+          st.nextToken();
+          assertEquals("testtag:", st.nextToken());
+      }
+    }
+
+    /**
+     * Tests absesence of tag if set to null
+     */
+    public void testHeaderNullTagLogging() throws Exception {
+      String s = log(true, null, "greetings", null, 1)[0];
+      assertEquals("<14>", s.substring(0, 4));
+
+      StringTokenizer st = new StringTokenizer(s.substring(21), " ");
+
+      // Throw away the hostname
+      st.nextToken();
+
+      assertEquals("greetings", st.nextToken());
+    }
+
+    /**
      * Tests that any header or footer in layout is sent.
      * @throws Exception if exception during test.
      */



Mime
View raw message