Return-Path: X-Original-To: apmail-logging-commits-archive@minotaur.apache.org Delivered-To: apmail-logging-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id CA75AE2FC for ; Fri, 11 Jan 2013 01:40:25 +0000 (UTC) Received: (qmail 93848 invoked by uid 500); 11 Jan 2013 01:40:25 -0000 Delivered-To: apmail-logging-commits-archive@logging.apache.org Received: (qmail 93824 invoked by uid 500); 11 Jan 2013 01:40:25 -0000 Mailing-List: contact commits-help@logging.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@logging.apache.org Delivered-To: mailing list commits@logging.apache.org Received: (qmail 93816 invoked by uid 99); 11 Jan 2013 01:40:25 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 11 Jan 2013 01:40:25 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 11 Jan 2013 01:40:22 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 43BDE23888FE; Fri, 11 Jan 2013 01:40:02 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: commits@logging.apache.org From: rgoers@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20130111014002.43BDE23888FE@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 @@ + + MessagePatternConverter now returns "null" if the log message is null. + Serialized LogEvents were not reset in the output stream causing them to deserialize incorrectly.