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/03/14 01:50:35 UTC

svn commit: r922699 - in /logging/log4j/companions/extras/trunk/src: changes/changes.xml main/java/org/apache/log4j/LogMF.java

Author: carnold
Date: Sun Mar 14 00:50:35 2010
New Revision: 922699

URL: http://svn.apache.org/viewvc?rev=922699&view=rev
Log:
Bug 48778: LogMF performance improvement for Number and Date types

Modified:
    logging/log4j/companions/extras/trunk/src/changes/changes.xml
    logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/LogMF.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=922699&r1=922698&r2=922699&view=diff
==============================================================================
--- logging/log4j/companions/extras/trunk/src/changes/changes.xml (original)
+++ logging/log4j/companions/extras/trunk/src/changes/changes.xml Sun Mar 14 00:50:35 2010
@@ -28,6 +28,7 @@
        <action action="fix" issue="46741">Misuse of "it's" in Javadoc for EnhancedPatternLayout.</action>
        <action action="fix" issue="46741">Leaving out %throwable in ConversionPattern adds throwable to logging message regardless.</action>
        <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>
     </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=922699&r1=922698&r2=922699&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 Sun Mar 14 00:50:35 2010
@@ -23,6 +23,7 @@ import java.text.MessageFormat;
 import java.text.NumberFormat;
 import java.util.Date;
 import java.util.ResourceBundle;
+import java.util.Locale;
 
 
 /**
@@ -38,6 +39,55 @@ public final class LogMF extends LogXF {
     private LogMF() {
     }
 
+    /**
+     * Number format.
+     */
+    private static NumberFormat numberFormat = null;
+    /**
+     * Locale at time of last number format request.
+     */
+    private static Locale numberLocale = null;
+    /**
+     * Date format.
+     */
+    private static DateFormat dateFormat = null;
+    /**
+     * Locale at time of last date format request.
+     */
+    private static Locale dateLocale = null;
+
+    /**
+     * Format number.
+     * @param n number to format, may not be null.
+     * @return formatted value.
+     */
+    private static synchronized String formatNumber(final Object n) {
+        Locale currentLocale = Locale.getDefault();
+        if (currentLocale != numberLocale || numberFormat == null) {
+            numberLocale = currentLocale;
+            numberFormat = NumberFormat.getInstance(currentLocale);
+        }
+        return numberFormat.format(n);
+    }
+
+
+    /**
+     * Format date.
+     * @param d date, may not be null.
+     * @return formatted value.
+     */
+    private static synchronized String formatDate(final Object d) {
+        Locale currentLocale = Locale.getDefault();
+        if (currentLocale != dateLocale || dateFormat == null) {
+            dateLocale = currentLocale;
+            dateFormat = DateFormat.getDateTimeInstance(
+                                DateFormat.SHORT,
+                                DateFormat.SHORT,
+                                currentLocale);
+        }
+        return dateFormat.format(d);
+    }
+
 
     /**
      * Formats arguments using a minimal subset
@@ -67,12 +117,11 @@ public final class LogMF extends LogXF {
 
                     if (arg0 instanceof String) {
                         replacement = arg0.toString();
-                    } else if (arg0 instanceof Number) {
-                        replacement = NumberFormat.getInstance().format(arg0);
+                    } else if (arg0 instanceof Double ||
+                               arg0 instanceof Float) {
+                       replacement = formatNumber(arg0);
                     } else if (arg0 instanceof Date) {
-                        replacement = DateFormat.getDateTimeInstance(
-                                DateFormat.SHORT,
-                                DateFormat.SHORT).format(arg0);
+                        replacement = formatDate(arg0);
                     } else {
                         replacement = String.valueOf(arg0);
                     }



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