Skip to content

Commit 3fa5de4

Browse files
committed
Add requiresLocation function for AsyncAppender (#3257)
1 parent e1715dc commit 3fa5de4

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed

log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/AsyncAppenderTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,11 @@ public void testShutdownTimeout(final LoggerContext context) {
173173
context.getLogger("Logger").info("This is a test");
174174
context.stop();
175175
}
176+
177+
@Test
178+
@LoggerContextSource("log4j-asynch-location.xml")
179+
public void testRequiresLocation(final LoggerContext context) {
180+
final AsyncAppender appender = context.getConfiguration().getAppender("Async");
181+
assertTrue(appender.requiresLocation());
182+
}
176183
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one or more
4+
~ contributor license agreements. See the NOTICE file distributed with
5+
~ this work for additional information regarding copyright ownership.
6+
~ The ASF licenses this file to you under the Apache License, Version 2.0
7+
~ (the "License"); you may not use this file except in compliance with
8+
~ the License. You may obtain a copy of the License at
9+
~
10+
~ http://www.apache.org/licenses/LICENSE-2.0
11+
~
12+
~ Unless required by applicable law or agreed to in writing, software
13+
~ distributed under the License is distributed on an "AS IS" BASIS,
14+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
~ See the License for the specific language governing permissions and
16+
~ limitations under the License.
17+
-->
18+
<Configuration status="OFF">
19+
20+
<Appenders>
21+
<Console name="STDOUT">
22+
<PatternLayout pattern="%m%L%n"/>
23+
</Console>
24+
<Async name="Async" includeLocation="true">
25+
<AppenderRef ref="STDOUT"/>
26+
</Async>
27+
</Appenders>
28+
29+
<Loggers>
30+
<Root level="debug" includeLocation="true">
31+
<AppenderRef ref="Async"/>
32+
</Root>
33+
</Loggers>
34+
35+
</Configuration>

log4j-core/src/main/java/org/apache/logging/log4j/core/appender/AsyncAppender.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.apache.logging.log4j.core.config.plugins.PluginElement;
4848
import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
4949
import org.apache.logging.log4j.core.filter.AbstractFilterable;
50+
import org.apache.logging.log4j.core.impl.LocationAware;
5051
import org.apache.logging.log4j.core.impl.Log4jLogEvent;
5152
import org.apache.logging.log4j.spi.AbstractLogger;
5253

@@ -471,4 +472,22 @@ public int getQueueRemainingCapacity() {
471472
public int getQueueSize() {
472473
return queue.size();
473474
}
475+
476+
@Override
477+
public boolean requiresLocation() {
478+
if (!includeLocation) {
479+
return false;
480+
}
481+
for (final Appender appender : this.getAppenders()) {
482+
if (appender instanceof LocationAware && ((LocationAware) appender).requiresLocation()) {
483+
return true;
484+
}
485+
}
486+
if (errorAppender != null
487+
&& errorAppender.getAppender() instanceof LocationAware
488+
&& ((LocationAware) errorAppender.getAppender()).requiresLocation()) {
489+
return true;
490+
}
491+
return false;
492+
}
474493
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="https://logging.apache.org/xml/ns"
4+
xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
5+
type="fixed">
6+
<issue id="3257" link="https://github.com/apache/logging-log4j2/issues/3257"/>
7+
<description format="asciidoc">Fix detection of location requirements in `AsyncAppender`.</description>
8+
</entry>

0 commit comments

Comments
 (0)