This is an automated email from the ASF dual-hosted git repository. mattsicker pushed a commit to branch fix/3.x/port-3260 in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
commit ea640be6eab51160dd13653424fc29acec6a299a Author: eldwrjwt <[email protected]> AuthorDate: Mon Jan 6 20:18:29 2025 +0800 Support `AsyncAppender::requiresLocation` This is a port of https://github.com/apache/logging-log4j2/pull/3260 to 3.x. This addresses #3257. --- .../log4j/core/appender/AsyncAppenderTest.java | 7 +++++ .../src/test/resources/log4j-asynch-location.xml | 35 ++++++++++++++++++++++ .../logging/log4j/core/appender/AsyncAppender.java | 5 ++++ .../appender/AsyncAppenderEventDispatcher.java | 9 ++++++ .../3257_fix_AsyncAppender_requiresLocation.xml | 8 +++++ 5 files changed, 64 insertions(+) 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 9631d9a2e0..9bb371eb6c 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 @@ -151,4 +151,11 @@ public class AsyncAppenderTest { 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 0000000000..9da5c4702f --- /dev/null +++ b/log4j-core-test/src/test/resources/log4j-asynch-location.xml @@ -0,0 +1,35 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ 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. + --> +<Configuration status="OFF"> + + <Appenders> + <Console name="STDOUT"> + <PatternLayout pattern="%m%L%n"/> + </Console> + <Async name="Async" includeLocation="true"> + <AppenderRef ref="STDOUT"/> + </Async> + </Appenders> + + <Loggers> + <Root level="debug" includeLocation="true"> + <AppenderRef ref="Async"/> + </Root> + </Loggers> + +</Configuration> 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 d55897a50a..ec7b8b9ee9 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 @@ -427,4 +427,9 @@ public final class AsyncAppender extends AbstractAppender { public int getQueueSize() { return queue.size(); } + + @Override + public boolean requiresLocation() { + return includeLocation && dispatcher.requiresLocation(); + } } diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/AsyncAppenderEventDispatcher.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/AsyncAppenderEventDispatcher.java index b597e1d0c4..1dd00446f4 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/AsyncAppenderEventDispatcher.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/AsyncAppenderEventDispatcher.java @@ -167,4 +167,13 @@ class AsyncAppenderEventDispatcher extends Log4jThread { // Wait for the completion. join(timeoutMillis); } + + boolean requiresLocation() { + for (var appender : appenders) { + if (appender.getAppender().requiresLocation()) { + return true; + } + } + return errorAppender != null && errorAppender.getAppender().requiresLocation(); + } } diff --git a/src/changelog/.3.x.x/3257_fix_AsyncAppender_requiresLocation.xml b/src/changelog/.3.x.x/3257_fix_AsyncAppender_requiresLocation.xml new file mode 100644 index 0000000000..3a885002a7 --- /dev/null +++ b/src/changelog/.3.x.x/3257_fix_AsyncAppender_requiresLocation.xml @@ -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="3257" link="https://github.com/apache/logging-log4j2/issues/3257"/> + <description format="asciidoc">Fix detection of location requirements in `AsyncAppender`.</description> +</entry>
