logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rgo...@apache.org
Subject svn commit: r1364491 - in /logging/log4j/log4j2/trunk: api/src/main/java/org/apache/logging/log4j/message/ api/src/test/java/org/apache/logging/log4j/message/ core/src/main/java/org/apache/logging/log4j/core/pattern/
Date Mon, 23 Jul 2012 04:36:52 GMT
Author: rgoers
Date: Mon Jul 23 04:36:52 2012
New Revision: 1364491

URL: http://svn.apache.org/viewvc?rev=1364491&view=rev
Log:
Rename FormattedMessage to MultiFormatMessage. Add JSON and JAVA as supported MapMessage formats.

Added:
    logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/message/MultiformatMessage.java
      - copied, changed from r1364421, logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/message/FormattedMessage.java
Removed:
    logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/message/FormattedMessage.java
Modified:
    logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/message/MapMessage.java
    logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/message/StructuredDataMessage.java
    logging/log4j/log4j2/trunk/api/src/test/java/org/apache/logging/log4j/message/MapMessageTest.java
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/pattern/MessagePatternConverter.java

Modified: logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/message/MapMessage.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/message/MapMessage.java?rev=1364491&r1=1364490&r2=1364491&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/message/MapMessage.java
(original)
+++ logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/message/MapMessage.java
Mon Jul 23 04:36:52 2012
@@ -26,17 +26,19 @@ import java.util.TreeMap;
 /**
  * Represents a Message that consists of a Map.
  */
