logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From carn...@apache.org
Subject svn commit: r923055 - in /logging/log4j/companions/extras/trunk/src: changes/changes.xml main/java/org/apache/log4j/LogMF.java test/java/org/apache/log4j/TestLogMF.java
Date Mon, 15 Mar 2010 04:44:19 GMT
Author: carnold
Date: Mon Mar 15 04:44:18 2010
New Revision: 923055

URL: http://svn.apache.org/viewvc?rev=923055&view=rev
Log:
Bug 48911: LogMF did not optimize simple patterns on multiple parameter log requests

Modified:
    logging/log4j/companions/extras/trunk/src/changes/changes.xml
    logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/LogMF.java
    logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/TestLogMF.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=923055&r1=923054&r2=923055&view=diff
==============================================================================
--- logging/log4j/companions/extras/trunk/src/changes/changes.xml (original)
+++ logging/log4j/companions/extras/trunk/src/changes/changes.xml Mon Mar 15 04:44:18 2010
@@ -30,6 +30,7 @@
        <action action="fix" issue="48531">Unit tests fail for system dates after 2009-12-31</action>
        <action action="fix" issue="48778">LogMF performance improvement for number
and date types.</action>
        <action action="fix" issue="48910">LogSF did not support \\{ escape sequence.</action>
+       <action action="fix" issue="48911">LogMF did not optimize simple patterns on
multiple parameter log requests.</action>
     </release>
 
 

Modified: logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/LogMF.java
URL: http://svn.apache.org/viewvc/logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/LogMF.java?rev=923055&r1=923054&r2=923055&view=diff
==============================================================================
--- logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/LogMF.java (original)
+++ logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/LogMF.java Mon Mar
15 04:44:18 2010
@@ -88,61 +88,48 @@ public final class LogMF extends LogXF {
         return dateFormat.format(d);
     }
 
-
     /**
-     * Formats arguments using a minimal subset
-     * of MessageFormat syntax.
-     * @param pattern pattern, may not be null.
-     * @param arg0 argument, may be null.
-     * @return Message string or null if pattern
-     * is not supported.
-     */
-    private static String subsetFormat(final String pattern,
-                                       final Object arg0) {
-        if (pattern != null) {
-            //
-            //  find position of first brace
-            //    if none then format is a literal
-            int bracePos = pattern.indexOf("{");
-
-            //
-            //  if the first format is {0}
-            //    and there are no other format specifiers
-            //    and no quotes then substitute for {0}
-            if (bracePos != -1) {
-                if ((pattern.indexOf("{0}", bracePos) == bracePos)
-                        && (pattern.indexOf("{", bracePos + 1) == -1)
-                        && (pattern.indexOf("'") == -1)) {
-                    String replacement;
-
-                    if (arg0 instanceof String) {
-                        replacement = arg0.toString();
-                    } else if (arg0 instanceof Double ||
-                               arg0 instanceof Float) {
-                       replacement = formatNumber(arg0);
-                    } else if (arg0 instanceof Date) {
-                        replacement = formatDate(arg0);
-                    } else {
-                        replacement = String.valueOf(arg0);
-                    }
+     * Format a single parameter like a "{0}" formatting specifier.
+     *
+     * @param arg0 parameter, may be null.
+     * @return string representation of arg0.
+     */
+    private static String formatObject(final Object arg0) {
+        if (arg0 instanceof String) {
+            return arg0.toString();
+        } else if (arg0 instanceof Double ||
+                   arg0 instanceof Float) {
+           return formatNumber(arg0);
+        } else if (arg0 instanceof Date) {
+            return formatDate(arg0);
+        }
+        return String.valueOf(arg0);
+    }
 
-                    final StringBuffer buf = new StringBuffer(pattern);
-                    buf.replace(bracePos,
-                            bracePos + "{0}".length(), replacement);
 
-                    return buf.toString();
-                }
-            } else {
-                //
-                //   pattern is a literal with no braces
-                //    and not quotes, return pattern.
-                if (pattern.indexOf("'") == -1) {
-                    return pattern;
-                }
+    /**
+     * Determines if pattern contains only {n} format elements
+     * and not apostrophes.
+     *
+     * @param pattern pattern, may not be null.
+     * @return true if pattern only contains {n} format elements.
+     */
+    private static boolean isSimple(final String pattern) {
+        if (pattern.indexOf('\'') != -1) {
+            return false;
+        }
+        for(int pos = pattern.indexOf('{');
+            pos != -1;
+            pos = pattern.indexOf('{', pos + 1)) {
+            if (pos + 2 >= pattern.length() ||
+                    pattern.charAt(pos+2) != '}' ||
+                    pattern.charAt(pos+1) < '0' ||
+                    pattern.charAt(pos+1) > '9') {
+                return false;
             }
         }
+        return true;
 
-        return null;
     }
 
     /**
@@ -155,6 +142,34 @@ public final class LogMF extends LogXF {
                                  final Object[] arguments) {
         if (pattern == null) {
             return null;
+        } else if(isSimple(pattern)) {
+            String formatted[] = new String[10];
+            int prev = 0;
+            String retval = "";
+            int pos = pattern.indexOf('{');
+            while(pos >= 0) {
+                if(pos + 2 < pattern.length() && 
+                      pattern.charAt(pos+2) == '}' &&
+                      pattern.charAt(pos+1) >= '0' &&
+                      pattern.charAt(pos+1) <= '9') {
+                    int index = pattern.charAt(pos+1) - '0';
+                    retval += pattern.substring(prev, pos);
+                    if (formatted[index] == null) {
+                         if (arguments == null || index >= arguments.length) {
+                            formatted[index] = pattern.substring(pos, pos+3);
+                         } else {
+                            formatted[index] = formatObject(arguments[index]);
+                         }
+                    }
+                    retval += formatted[index];
+                    prev = pos + 3;
+                    pos = pattern.indexOf('{', prev);
+                } else {
+                    pos = pattern.indexOf('{', pos + 1);
+                }
+            }
+            retval += pattern.substring(prev);
+            return retval;
         }
         try {
             return MessageFormat.format(pattern, arguments);
@@ -164,21 +179,52 @@ public final class LogMF extends LogXF {
     }
 
     /**
-     * Formats arguments using MessageFormat.
-     * @param pattern pattern, may be malformed.
-     * @param arg0 argument, may be null or mismatched.
-     * @return Message string
+     * Formats a single argument using MessageFormat.
+     * @param pattern pattern, may be malformed or null.
+     * @param arguments arguments, may be null or mismatched.
+     * @return Message string or null
      */
-    private static String format(final String pattern, final Object arg0) {
-        String msg = subsetFormat(pattern, arg0);
-
-        if (msg == null) {
-            msg = format(pattern, toArray(arg0));
+    private static String format(final String pattern,
+                                 final Object arg0) {
+        if (pattern == null) {
+            return null;
+        } else if(isSimple(pattern)) {
+            String formatted = null;
+            int prev = 0;
+            String retval = "";
+            int pos = pattern.indexOf('{');
+            while(pos >= 0) {
+                if(pos + 2 < pattern.length() &&
+                      pattern.charAt(pos+2) == '}' &&
+                      pattern.charAt(pos+1) >= '0' &&
+                      pattern.charAt(pos+1) <= '9') {
+                    int index = pattern.charAt(pos+1) - '0';
+                    retval += pattern.substring(prev, pos);
+                    if (index != 0) {
+                        retval += pattern.substring(pos, pos+3);
+                    } else {
+                        if (formatted == null) {
+                            formatted = formatObject(arg0);
+                        }
+                        retval += formatted;
+                    }
+                    prev = pos + 3;
+                    pos = pattern.indexOf('{', prev);
+                } else {
+                    pos = pattern.indexOf('{', pos + 1);
+                }
+            }
+            retval += pattern.substring(prev);
+            return retval;
+        }
+        try {
+            return MessageFormat.format(pattern, new Object[] { arg0 });
+        } catch (IllegalArgumentException ex) {
+            return pattern;
         }
-
-        return msg;
     }
 
+
     /**
      * Formats arguments using MessageFormat using a pattern from
      * a resource bundle.

Modified: logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/TestLogMF.java
URL: http://svn.apache.org/viewvc/logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/TestLogMF.java?rev=923055&r1=923054&r2=923055&view=diff
==============================================================================
--- logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/TestLogMF.java (original)
+++ logging/log4j/companions/extras/trunk/src/test/java/org/apache/log4j/TestLogMF.java Mon
Mar 15 04:44:18 2010
@@ -1236,4 +1236,45 @@ public class TestLogMF extends TestCase 
         assertEquals("Hello, World.", capture.getMessage());
     }
 
+
+    /**
+     * Test LogMF.info with a pattern containing {9} and one argument.
+     */
+    public void testInfo1ParamBrace9() {
+        LogCapture capture = new LogCapture(Level.INFO);
+        LogMF.info(logger, "Hello, {9}{0}", "World");
+        assertEquals("Hello, {9}World", capture.getMessage());
+    }
+
+    /**
+     * Test LogMF.info with a pattern containing {9} and two arguments.
+     */
+    public void testInfo2ParamBrace9() {
+        LogCapture capture = new LogCapture(Level.INFO);
+        LogMF.info(logger, "{1}, {9}{0}", "World", "Hello");
+        assertEquals("Hello, {9}World", capture.getMessage());
+    }
+
+    /**
+     * Test LogMF.info with a pattern containing {9} and two arguments.
+     */
+    public void testInfo10ParamBrace9() {
+        LogCapture capture = new LogCapture(Level.INFO);
+        LogMF.info(logger, "{1}, {9}{0}",
+                new Object[] { "World", "Hello", null, null, null,
+                                null, null, null, null, "New " });
+        assertEquals("Hello, New World", capture.getMessage());
+    }
+
+    /**
+     * Test LogMF.info with indexes just outside of 0 to 9.
+     */
+    public void testInfo1ParamBraceSlashColon() {
+        LogCapture capture = new LogCapture(Level.INFO);
+        String pattern = "Hello, {/}{0}{:}";
+        LogMF.info(logger, pattern, "World");
+        assertEquals(pattern, capture.getMessage());
+    }
+
+
 }



---------------------------------------------------------------------
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