Skip to content

Commit 156b34e

Browse files
committed
Fix ThreadLocalRecyclerNestedLoggingTest (#2016)
1 parent 6bd656d commit 156b34e

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

log4j-api/src/main/java/org/apache/logging/log4j/spi/LoggingSystemProperty.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,15 @@ public static class Constant {
486486
LoggingSystemProperty.SYSTEM_PROPERTY_PREFIX + PropertyComponent.Constant.WEB + DELIM + IS_WEB_APP;
487487

488488
static final String RECYCLER_FACTORY = "factory";
489+
public static final String RECYCLER_FACTORY_PROPERTY = LoggingSystemProperty.SYSTEM_PROPERTY_PREFIX
490+
+ PropertyComponent.Constant.RECYCLER
491+
+ DELIM
492+
+ RECYCLER_FACTORY;
493+
489494
static final String RECYCLER_CAPACITY = "capacity";
495+
public static final String RECYCLER_CAPACITY_PROPERTY = LoggingSystemProperty.SYSTEM_PROPERTY_PREFIX
496+
+ PropertyComponent.Constant.RECYCLER
497+
+ DELIM
498+
+ RECYCLER_CAPACITY;
490499
}
491500
}

log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/ThreadLocalRecyclerNestedLoggingTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,18 @@
2525
import org.apache.logging.log4j.core.test.appender.ListAppender;
2626
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
2727
import org.apache.logging.log4j.core.test.junit.Named;
28+
import org.apache.logging.log4j.internal.recycler.ThreadLocalRecyclerFactoryProvider;
29+
import org.apache.logging.log4j.spi.LoggingSystemProperty;
2830
import org.assertj.core.api.Assertions;
2931
import org.junit.jupiter.api.Test;
32+
import org.junitpioneer.jupiter.SetSystemProperty;
3033

3134
/**
3235
* Tests if logging while trying to encode an event causes thread local recycler to incorrectly share buffers and end up overriding layout's earlier encoding work.
3336
*
3437
* @see <a href="https://issues.apache.org/jira/browse/LOG4J2-2368">LOG4J2-2368</a>
3538
*/
39+
@SetSystemProperty(key = LoggingSystemProperty.Constant.RECYCLER_FACTORY_PROPERTY, value = "threadLocal")
3640
public class ThreadLocalRecyclerNestedLoggingTest {
3741

3842
private static final class ThrowableLoggingInGetMessage extends RuntimeException {
@@ -56,10 +60,23 @@ public void nested_logging_should_not_pollute_thread_local(
5660
final LoggerContext loggerContext,
5761
final @Named(value = "List1") ListAppender appender1,
5862
final @Named(value = "List2") ListAppender appender2) {
63+
64+
// Verify the recycler factory type
65+
final String actualRecyclerFactoryClassName =
66+
loggerContext.getConfiguration().getRecyclerFactory().getClass().getCanonicalName();
67+
final String expectedRecyclerFactoryClassName =
68+
ThreadLocalRecyclerFactoryProvider.class.getCanonicalName() + ".ThreadLocalRecyclerFactory";
69+
Assertions.assertThat(actualRecyclerFactoryClassName).isEqualTo(expectedRecyclerFactoryClassName);
70+
71+
// Perform nested logging
5972
final Logger logger = loggerContext.getLogger(ThreadLocalRecyclerNestedLoggingTest.class);
6073
logger.error("A", new ThrowableLoggingInGetMessage(logger));
74+
75+
// Collect logged messages
6176
final List<String> messages1 = readAppendedMessages(appender1);
6277
final List<String> messages2 = readAppendedMessages(appender2);
78+
79+
// Verify logged messages
6380
Assertions.assertThat(messages1)
6481
.containsExactlyInAnyOrderElementsOf(messages2)
6582
.containsExactlyInAnyOrderElementsOf(Stream.of("['B',null]", "['A','C']")

log4j-layout-template-json-test/src/test/resources/threadLocalRecyclerNestedLogging.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@
1515
~ See the License for the specific language governing permissions and
1616
~ limitations under the License.
1717
-->
18-
<Configuration status="OFF" name="NestedLoggingFromThrowableMessageTest">
18+
<Configuration status="OFF" name="ThreadLocalRecyclerNestedLoggingTest">
1919
<Properties>
2020
<Property name="eventTemplate">[{"$resolver": "message", "stringified": true}, {"$resolver": "exception", "field": "message"}]</Property>
2121
</Properties>
2222
<Appenders>
2323
<List name="List1" raw="true">
24-
<JsonTemplateLayout eventTemplate="${eventTemplate}" recyclerFactory="threadLocal" eventDelimiter=""/>
24+
<JsonTemplateLayout eventTemplate="${eventTemplate}" eventDelimiter=""/>
2525
</List>
2626
<List name="List2" raw="true">
27-
<JsonTemplateLayout eventTemplate="${eventTemplate}" recyclerFactory="threadLocal" eventDelimiter=""/>
27+
<JsonTemplateLayout eventTemplate="${eventTemplate}" eventDelimiter=""/>
2828
</List>
2929
</Appenders>
3030
<Loggers>

0 commit comments

Comments
 (0)