Skip to content

Commit e8c86e2

Browse files
jvzeldwrjwt
andauthored
Support AsyncAppender::requiresLocation (#3862)
* Support `AsyncAppender::requiresLocation` This is a port of #3260 to 3.x. This addresses #3257. * Remove unneeded file --------- Co-authored-by: eldwrjwt <[email protected]>
1 parent ebfc571 commit e8c86e2

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-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
@@ -151,4 +151,11 @@ public void testShutdownTimeout(final LoggerContext context) {
151151
context.getLogger("Logger").info("This is a test");
152152
context.stop();
153153
}
154+
155+
@Test
156+
@LoggerContextSource("log4j-asynch-location.xml")
157+
public void testRequiresLocation(final LoggerContext context) {
158+
final AsyncAppender appender = context.getConfiguration().getAppender("Async");
159+
assertTrue(appender.requiresLocation());
160+
}
154161
}
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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,4 +427,9 @@ public int getQueueRemainingCapacity() {
427427
public int getQueueSize() {
428428
return queue.size();
429429
}
430+
431+
@Override
432+
public boolean requiresLocation() {
433+
return includeLocation && dispatcher.requiresLocation();
434+
}
430435
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,13 @@ void stop(final long timeoutMillis) throws InterruptedException {
167167
// Wait for the completion.
168168
join(timeoutMillis);
169169
}
170+
171+
boolean requiresLocation() {
172+
for (var appender : appenders) {
173+
if (appender.getAppender().requiresLocation()) {
174+
return true;
175+
}
176+
}
177+
return errorAppender != null && errorAppender.getAppender().requiresLocation();
178+
}
170179
}

0 commit comments

Comments
 (0)