-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LOG4J2-3198: Log4j2 no longer formates lookups in messages by default
Lookups in messages are confusing, and muddy the line between logging APIs and implementation. Given a particular API, there's an expectation that a particular shape of call will result in specific results. However, lookups in messages can be passed into JUL and will result in resolved output in log4j formatted output, but not any other implementations despite no direct dependency on those implementations. There's also a cost to searching formatted message strings for particular escape sequences which define lookups. This feature is not used as far as we've been able to tell searching github and stackoverflow, so it's unnecessary for every log event in every application to burn several cpu cycles searching for the value.
- Loading branch information
1 parent
df0b5a1
commit 0f5bff1
Showing
6 changed files
with
103 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...rc/test/java/org/apache/logging/log4j/core/pattern/MessagePatternConverterLookupTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* 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.builder.impl.DefaultConfigurationBuilder; | ||
import org.apache.logging.log4j.core.impl.Log4jLogEvent; | ||
import org.apache.logging.log4j.message.Message; | ||
import org.apache.logging.log4j.message.ParameterizedMessage; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class MessagePatternConverterLookupTest { | ||
|
||
@BeforeAll | ||
public static void beforeAll() { | ||
System.setProperty("log4j2.formatMsgNoLookups", "false"); | ||
} | ||
|
||
@AfterAll | ||
public static void afterAll() { | ||
System.clearProperty("log4j2.formatMsgNoLookups"); | ||
} | ||
|
||
@Test | ||
public void testLookup() { | ||
final Configuration config = new DefaultConfigurationBuilder() | ||
.addProperty("foo", "bar") | ||
.build(true); | ||
final MessagePatternConverter converter = MessagePatternConverter.newInstance(config, null); | ||
final Message msg = new ParameterizedMessage("${foo}"); | ||
final LogEvent event = Log4jLogEvent.newBuilder() // | ||
.setLoggerName("MyLogger") // | ||
.setLevel(Level.DEBUG) // | ||
.setMessage(msg).build(); | ||
final StringBuilder sb = new StringBuilder(); | ||
converter.format(event, sb); | ||
assertEquals("bar", sb.toString(), "Unexpected result"); | ||
} | ||
|
||
@Test | ||
public void testDisabledLookup() { | ||
final Configuration config = new DefaultConfigurationBuilder() | ||
.addProperty("foo", "bar") | ||
.build(true); | ||
final MessagePatternConverter converter = MessagePatternConverter.newInstance( | ||
config, new String[] {"nolookups"}); | ||
final Message msg = new ParameterizedMessage("${foo}"); | ||
final LogEvent event = Log4jLogEvent.newBuilder() // | ||
.setLoggerName("MyLogger") // | ||
.setLevel(Level.DEBUG) // | ||
.setMessage(msg).build(); | ||
final StringBuilder sb = new StringBuilder(); | ||
converter.format(event, sb); | ||
assertEquals("${foo}", sb.toString(), "Expected the raw pattern string without lookup"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters