You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by ca...@apache.org on 2010/10/29 05:37:29 UTC

svn commit: r1028600 - in /logging/log4j/companions/extras/trunk/src: changes/ main/java/org/apache/log4j/ main/java/org/apache/log4j/pattern/ test/java/org/apache/log4j/ test/java/org/apache/log4j/pattern/ test/resources/org/apache/log4j/

Author: carnold
Date: Fri Oct 29 03:37:28 2010
New Revision: 1028600

URL: http://svn.apache.org/viewvc?rev=1028600&view=rev
Log:
Bug 50039: Add right truncation modifier to EnhancedPatternLayout

Added:
    logging/log4j/companions/extras/trunk/src/test/resources/org/apache/log4j/enhancedPatternLayout17.properties
      - copied, changed from r1028174, logging/log4j/companions/extras/trunk/src/test/resources/org/apache/log4j/enhancedPatternLayout16.properties
Modified:
    logging/log4j/companions/extras/trunk/src/changes/changes.xml
    logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/EnhancedPatternLayout.java
    logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/pattern/FormattingInfo.java
    logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/pattern/PatternParser.java
    logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/EnhancedPatternLayoutTestCase.java
    logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/pattern/FormattingInfoTest.java

Modified: logging/log4j/companions/extras/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/companions/extras/trunk/src/changes/changes.xml?rev=1028600&r1=1028599&r2=1028600&view=diff
==============================================================================
--- logging/log4j/companions/extras/trunk/src/changes/changes.xml (original)
+++ logging/log4j/companions/extras/trunk/src/changes/changes.xml Fri Oct 29 03:37:28 2010
@@ -40,6 +40,7 @@
        <action action="add" issue="43282">Add OSGi packaging info.</action>
        <action issue="48588" action="fix">DOMConfigurator does not close input stream when configured based on URL.</action>
        <action issue="49078" action="fix">javadoc.jar was missing NOTICE and LICENSE.</action>
+       <action issue="50039" action="add">Add right truncation modifier to EnhancedPatternLayout.</action>
     </release>
 
 

Modified: logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/EnhancedPatternLayout.java
URL: http://svn.apache.org/viewvc/logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/EnhancedPatternLayout.java?rev=1028600&r1=1028599&r2=1028600&view=diff
==============================================================================
--- logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/EnhancedPatternLayout.java (original)
+++ logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/EnhancedPatternLayout.java Fri Oct 29 03:37:28 2010
@@ -309,13 +309,15 @@ import org.apache.log4j.spi.LoggingEvent
 
    <p>By default the relevant information is output as is. However,
    with the aid of format modifiers it is possible to change the
-   minimum field width, the maximum field width and justification.
+   minimum field width, the maximum field width, justification
+   and truncation.
 
-   <p>The optional format modifier is placed between the percent sign
+   <p>The optional format modifiers are placed between the percent sign
    and the conversion character.
 
-   <p>The first optional format modifier is the <em>left justification
-   flag</em> which is just the minus (-) character. Then comes the
+   <p>The <em>left justification flag</em>, the minus sign (-),
+    the <em>right truncation flag</em>, the exclamation mark (!),
+    or any combination appear first.  Followed by the
    optional <em>minimum field width</em> modifier. This is a decimal
    constant that represents the minimum number of characters to
    output. If the data item requires fewer characters, it is padded on
@@ -334,7 +336,8 @@ import org.apache.log4j.spi.LoggingEvent
    example, it the maximum field width is eight and the data item is
    ten characters long, then the first two characters of the data item
    are dropped. This behavior deviates from the printf function in C
-   where truncation is done from the end.
+   where truncation is done from the end.  The <em>right truncation flag</em>,
+   described previously, will override this behavior.
 
    <p>Below are various format modifier examples for the category
    conversion specifier.
@@ -370,6 +373,15 @@ import org.apache.log4j.spi.LoggingEvent
    characters.
 
    <tr>
+   <td align=center>%!.30c</td>
+   <td align=center>NA</td>
+   <td align=center>none</td>
+   <td align=center>30</td>
+
+   <td>Truncate from the end if the category name is longer than 30
+   characters.
+
+   <tr>
    <td align=center>%20.30c</td>
    <td align=center>false</td>
    <td align=center>20</td>

