[jboss-svn-commits] JBoss Common SVN: r3222 - jboss-logmanager/trunk/src/main/java/org/jboss/logmanager.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu Jun 4 19:54:42 EDT 2009


Author: david.lloyd at jboss.com
Date: 2009-06-04 19:54:42 -0400 (Thu, 04 Jun 2009)
New Revision: 3222

Modified:
   jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/ExtLogRecord.java
Log:
Fix caching issue for formatted records; add printf formatting capability to records

Modified: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/ExtLogRecord.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/ExtLogRecord.java	2009-06-04 21:26:43 UTC (rev 3221)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/ExtLogRecord.java	2009-06-04 23:54:42 UTC (rev 3222)
@@ -40,6 +40,21 @@
     private static final long serialVersionUID = -9174374711278052369L;
 
     /**
+     * The format style to use.
+     */
+    public enum FormatStyle {
+
+        /**
+         * Format the message using the {@link java.text.MessageFormat} parameter style.
+         */
+        MESSAGE_FORMAT,
+        /**
+         * Format the message using the {@link java.util.Formatter} (also known as {@code printf()}) parameter style.
+         */
+        PRINTF,
+    }
+
+    /**
      * Construct a new instance.  Grabs the current NDC immediately.  MDC is deferred.
      *
      * @param level a logging level value
@@ -55,6 +70,23 @@
     }
 
     /**
+     * Construct a new instance.  Grabs the current NDC immediately.  MDC is deferred.
+     *
+     * @param level a logging level value
+     * @param msg the raw non-localized logging message (may be null)
+     * @param formatStyle the parameter format style to use
+     * @param loggerClassName the name of the logger class
+     */
+    public ExtLogRecord(final java.util.logging.Level level, final String msg, final FormatStyle formatStyle, final String loggerClassName) {
+        super(level, msg);
+        this.formatStyle = formatStyle == null ? FormatStyle.MESSAGE_FORMAT : formatStyle;
+        this.loggerClassName = loggerClassName;
+        ndc = NDC.get();
+        setUnknownCaller();
+        threadName = Thread.currentThread().getName();
+    }
+
+    /**
      * Construct a new instance by copying an original record.
      *
      * @param original the record to copy
@@ -81,6 +113,7 @@
     private transient final String loggerClassName;
     private transient boolean calculateCaller = true;
 
+    private FormatStyle formatStyle = FormatStyle.MESSAGE_FORMAT;
     private Map<String, String> mdcCopy;
     private int sourceLineNumber = -1;
     private String sourceFileName;
@@ -247,6 +280,11 @@
         super.setSourceMethodName(sourceMethodName);
     }
 
+    /**
+     * Get the fully formatted log record, with resources resolved and parameters applied.
+     *
+     * @return the formatted log record
+     */
     public String getFormattedMessage() {
         if (formattedMessage == null) {
             formattedMessage = formatRecord();
@@ -265,9 +303,19 @@
             }
         }
         final Object[] parameters = getParameters();
-        return parameters != null &&
-                parameters.length > 0 &&
-                msg.indexOf('{') >= 0 ? MessageFormat.format(msg, parameters) : msg;
+        if (parameters == null || parameters.length == 0) {
+            return msg;
+        }
+        switch (formatStyle) {
+            case PRINTF: {
+                return String.format(msg, parameters);
+            }
+            case MESSAGE_FORMAT: {
+                return msg.indexOf('{') >= 0 ? MessageFormat.format(msg, parameters) : msg;
+            }
+        }
+        // should be unreachable
+        return msg;
     }
 
     /**
@@ -287,4 +335,57 @@
     public void setThreadName(final String threadName) {
         this.threadName = threadName;
     }
+
+    /**
+     * Set the raw message.  Any cached formatted message is discarded.  The parameter format is set to be
+     * {@link java.text.MessageFormat}-style.
+     *
+     * @param message the new raw message
+     */
+    public void setMessage(final String message) {
+        setMessage(message, FormatStyle.MESSAGE_FORMAT);
+    }
+
+    /**
+     * Set the raw message.  Any cached formatted message is discarded.  The parameter format is set according to the
+     * given argument.
+     *
+     * @param message the new raw message
+     * @param formatStyle the format style to use
+     */
+    public void setMessage(final String message, final FormatStyle formatStyle) {
+        this.formatStyle = formatStyle == null ? FormatStyle.MESSAGE_FORMAT : formatStyle;
+        formattedMessage = null;
+        super.setMessage(message);
+    }
+
+    /**
+     * Set the parameters to the log message.  Any cached formatted message is discarded.
+     *
+     * @param parameters the log message parameters. (may be null)
+     */
+    public void setParameters(final Object[] parameters) {
+        formattedMessage = null;
+        super.setParameters(parameters);
+    }
+
+    /**
+     * Set the localization resource bundle.  Any cached formatted message is discarded.
+     *
+     * @param bundle localization bundle (may be null)
+     */
+    public void setResourceBundle(final ResourceBundle bundle) {
+        formattedMessage = null;
+        super.setResourceBundle(bundle);
+    }
+
+    /**
+     * Set the localization resource bundle name.  Any cached formatted message is discarded.
+     *
+     * @param name localization bundle name (may be null)
+     */
+    public void setResourceBundleName(final String name) {
+        formattedMessage = null;
+        super.setResourceBundleName(name);
+    }
 }




More information about the jboss-svn-commits mailing list