logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rgo...@apache.org
Subject svn commit: r1204755 - in /logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src: main/java/org/apache/logging/log4j/core/pattern/MapPatternConverter.java test/java/org/apache/logging/log4j/core/pattern/MapPatternConverterTest.java
Date Mon, 21 Nov 2011 23:58:14 GMT
Author: rgoers
Date: Mon Nov 21 23:58:14 2011
New Revision: 1204755

URL: http://svn.apache.org/viewvc?rev=1204755&view=rev
Log:
Add MapPatternConverter

Added:
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/MapPatternConverter.java
      - copied, changed from r1195339, logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/MDCPatternConverter.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/pattern/MapPatternConverterTest.java

Copied: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/MapPatternConverter.java
(from r1195339, logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/MDCPatternConverter.java)
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/MapPatternConverter.java?p2=logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/MapPatternConverter.java&p1=logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/MDCPatternConverter.java&r1=1195339&r2=1204755&rev=1204755&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/MDCPatternConverter.java
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/pattern/MapPatternConverter.java
Mon Nov 21 23:58:14 2011
@@ -19,21 +19,21 @@ 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.MapMessage;
 
 import java.util.Map;
 import java.util.Set;
 import java.util.TreeSet;
 
 /**
- * Able to handle the contents of the LogEvent's MDC and either
+ * Able to handle the contents of the LogEvent's MapMessage and either
  * output the entire contents of the properties in a similar format to the
  * java.util.Hashtable.toString(), or to output the value of a specific key
- * within the property bundle
- * when this pattern converter has the option set.
+ * within the Map.
  */
- @Plugin(name="MDCPatternConverter", type="Converter")
-@ConverterKeys({"X", "mdc", "MDC"})
-public final class MDCPatternConverter extends LogEventPatternConverter {
+@Plugin(name="MapPatternConverter", type="Converter")
+@ConverterKeys({"K", "map", "MAP"})
+public final class MapPatternConverter extends LogEventPatternConverter {
     /**
      * Name of property to output.
      */
@@ -44,8 +44,8 @@ public final class MDCPatternConverter e
      *
      * @param options options, may be null.
      */
-    private MDCPatternConverter(final String[] options) {
-        super(((options != null) && (options.length > 0)) ? ("MDC{" + options[0]
+ "}") : "MDC", "mdc");
+    private MapPatternConverter(final String[] options) {
+        super(((options != null) && (options.length > 0)) ? ("MAP{" + options[0]
+ "}") : "MAP", "map");
         key = (options != null && options.length > 0) ? options[0] : null;
     }
 
@@ -55,38 +55,42 @@ public final class MDCPatternConverter e
      * @param options options, may be null or first element contains name of property to
format.
      * @return instance of PropertiesPatternConverter.
      */
-    public static MDCPatternConverter newInstance(final String[] options) {
-        return new MDCPatternConverter(options);
+    public static MapPatternConverter newInstance(final String[] options) {
+        return new MapPatternConverter(options);
     }
 
     /**
      * {@inheritDoc}
      */
     public void format(final LogEvent event, final StringBuilder toAppendTo) {
-        Map<String, String> contextMap = event.getContextMap();
+        MapMessage msg;
+        if (event.getMessage() instanceof MapMessage) {
+            msg = (MapMessage) event.getMessage();
+        } else {
+            return;
+        }
+        Map<String, String> map = msg.getData();
         // if there is no additional options, we output every single
-        // Key/Value pair for the MDC in a similar format to Hashtable.toString()
+        // Key/Value pair for the Map in a similar format to Hashtable.toString()
         if (key == null) {
-
-
-            if (contextMap == null || contextMap.size() == 0) {
+            if (map.size() == 0) {
                 toAppendTo.append("{}");
                 return;
             }
             StringBuilder sb = new StringBuilder("{");
-            Set<String> keys = new TreeSet<String>(contextMap.keySet());
+            Set<String> keys = new TreeSet<String>(map.keySet());
             for (String key : keys) {
                 if (sb.length() > 1) {
                     sb.append(", ");
                 }
-                sb.append(key).append("=").append(contextMap.get(key));
+                sb.append(key).append("=").append(map.get(key));
 
             }
             sb.append("}");
             toAppendTo.append(sb);
-        } else if (contextMap != null) {
+        } else {
             // otherwise they just want a single key output
-            Object val = contextMap.get(key);
+            String val = map.get(key);
 
             if (val != null) {
                 toAppendTo.append(val);

Added: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/pattern/MapPatternConverterTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/pattern/MapPatternConverterTest.java?rev=1204755&view=auto
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/pattern/MapPatternConverterTest.java
(added)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/pattern/MapPatternConverterTest.java
Mon Nov 21 23:58:14 2011
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.core.pattern;
+
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.impl.Log4jLogEvent;
+import org.apache.logging.log4j.message.MapMessage;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+/**
+ *
+ */
+public class MapPatternConverterTest {
+
+    @Test
+    public void testConverter() {
+
+        MapMessage msg = new MapMessage();
+        msg.put("subject", "I");
+        msg.put("verb", "love");
+        msg.put("object", "Log4j");
+        MapPatternConverter converter = MapPatternConverter.newInstance(null);
+        LogEvent event = new Log4jLogEvent("MyLogger", null, null, Level.DEBUG, msg, null);
+        StringBuilder sb = new StringBuilder();
+        converter.format(event, sb);
+        String str = sb.toString();
+        String expected = "subject=I";
+        assertTrue("Missing or incorrect subject. Expected " + expected + ", actual " + str,
str.contains(expected));
+        expected = "verb=love";
+        assertTrue("Missing or incorrect verb", str.contains(expected));
+        expected = "object=Log4j";
+        assertTrue("Missing or incorrect object", str.contains(expected));
+    }
+
+    @Test
+    public void testConverterWithKey() {
+
+        MapMessage msg = new MapMessage();
+        msg.put("subject", "I");
+        msg.put("verb", "love");
+        msg.put("object", "Log4j");
+        MapPatternConverter converter = MapPatternConverter.newInstance(new String[] {"object"});
+        LogEvent event = new Log4jLogEvent("MyLogger", null, null, Level.DEBUG, msg, null);
+        StringBuilder sb = new StringBuilder();
+        converter.format(event, sb);
+        String str = sb.toString();
+        String expected = "Log4j";
+        assertEquals(expected, str);
+    }
+}



Mime
View raw message