Modified: logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/pattern/FormattingInfo.java
URL: http://svn.apache.org/viewvc/logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/pattern/FormattingInfo.java?rev=1028600&r1=1028599&r2=1028600&view=diff
==============================================================================
--- logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/pattern/FormattingInfo.java (original)
+++ logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/pattern/FormattingInfo.java Fri Oct 29 03:37:28 2010
@@ -57,16 +57,45 @@ public final class FormattingInfo {
   private final boolean leftAlign;
 
   /**
+   * Right truncation.
+   * @since 1.2.17
+   */
+  private final boolean rightTruncate;
+
+  /**
    * Creates new instance.
    * @param leftAlign left align if true.
    * @param minLength minimum length.
    * @param maxLength maximum length.
+   * @deprecated since 1.2.17
    */
   public FormattingInfo(
-    final boolean leftAlign, final int minLength, final int maxLength) {
+    final boolean leftAlign, 
+    final int minLength, 
+    final int maxLength) {
     this.leftAlign = leftAlign;
     this.minLength = minLength;
     this.maxLength = maxLength;
+    this.rightTruncate = false;
+  }
+
+  /**
+   * Creates new instance.
+   * @param leftAlign left align if true.
+   * @param rightTruncate right truncate if true.
+   * @param minLength minimum length.
+   * @param maxLength maximum length.
+   * @since 1.2.17
+   */
+  public FormattingInfo(
+    final boolean leftAlign, 
+    final boolean rightTruncate,
+    final int minLength, 
+    final int maxLength) {
+    this.leftAlign = leftAlign;
+    this.minLength = minLength;
+    this.maxLength = maxLength;
+    this.rightTruncate = rightTruncate;
   }
 
   /**
@@ -86,6 +115,15 @@ public final class FormattingInfo {
   }
 
   /**
+   * Determine if right truncated.
+   * @return true if right truncated.
+   * @since 1.2.17
+   */
+  public boolean isRightTruncated() {
+    return rightTruncate;
+  }
+
+  /**
    * Get minimum length.
    * @return minimum length.
    */
@@ -111,7 +149,11 @@ public final class FormattingInfo {
     final int rawLength = buffer.length() - fieldStart;
 
     if (rawLength > maxLength) {
-      buffer.delete(fieldStart, buffer.length() - maxLength);
+      if(rightTruncate) {
+         buffer.setLength(fieldStart + maxLength);
+      } else {
+         buffer.delete(fieldStart, buffer.length() - maxLength);
+      }
     } else if (rawLength < minLength) {
       if (leftAlign) {
         final int fieldEnd = buffer.length();

Modified: logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/pattern/PatternParser.java
URL: http://svn.apache.org/viewvc/logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/pattern/PatternParser.java?rev=1028600&r1=1028599&r2=1028600&view=diff
==============================================================================
--- logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/pattern/PatternParser.java (original)
+++ logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/pattern/PatternParser.java Fri Oct 29 03:37:28 2010
@@ -304,11 +304,22 @@ public final class PatternParser {
         case '-':
           formattingInfo =
             new FormattingInfo(
-              true, formattingInfo.getMinLength(),
+              true, 
+              formattingInfo.isRightTruncated(),
+              formattingInfo.getMinLength(),
               formattingInfo.getMaxLength());
+          break;
 
+        case '!':
+          formattingInfo =
+            new FormattingInfo(
+              formattingInfo.isLeftAligned(), 
+              true,
+              formattingInfo.getMinLength(),
+              formattingInfo.getMaxLength());
           break;
 
+
         case '.':
           state = DOT_STATE;
 
@@ -319,7 +330,9 @@ public final class PatternParser {
           if ((c >= '0') && (c <= '9')) {
             formattingInfo =
               new FormattingInfo(
-                formattingInfo.isLeftAligned(), c - '0',
+                formattingInfo.isLeftAligned(), 
+                formattingInfo.isRightTruncated(),
+                c - '0',
                 formattingInfo.getMaxLength());
             state = MIN_STATE;
           } else {
@@ -343,6 +356,7 @@ public final class PatternParser {
           formattingInfo =
             new FormattingInfo(
               formattingInfo.isLeftAligned(),
+              formattingInfo.isRightTruncated(),
               (formattingInfo.getMinLength() * 10) + (c - '0'),
               formattingInfo.getMaxLength());
         } else if (c == '.') {
@@ -364,7 +378,9 @@ public final class PatternParser {
         if ((c >= '0') && (c <= '9')) {
           formattingInfo =
             new FormattingInfo(
-              formattingInfo.isLeftAligned(), formattingInfo.getMinLength(),
+              formattingInfo.isLeftAligned(), 
+              formattingInfo.isRightTruncated(),
+              formattingInfo.getMinLength(),
               c - '0');
           state = MAX_STATE;
         } else {
@@ -383,7 +399,9 @@ public final class PatternParser {
         if ((c >= '0') && (c <= '9')) {
           formattingInfo =
             new FormattingInfo(
-              formattingInfo.isLeftAligned(), formattingInfo.getMinLength(),
+              formattingInfo.isLeftAligned(), 
+              formattingInfo.isRightTruncated(),
+              formattingInfo.getMinLength(),
               (formattingInfo.getMaxLength() * 10) + (c - '0'));
         } else {
           i = finalizeConverter(

Modified: logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/EnhancedPatternLayoutTestCase.java
URL: http://svn.apache.org/viewvc/logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/EnhancedPatternLayoutTestCase.java?rev=1028600&r1=1028599&r2=1028600&view=diff
==============================================================================
--- logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/EnhancedPatternLayoutTestCase.java (original)
+++ logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/EnhancedPatternLayoutTestCase.java Fri Oct 29 03:37:28 2010
@@ -416,6 +416,19 @@ public class EnhancedPatternLayoutTestCa
       assertTrue(cstDate.getTime() >= start - 1000 && cstDate.getTime() < end + 1000);
     }
 
+  /**
+   *   Test left and right truncation
+   */
+  public void test17() throws Exception {
+    configure("input/pattern/enhancedPatternLayout17.properties");
+    root.info("012");
+    root.info("");
+    root.info("12345");
+    root.info("0123456789");
+    assertTrue(compare("patternLayout17.log", "witness/pattern/enhancedPatternLayout.17"));
+  }
+
+
   void common() {
     int i = -1;
 

Modified: logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/pattern/FormattingInfoTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/pattern/FormattingInfoTest.java?rev=1028600&r1=1028599&r2=1028600&view=diff
==============================================================================
--- logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/pattern/FormattingInfoTest.java (original)
+++ logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/pattern/FormattingInfoTest.java Fri Oct 29 03:37:28 2010
@@ -53,7 +53,7 @@ public class FormattingInfoTest extends 
    *
    */
   public void testConstructor() {
-      FormattingInfo field = new FormattingInfo(true, 3, 6);
+      FormattingInfo field = new FormattingInfo(true, false, 3, 6);
       assertNotNull(field);
       assertEquals(3, field.getMinLength());
       assertEquals(6, field.getMaxLength());
@@ -65,17 +65,27 @@ public class FormattingInfoTest extends 
    */
   public void testTruncate() {
       StringBuffer buf = new StringBuffer("foobar");
-      FormattingInfo field = new FormattingInfo(true, 0, 3);
+      FormattingInfo field = new FormattingInfo(true, false, 0, 3);
       field.format(2, buf);
       assertEquals("fobar", buf.toString());
   }
 
+  /**
+   * Field exceeds maximum width, truncate right.
+   */
+  public void testRightTruncate() {
+      StringBuffer buf = new StringBuffer("foobar");
+      FormattingInfo field = new FormattingInfo(true, true, 0, 3);
+      field.format(2, buf);
+      assertEquals("fooba", buf.toString());
+  }
+
     /**
      * Add padding to left since field is not minimum width.
      */
     public void testPadLeft() {
         StringBuffer buf = new StringBuffer("foobar");
-        FormattingInfo field = new FormattingInfo(false, 5, 10);
+        FormattingInfo field = new FormattingInfo(false, false, 5, 10);
         field.format(2, buf);
         assertEquals("fo obar", buf.toString());
     }
@@ -85,7 +95,7 @@ public class FormattingInfoTest extends 
      */
     public void testPadRight() {
         StringBuffer buf = new StringBuffer("foobar");
-        FormattingInfo field = new FormattingInfo(true, 5, 10);
+        FormattingInfo field = new FormattingInfo(true, false, 5, 10);
         field.format(2, buf);
         assertEquals("foobar ", buf.toString());
     }

Copied: logging/log4j/companions/extras/trunk/src/test/resources/org/apache/log4j/enhancedPatternLayout17.properties (from r1028174, logging/log4j/companions/extras/trunk/src/test/resources/org/apache/log4j/enhancedPatternLayout16.properties)
URL: http://svn.apache.org/viewvc/logging/log4j/companions/extras/trunk/src/test/resources/org/apache/log4j/enhancedPatternLayout17.properties?p2=logging/log4j/companions/extras/trunk/src/test/resources/org/apache/log4j/enhancedPatternLayout17.properties&p1=logging/log4j/companions/extras/trunk/src/test/resources/org/apache/log4j/enhancedPatternLayout16.properties&r1=1028174&r2=1028600&rev=1028600&view=diff
==============================================================================
--- logging/log4j/companions/extras/trunk/src/test/resources/org/apache/log4j/enhancedPatternLayout16.properties (original)
+++ logging/log4j/companions/extras/trunk/src/test/resources/org/apache/log4j/enhancedPatternLayout17.properties Fri Oct 29 03:37:28 2010
@@ -15,10 +15,10 @@
 #
 log4j.rootCategory=TRACE, testAppender
 log4j.appender.testAppender=org.apache.log4j.FileAppender
-log4j.appender.testAppender.File=patternLayout16.log
+log4j.appender.testAppender.File=patternLayout17.log
 log4j.appender.testAppender.Append=false
 log4j.appender.testAppender.layout=org.apache.log4j.EnhancedPatternLayout
-log4j.appender.testAppender.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss}{GMT}Z %d{yyyy-MM-dd HH:mm:ss}{GMT-6}-0600 - %m%n
+log4j.appender.testAppender.layout.ConversionPattern=%!.2p - %m %4.5m %-4.5m %!4.5m %!-4.5m %-!4.5m%n
 
 #  Prevent internal log4j DEBUG messages from polluting the output.
 log4j.logger.org.apache.log4j.PropertyConfigurator=INFO



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