diff --git a/log4j-jul/pom.xml b/log4j-jul/pom.xml index 3ca1fa3..2eb4724 100644 --- a/log4j-jul/pom.xml +++ b/log4j-jul/pom.xml @@ -63,6 +63,32 @@ + + maven-compiler-plugin + ${compiler.plugin.version} + + + default-testCompile + + testCompile + + test-compile + + none + + + + process-test-plugins + + testCompile + + test-compile + + only + + + + org.apache.maven.plugins diff --git a/log4j-jul/src/main/java/org/apache/logging/log4j/jul/ApiLogger.java b/log4j-jul/src/main/java/org/apache/logging/log4j/jul/ApiLogger.java index adac9be..d558fdb 100644 --- a/log4j-jul/src/main/java/org/apache/logging/log4j/jul/ApiLogger.java +++ b/log4j-jul/src/main/java/org/apache/logging/log4j/jul/ApiLogger.java @@ -23,6 +23,7 @@ import java.util.logging.LogRecord; import java.util.logging.Logger; import org.apache.logging.log4j.message.Message; +import org.apache.logging.log4j.message.MessageFactory; import org.apache.logging.log4j.spi.ExtendedLogger; /** @@ -56,7 +57,11 @@ public class ApiLogger extends Logger { return; } final org.apache.logging.log4j.Level level = LevelTranslator.toLevel(record.getLevel()); - final Message message = logger.getMessageFactory().newMessage(record.getMessage(), record.getParameters()); + final Object[] parameters = record.getParameters(); + final MessageFactory messageFactory = logger.getMessageFactory(); + final Message message = parameters == null ? + messageFactory.newMessage(record.getMessage()) /* LOG4J2-1251: not formatted case */: + messageFactory.newMessage(record.getMessage(), parameters); final Throwable thrown = record.getThrown(); logger.logIfEnabled(FQCN, level, null, message, thrown); } diff --git a/log4j-jul/src/test/java/org/apache/logging/log4j/jul/BracketInNotInterpolatedMessageTest.java b/log4j-jul/src/test/java/org/apache/logging/log4j/jul/BracketInNotInterpolatedMessageTest.java index af1e699..c5cfa8d 100644 --- a/log4j-jul/src/test/java/org/apache/logging/log4j/jul/BracketInNotInterpolatedMessageTest.java +++ b/log4j-jul/src/test/java/org/apache/logging/log4j/jul/BracketInNotInterpolatedMessageTest.java @@ -1,4 +1,94 @@ +/* + * 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.jul; +import org.apache.logging.log4j.core.Filter; +import org.apache.logging.log4j.core.Layout; +import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.appender.AbstractAppender; +import org.apache.logging.log4j.core.config.plugins.Plugin; +import org.apache.logging.log4j.core.config.plugins.PluginAttribute; +import org.apache.logging.log4j.core.config.plugins.PluginElement; +import org.apache.logging.log4j.core.config.plugins.PluginFactory; +import org.apache.logging.log4j.test.appender.ListAppender; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; +import java.util.logging.LogRecord; +import java.util.logging.Logger; + +import static java.util.logging.Level.INFO; +import static org.junit.Assert.assertEquals; + public class BracketInNotInterpolatedMessageTest { + private ListAppender appender; + + @BeforeClass + public static void setUpClass() { + System.setProperty("java.util.logging.manager", LogManager.class.getName()); + System.setProperty("log4j.configurationFile", "log4j2-interpolation-noparms.xml"); + } + + @AfterClass + public static void tearDownClass() { + System.clearProperty("java.util.logging.manager"); + } + + @Test + public void noInterpolation() { + BracketInNotInterpolatedMessageMemory.EVENTS.clear(); + final Logger logger = Logger.getLogger("Test"); + logger.info("{raw}"); + logger.log(new LogRecord(INFO, "{raw}"));// should lead to the same as previous but was not the case LOG4J2-1251 + assertEquals(2, BracketInNotInterpolatedMessageMemory.EVENTS.size()); + assertEquals("{raw}",BracketInNotInterpolatedMessageMemory.EVENTS.get(0)); + assertEquals("{raw}",BracketInNotInterpolatedMessageMemory.EVENTS.get(1)); + } + + @Plugin(name = "BracketInNotInterpolatedMessageMemory", category = "Core", elementType = "appender") + public static class BracketInNotInterpolatedMessageMemory extends AbstractAppender { + private static final List EVENTS = new ArrayList<>(); + + private BracketInNotInterpolatedMessageMemory( + final String name, final Filter filter, final Layout layout) { + super(name, filter, layout); + } + + private BracketInNotInterpolatedMessageMemory( + final String name, final Filter filter, + final Layout layout, final boolean ignoreExceptions) { + super(name, filter, layout, ignoreExceptions); + } + + @PluginFactory + public static BracketInNotInterpolatedMessageMemory createAppender( + @PluginAttribute("name") final String name, + @PluginElement("Layout") Layout layout, + @PluginElement("Filter") final Filter filter) { + return new BracketInNotInterpolatedMessageMemory(name, filter, layout); + } + + @Override + public void append(final LogEvent event) { + EVENTS.add(event.getMessage().getFormattedMessage()); + } + } } diff --git a/log4j-jul/src/test/resources/log4j2-interpolation-noparms.xml b/log4j-jul/src/test/resources/log4j2-interpolation-noparms.xml index bbfeb91..4a182b7 100644 --- a/log4j-jul/src/test/resources/log4j2-interpolation-noparms.xml +++ b/log4j-jul/src/test/resources/log4j2-interpolation-noparms.xml @@ -14,33 +14,15 @@ ~ See the License for the specific language governing permissions and ~ limitations under the License. --> - - + - - - - - - - - + + + - - - - - - - - - - - - - - + +