-public class MapMessage implements FormattedMessage, Serializable {
+public class MapMessage implements MultiformatMessage, Serializable {
     /**
      * When set as the format specifier causes the Map to be formatted as XML.
      */
-    public static final String XML = "XML";
+
+    public enum MapFormat {
+        XML, JSON, JAVA
+    }
+
     private static final long serialVersionUID = -5031471831131487120L;
 
     private final Map<String, String> data;
 
-    private String format = null;
-
     /**
      * Constructor.
      */
@@ -53,23 +55,6 @@ public class MapMessage implements Forma
     }
 
     /**
-     * The format String. Specifying "xml" will cause the message to be XML.
-     * @param format The message format.
-     */
-    public void setFormat(String format) {
-        this.format = format;
-    }
-
-    /**
-     * Return the format String.
-     * @return the format String.
-     */
-    public String getFormat() {
-        return this.format;
-    }
-
-
-    /**
      * Return the data elements as if they were parameters on the logging event.
      * @return the data elements.
      */
@@ -149,21 +134,44 @@ public class MapMessage implements Forma
      * @return The formatted String.
      */
     public String asString() {
-        return asString(format == null ? "" : format);
+        return asString((MapFormat) null);
     }
 
+    public String asString(String format) {
+        try {
+            return asString(MapFormat.valueOf(format.toUpperCase()));
+        } catch (IllegalArgumentException ex) {
+            return asString();
+        }
+    }
     /**
      * Format the Structured data as described in RFC 5424.
      *
      * @param format The format identifier. Ignored in this implementation.
      * @return The formatted String.
      */
-    public String asString(String format) {
+    private String asString(MapFormat format) {
         StringBuilder sb = new StringBuilder();
-        if (format.equalsIgnoreCase(XML)) {
-            asXML(sb);
-        } else {
+        if (format == null) {
             appendMap(sb);
+        } else {
+            switch (format) {
+                case XML : {
+                    asXML(sb);
+                    break;
+                }
+                case JSON : {
+                    asJSON(sb);
+                    break;
+                }
+                case JAVA : {
+                    asJava(sb);
+                    break;
+                }
+                default : {
+                    appendMap(sb);
+                }
+            }
         }
         return sb.toString();
     }
@@ -185,6 +193,30 @@ public class MapMessage implements Forma
         return asString();
     }
 
+    /**
+     *
+     * @param formats An array of Strings that provide extra information about how to format
the message.
+     * MapMessage uses the first format specifier it recognizes. The supported formats are
XML, JSON, and
+     * JAVA. The default format is key1="value1" key2="value2" as required by RFC 5424 messages.
+     *
+     * @return The formatted message.
+     */
+    public String getFormattedMessage(String[] formats) {
+        if (formats == null || formats.length == 0) {
+            return asString();
+        } else {
+            for (String format : formats) {
+                for (MapFormat f : MapFormat.values()) {
+                    if (f.name().equalsIgnoreCase(format)) {
+                        return asString(f);
+                    }
+                }
+            }
+            return asString();
+        }
+
+    }
+
     protected void appendMap(StringBuilder sb) {
         SortedMap<String, String> sorted = new TreeMap<String, String>(data);
         boolean first = true;
@@ -197,6 +229,36 @@ public class MapMessage implements Forma
         }
     }
 
+    protected void asJSON(StringBuilder sb) {
+        SortedMap<String, String> sorted = new TreeMap<String, String>(data);
+        boolean first = true;
+        sb.append("{");
+        for (Map.Entry<String, String> entry : sorted.entrySet()) {
+            if (!first) {
+                sb.append(", ");
+            }
+            first = false;
+            sb.append("\"").append(entry.getKey()).append("\":");
+            sb.append("\"").append(entry.getValue()).append("\"");
+        }
+        sb.append("}");
+    }
+
+
+    protected void asJava(StringBuilder sb) {
+        SortedMap<String, String> sorted = new TreeMap<String, String>(data);
+        boolean first = true;
+        sb.append("{");
+        for (Map.Entry<String, String> entry : sorted.entrySet()) {
+            if (!first) {
+                sb.append(", ");
+            }
+            first = false;
+            sb.append(entry.getKey()).append("=\"").append(entry.getValue()).append("\"");
+        }
+        sb.append("}");
+    }
+
     public MapMessage newInstance(Map<String, String> map) {
         return new MapMessage(map);
     }

Copied: logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/message/MultiformatMessage.java
(from r1364421, logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/message/FormattedMessage.java)
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/message/MultiformatMessage.java?p2=logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/message/MultiformatMessage.java&p1=logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/message/FormattedMessage.java&r1=1364421&r2=1364491&rev=1364491&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/message/FormattedMessage.java
(original)
+++ logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/message/MultiformatMessage.java
Mon Jul 23 04:36:52 2012
@@ -17,21 +17,20 @@
 package org.apache.logging.log4j.message;
 
 /**
- * A Message that can have a format String attached to it. The format string is used by the
+ * A Message that can render itself in more than one way. The format string is used by the
  * Message implementation as extra information that it may use to help it to determine how
  * to format itself. For example, MapMessage accepts a format of "XML" to tell it to render
  * the Map as XML instead of its default format of {key1="value1" key2="value2"}.
  */
-public interface FormattedMessage extends Message {
-    /**
-     * Set the message format.
-     * @param format The message format.
-     */
-    void setFormat(String format);
+public interface MultiformatMessage extends Message {
 
     /**
-     * Return the message format.
-     * @return the message format String.
+     * Returns the Message formatted as a String.
+     *
+     * @param formats An array of Strings that provide extra information about how to format
the message.
+     * Each MultiformatMessage implementation is free to use the provided formats however
they choose.
+     *
+     * @return The message String.
      */
-    String getFormat();
+    String getFormattedMessage(String[] formats);
 }

Modified: logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/message/StructuredDataMessage.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/message/StructuredDataMessage.java?rev=1364491&r1=1364490&r2=1364491&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/message/StructuredDataMessage.java
(original)
+++ logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/message/StructuredDataMessage.java
Mon Jul 23 04:36:52 2012
@@ -22,11 +22,11 @@ import java.util.Map;
 /**
  * Represents a Message that conforms to RFC 5424 (http://tools.ietf.org/html/rfc5424).
  */
-public class StructuredDataMessage extends MapMessage implements FormattedMessage, Serializable
{
+public class StructuredDataMessage extends MapMessage implements MultiformatMessage, Serializable
{
     /**
      * Full message format includes the type and message.
      */
-    public static final String FULL = "full";
+    public static final String FULL = "FULL";
 
     private static final long serialVersionUID = 1703221292892071920L;
     private static final int MAX_LENGTH = 32;
@@ -243,9 +243,32 @@ public class StructuredDataMessage exten
         return asString(FULL, null);
     }
 
+    /**
+     * Format the message according the the specified format.
+     * @param formats An array of Strings that provide extra information about how to format
the message.
+     * StructuredDataMessage accepts only a format of "FULL" which will cause the event type
to be
+     * prepended and the event message to be appended. Specifying any other value will cause
only the
+     * StructuredData to be included. The default is "FULL".
+     *
+     * @return
+     */
+    @Override
+    public String getFormattedMessage(String[] formats) {
+        if (formats != null && formats.length > 0) {
+            for (String format : formats) {
+                if (format.equalsIgnoreCase(FULL)) {
+                    return asString(FULL, null);
+                }
+            }
+            return asString(null, null);
+        } else {
+            return asString(FULL, null);
+        }
+    }
+
     @Override
     public String toString() {
-        return asString(null);
+        return asString(null, null);
     }
 
 

Modified: logging/log4j/log4j2/trunk/api/src/test/java/org/apache/logging/log4j/message/MapMessageTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/api/src/test/java/org/apache/logging/log4j/message/MapMessageTest.java?rev=1364491&r1=1364490&r2=1364491&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/api/src/test/java/org/apache/logging/log4j/message/MapMessageTest.java
(original)
+++ logging/log4j/log4j2/trunk/api/src/test/java/org/apache/logging/log4j/message/MapMessageTest.java
Mon Jul 23 04:36:52 2012
@@ -40,13 +40,34 @@ public class MapMessageTest {
     public void testXML() {
         String testMsg = "Test message {}";
         MapMessage msg = new MapMessage();
-        msg.setFormat("XML");
         msg.put("message", testMsg);
         msg.put("project", "Log4j");
-        String result = msg.getFormattedMessage();
+        String result = msg.getFormattedMessage(new String[]{"XML"});
         String expected = "<Map>\n  <Entry key=message>Test message {}</Entry>\n"
+
             "  <Entry key=project>Log4j</Entry>\n" +
             "</Map>";
         assertEquals(expected, result);
     }
+
+    @Test
+    public void testJSON() {
+        String testMsg = "Test message {}";
+        MapMessage msg = new MapMessage();
+        msg.put("message", testMsg);
+        msg.put("project", "Log4j");
+        String result = msg.getFormattedMessage(new String[]{"JSON"});
+        String expected = "{\"message\":\"Test message {}\", \"project\":\"Log4j\"}";
+        assertEquals(expected, result);
+    }
+
+    @Test
+    public void testJava() {
+        String testMsg = "Test message {}";
+        MapMessage msg = new MapMessage();
+        msg.put("message", testMsg);
+        msg.put("project", "Log4j");
+        String result = msg.getFormattedMessage(new String[]{"Java"});
+        String expected = "{message=\"Test message {}\", project=\"Log4j\"}";
+        assertEquals(expected, result);
+    }
 }

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/pattern/MessagePatternConverter.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/pattern/MessagePatternConverter.java?rev=1364491&r1=1364490&r2=1364491&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/pattern/MessagePatternConverter.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/pattern/MessagePatternConverter.java
Mon Jul 23 04:36:52 2012
@@ -18,7 +18,7 @@ package org.apache.logging.log4j.core.pa
 
 import org.apache.logging.log4j.core.LogEvent;
 import org.apache.logging.log4j.core.config.plugins.Plugin;
-import org.apache.logging.log4j.message.FormattedMessage;
+import org.apache.logging.log4j.message.MultiformatMessage;
 import org.apache.logging.log4j.message.Message;
 
 /**
@@ -28,7 +28,7 @@ import org.apache.logging.log4j.message.
 @ConverterKeys({"m", "msg", "message" })
 public final class MessagePatternConverter extends LogEventPatternConverter {
 
-    private final String format;
+    private final String[] formats;
 
     /**
      * Private constructor.
@@ -36,7 +36,7 @@ public final class MessagePatternConvert
      */
     private MessagePatternConverter(final String[] options) {
         super("Message", "message");
-        format = (options != null && options.length > 0) ? options[0] : null;
+        formats = options;
     }
 
     /**
@@ -54,9 +54,12 @@ public final class MessagePatternConvert
      */
     public void format(final LogEvent event, final StringBuilder toAppendTo) {
         Message msg = event.getMessage();
-        if (msg != null && msg instanceof FormattedMessage) {
-            ((FormattedMessage) msg).setFormat(format);
+        if (msg != null) {
+            if (msg instanceof MultiformatMessage) {
+                toAppendTo.append(((MultiformatMessage) msg).getFormattedMessage(formats));
+            } else{
+                toAppendTo.append(msg.getFormattedMessage());
+            }
         }
-        toAppendTo.append(msg.getFormattedMessage());
     }
 }



Mime
View raw message