From 68380f191be59992ebb6cb4490d1757761108a78 Mon Sep 17 00:00:00 2001 From: Xianren Liu Date: Sun, 1 Dec 2024 23:19:25 +0800 Subject: [PATCH] Add requiresLocation function for AsyncAppender (#3257) --- .../core/appender/AsyncAppenderTest.java | 7 ++++ .../test/resources/log4j-asynch-location.xml | 35 +++++++++++++++++++ .../log4j/core/appender/AsyncAppender.java | 19 ++++++++++ ...57_fix_AsyncAppender_requiresLocation.xml | 8 +++++ 4 files changed, 69 insertions(+) create mode 100644 log4j-core-test/src/test/resources/log4j-asynch-location.xml create mode 100644 src/changelog/.2.x.x/3257_fix_AsyncAppender_requiresLocation.xml diff --git a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/AsyncAppenderTest.java b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/AsyncAppenderTest.java index e693d2ca15f..dda884d9c40 100644 --- a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/AsyncAppenderTest.java +++ b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/AsyncAppenderTest.java @@ -173,4 +173,11 @@ public void testShutdownTimeout(final LoggerContext context) { context.getLogger("Logger").info("This is a test"); context.stop(); } + + @Test + @LoggerContextSource("log4j-asynch-location.xml") + public void testRequiresLocation(final LoggerContext context) { + final AsyncAppender appender = context.getConfiguration().getAppender("Async"); + assertTrue(appender.requiresLocation()); + } } diff --git a/log4j-core-test/src/test/resources/log4j-asynch-location.xml b/log4j-core-test/src/test/resources/log4j-asynch-location.xml new file mode 100644 index 00000000000..9da5c4702ff --- /dev/null +++ b/log4j-core-test/src/test/resources/log4j-asynch-location.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/AsyncAppender.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/AsyncAppender.java index 463067ae948..f8487071234 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/AsyncAppender.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/AsyncAppender.java @@ -47,6 +47,7 @@ import org.apache.logging.log4j.core.config.plugins.PluginElement; import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required; import org.apache.logging.log4j.core.filter.AbstractFilterable; +import org.apache.logging.log4j.core.impl.LocationAware; import org.apache.logging.log4j.core.impl.Log4jLogEvent; import org.apache.logging.log4j.spi.AbstractLogger; @@ -471,4 +472,22 @@ public int getQueueRemainingCapacity() { public int getQueueSize() { return queue.size(); } + + @Override + public boolean requiresLocation() { + if (!includeLocation) { + return false; + } + for (final Appender appender : this.getAppenders()) { + if (appender instanceof LocationAware && ((LocationAware) appender).requiresLocation()) { + return true; + } + } + if (errorAppender != null + && errorAppender.getAppender() instanceof LocationAware + && ((LocationAware) errorAppender.getAppender()).requiresLocation()) { + return true; + } + return false; + } } diff --git a/src/changelog/.2.x.x/3257_fix_AsyncAppender_requiresLocation.xml b/src/changelog/.2.x.x/3257_fix_AsyncAppender_requiresLocation.xml new file mode 100644 index 00000000000..c56c76ee401 --- /dev/null +++ b/src/changelog/.2.x.x/3257_fix_AsyncAppender_requiresLocation.xml @@ -0,0 +1,8 @@ + + + + Fix detection of location requirements in `AsyncAppender`. + \ No newline at end of file