logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rgo...@apache.org
Subject svn commit: r1431787 - in /logging/log4j/log4j2/trunk: core/src/main/java/org/apache/logging/log4j/core/pattern/MessagePatternConverter.java core/src/test/java/org/apache/logging/log4j/core/pattern/MessagePatternConverterTest.java src/changes/changes.xml
Date Fri, 11 Jan 2013 01:40:02 GMT
Author: rgoers
Date: Fri Jan 11 01:40:01 2013
New Revision: 1431787

URL: http://svn.apache.org/viewvc?rev=1431787&view=rev
Log:
LOG4J2-143 - MessagePatternConverter now returns null if the log message is null.

Added:
    logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/pattern/MessagePatternConverterTest.java
Modified:
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/pattern/MessagePatternConverter.java
    logging/log4j/log4j2/trunk/src/changes/changes.xml

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=1431787&r1=1431786&r2=1431787&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
Fri Jan 11 01:40:01 2013
@@ -67,8 +67,12 @@ public final class MessagePatternConvert
             } else {
                 result = msg.getFormattedMessage();
             }
-            toAppendTo.append(config != null && result.contains("${") ?
-                config.getSubst().replace(event, result) : result);
+            if (result != null) {
+                toAppendTo.append(config != null && result.contains("${") ?
+                    config.getSubst().replace(event, result) : result);
+            } else {
+                toAppendTo.append("null");
+            }
         }
     }
 }

Added: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/pattern/MessagePatternConverterTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/pattern/MessagePatternConverterTest.java?rev=1431787&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/pattern/MessagePatternConverterTest.java
(added)
+++ logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/pattern/MessagePatternConverterTest.java
Fri Jan 11 01:40:01 2013
@@ -0,0 +1,74 @@
+/*
+ * 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.config.Configuration;
+import org.apache.logging.log4j.core.config.DefaultConfiguration;
+import org.apache.logging.log4j.core.impl.Log4jLogEvent;
+import org.apache.logging.log4j.message.Message;
+import org.apache.logging.log4j.message.SimpleMessage;
+import org.junit.Test;
+
+import static org.junit.Assert.assertTrue;
+
+/**
+ *
+ */
+public class MessagePatternConverterTest {
+
+    @Test
+    public void testPattern() throws Exception {
+        MessagePatternConverter converter = MessagePatternConverter.newInstance(null, null);
+        Message msg = new SimpleMessage("Hello!");
+        LogEvent event = new Log4jLogEvent("MyLogger", null, null, Level.DEBUG, msg, null);
+        StringBuilder sb = new StringBuilder();
+        converter.format(event, sb);
+        assertTrue("Unexpected result", "Hello!".equals(sb.toString()));
+        event = new Log4jLogEvent("MyLogger", null, null, Level.DEBUG, null, null);
+        sb = new StringBuilder();
+        converter.format(event, sb);
+        assertTrue("Incorrect length: " + sb.length(), sb.length() == 0);
+        msg = new SimpleMessage(null);
+        event = new Log4jLogEvent("MyLogger", null, null, Level.DEBUG, msg, null);
+        sb = new StringBuilder();
+        converter.format(event, sb);
+        assertTrue("Incorrect length: " + sb.length(), sb.length() == 4);
+
+    }
+
+    @Test
+    public void testPatternWithConfiguration() throws Exception {
+        Configuration config = new DefaultConfiguration();
+        MessagePatternConverter converter = MessagePatternConverter.newInstance(config, null);
+        Message msg = new SimpleMessage("Hello!");
+        LogEvent event = new Log4jLogEvent("MyLogger", null, null, Level.DEBUG, msg, null);
+        StringBuilder sb = new StringBuilder();
+        converter.format(event, sb);
+        assertTrue("Unexpected result", "Hello!".equals(sb.toString()));
+        event = new Log4jLogEvent("MyLogger", null, null, Level.DEBUG, null, null);
+        sb = new StringBuilder();
+        converter.format(event, sb);
+        assertTrue("Incorrect length: " + sb.length(), sb.length() == 0);
+        msg = new SimpleMessage(null);
+        event = new Log4jLogEvent("MyLogger", null, null, Level.DEBUG, msg, null);
+        sb = new StringBuilder();
+        converter.format(event, sb);
+        assertTrue("Incorrect length: " + sb.length(), sb.length() == 4);
+    }
+}

Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1431787&r1=1431786&r2=1431787&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Fri Jan 11 01:40:01 2013
@@ -23,6 +23,9 @@
 
   <body>
     <release version="2.0-beta4" date="TBD" description="Bug fixes and enhancements">
+      <action issue="LOG4J2-143" dev="rgoers" type="fix">
+        MessagePatternConverter now returns "null" if the log message is null.
+      </action>
       <action issue="LOG4J2-142" dev="rgoers" type="fix">
         Serialized LogEvents were not reset in the output stream causing them to deserialize
incorrectly.
       </action>



Mime
View raw message