-
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.
Fix handling of
log4j2.messageFactory
and `log4j2.flowMessageFactor…
…y` properties (#2505)
- Loading branch information
Showing
6 changed files
with
276 additions
and
36 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
71 changes: 71 additions & 0 deletions
71
...st/src/test/java/org/apache/logging/log4j/core/LoggerMessageFactoryCustomizationTest.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,71 @@ | ||
/* | ||
* 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; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.apache.logging.log4j.message.AbstractMessageFactory; | ||
import org.apache.logging.log4j.message.DefaultFlowMessageFactory; | ||
import org.apache.logging.log4j.message.Message; | ||
import org.apache.logging.log4j.message.MessageFactory; | ||
import org.apache.logging.log4j.message.ParameterizedMessageFactory; | ||
import org.junit.jupiter.api.Test; | ||
import org.junitpioneer.jupiter.ClearSystemProperty; | ||
import org.junitpioneer.jupiter.SetSystemProperty; | ||
|
||
class LoggerMessageFactoryCustomizationTest { | ||
|
||
@Test | ||
@ClearSystemProperty(key = "log4j2.messageFactory") | ||
@ClearSystemProperty(key = "log4j2.flowMessageFactory") | ||
void arguments_should_be_honored() { | ||
final LoggerContext loggerContext = | ||
new LoggerContext(LoggerMessageFactoryCustomizationTest.class.getSimpleName()); | ||
final Logger logger = new Logger( | ||
loggerContext, "arguments_should_be_honored", new TestMessageFactory(), new TestFlowMessageFactory()); | ||
assertTestMessageFactories(logger); | ||
} | ||
|
||
@Test | ||
@SetSystemProperty( | ||
key = "log4j2.messageFactory", | ||
value = "org.apache.logging.log4j.core.LoggerMessageFactoryCustomizationTest$TestMessageFactory") | ||
@SetSystemProperty( | ||
key = "log4j2.flowMessageFactory", | ||
value = "org.apache.logging.log4j.core.LoggerMessageFactoryCustomizationTest$TestFlowMessageFactory") | ||
void properties_should_be_honored() { | ||
final LoggerContext loggerContext = | ||
new LoggerContext(LoggerMessageFactoryCustomizationTest.class.getSimpleName()); | ||
final Logger logger = new Logger(loggerContext, "properties_should_be_honored", null, null); | ||
assertTestMessageFactories(logger); | ||
} | ||
|
||
private static void assertTestMessageFactories(Logger logger) { | ||
assertThat((MessageFactory) logger.getMessageFactory()).isInstanceOf(TestMessageFactory.class); | ||
assertThat(logger.getFlowMessageFactory()).isInstanceOf(TestFlowMessageFactory.class); | ||
} | ||
|
||
public static final class TestMessageFactory extends AbstractMessageFactory { | ||
|
||
@Override | ||
public Message newMessage(final String message, final Object... params) { | ||
return ParameterizedMessageFactory.INSTANCE.newMessage(message, params); | ||
} | ||
} | ||
|
||
public static final class TestFlowMessageFactory extends DefaultFlowMessageFactory {} | ||
} |
41 changes: 41 additions & 0 deletions
41
.../test/java/org/apache/logging/log4j/core/LoggerMessageFactoryDefaultsTlaDisabledTest.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,41 @@ | ||
/* | ||
* 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; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.apache.logging.log4j.core.util.Constants; | ||
import org.apache.logging.log4j.message.DefaultFlowMessageFactory; | ||
import org.apache.logging.log4j.message.MessageFactory; | ||
import org.apache.logging.log4j.message.ParameterizedMessageFactory; | ||
import org.junit.jupiter.api.Test; | ||
import org.junitpioneer.jupiter.SetSystemProperty; | ||
|
||
class LoggerMessageFactoryDefaultsTlaDisabledTest { | ||
|
||
@Test | ||
@SetSystemProperty(key = "log4j2.enableThreadLocals", value = "false") | ||
void defaults_should_match_when_thread_locals_disabled() { | ||
assertThat(Constants.ENABLE_THREADLOCALS).isFalse(); | ||
final LoggerContext loggerContext = | ||
new LoggerContext(LoggerMessageFactoryDefaultsTlaDisabledTest.class.getSimpleName()); | ||
final Logger logger = | ||
new Logger(loggerContext, "defaults_should_match_when_thread_locals_disabled", null, null); | ||
assertThat((MessageFactory) logger.getMessageFactory()).isSameAs(ParameterizedMessageFactory.INSTANCE); | ||
assertThat(logger.getFlowMessageFactory()).isSameAs(DefaultFlowMessageFactory.INSTANCE); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...c/test/java/org/apache/logging/log4j/core/LoggerMessageFactoryDefaultsTlaEnabledTest.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,41 @@ | ||
/* | ||
* 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; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.apache.logging.log4j.core.util.Constants; | ||
import org.apache.logging.log4j.message.DefaultFlowMessageFactory; | ||
import org.apache.logging.log4j.message.MessageFactory; | ||
import org.apache.logging.log4j.message.ReusableMessageFactory; | ||
import org.junit.jupiter.api.Test; | ||
import org.junitpioneer.jupiter.SetSystemProperty; | ||
|
||
class LoggerMessageFactoryDefaultsTlaEnabledTest { | ||
|
||
@Test | ||
@SetSystemProperty(key = "log4j2.is.webapp", value = "false") | ||
@SetSystemProperty(key = "log4j2.enableThreadLocals", value = "true") | ||
void defaults_should_match_when_thread_locals_enabled() { | ||
assertThat(Constants.ENABLE_THREADLOCALS).isTrue(); | ||
final LoggerContext loggerContext = | ||
new LoggerContext(LoggerMessageFactoryDefaultsTlaEnabledTest.class.getSimpleName()); | ||
final Logger logger = new Logger(loggerContext, "defaults_should_match_when_thread_locals_enabled", null, null); | ||
assertThat((MessageFactory) logger.getMessageFactory()).isSameAs(ReusableMessageFactory.INSTANCE); | ||
assertThat(logger.getFlowMessageFactory()).isSameAs(DefaultFlowMessageFactory.INSTANCE); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns="https://logging.apache.org/xml/ns" | ||
xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd" | ||
type="fixed"> | ||
<issue id="2505" link="https://github.com/apache/logging-log4j2/pull/2505"/> | ||
<description format="asciidoc">Fix handling of `log4j2.messageFactory` and `log4j2.flowMessageFactory` properties</description> | ||
</entry> |