Skip to content

Commit 87147db

Browse files
committed
Add requiresLocation function for FailoverAppender (#3257)
1 parent e1715dc commit 87147db

File tree

4 files changed

+83
-23
lines changed

4 files changed

+83
-23
lines changed

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

+18-23
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static org.junit.jupiter.api.Assertions.assertEquals;
2020
import static org.junit.jupiter.api.Assertions.assertNotNull;
21+
import static org.junit.jupiter.api.Assertions.assertTrue;
2122

2223
import java.util.List;
2324
import org.apache.logging.log4j.Logger;
@@ -27,33 +28,14 @@
2728
import org.apache.logging.log4j.core.test.appender.ListAppender;
2829
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
2930
import org.apache.logging.log4j.core.test.junit.Named;
30-
import org.junit.jupiter.api.AfterEach;
3131
import org.junit.jupiter.api.Test;
3232

33-
@LoggerContextSource("log4j-failover.xml")
3433
public class FailoverAppenderTest {
35-
private final ListAppender app;
36-
private final FailOnceAppender foApp;
37-
private final Logger logger;
38-
private final Logger onceLogger;
39-
40-
public FailoverAppenderTest(
41-
final LoggerContext context,
42-
@Named("List") final ListAppender app,
43-
@Named("Once") final FailOnceAppender foApp) {
44-
this.app = app;
45-
this.foApp = foApp;
46-
logger = context.getLogger("LoggerTest");
47-
onceLogger = context.getLogger("Once");
48-
}
49-
50-
@AfterEach
51-
public void tearDown() throws Exception {
52-
app.clear();
53-
}
5434

5535
@Test
56-
public void testFailover() {
36+
@LoggerContextSource("log4j-failover.xml")
37+
public void testFailover(final LoggerContext context, @Named("List") final ListAppender app) {
38+
final Logger logger = context.getLogger("LoggerTest");
5739
logger.error("This is a test");
5840
List<LogEvent> events = app.getEvents();
5941
assertNotNull(events);
@@ -66,7 +48,13 @@ public void testFailover() {
6648
}
6749

6850
@Test
69-
public void testRecovery() throws Exception {
51+
@LoggerContextSource("log4j-failover.xml")
52+
public void testRecovery(
53+
final LoggerContext context,
54+
@Named("List") final ListAppender app,
55+
@Named("Once") final FailOnceAppender foApp)
56+
throws Exception {
57+
final Logger onceLogger = context.getLogger("Once");
7058
onceLogger.error("Fail once");
7159
onceLogger.error("Fail again");
7260
List<LogEvent> events = app.getEvents();
@@ -81,4 +69,11 @@ public void testRecovery() throws Exception {
8169
events = foApp.drainEvents();
8270
assertEquals(events.size(), 2, "Incorrect number of events in primary appender");
8371
}
72+
73+
@Test
74+
@LoggerContextSource("log4j-failover-location.xml")
75+
public void testRequiresLocation(final LoggerContext context) {
76+
final FailoverAppender appender = context.getConfiguration().getAppender("Failover");
77+
assertTrue(appender.requiresLocation());
78+
}
8479
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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" name="RoutingTest">
19+
20+
<Appenders>
21+
<Console name="CONSOLE">
22+
<PatternLayout pattern="%m%L%n"/>
23+
</Console>
24+
<Console name="CONSOLE2">
25+
<PatternLayout pattern="%m%L%n"/>
26+
</Console>
27+
<Failover name="Failover" primary="CONSOLE">
28+
<Failovers>
29+
<AppenderRef ref="CONSOLE2"/>
30+
</Failovers>
31+
</Failover>
32+
</Appenders>
33+
34+
<Loggers>
35+
<Root level="debug" includeLocation="true">
36+
<AppenderRef ref="Failover"/>
37+
</Root>
38+
</Loggers>
39+
40+
</Configuration>

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

+17
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
3535
import org.apache.logging.log4j.core.config.plugins.PluginElement;
3636
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
37+
import org.apache.logging.log4j.core.impl.LocationAware;
3738
import org.apache.logging.log4j.core.util.Booleans;
3839
import org.apache.logging.log4j.core.util.Constants;
3940

@@ -223,4 +224,20 @@ public static FailoverAppender createAppender(
223224
return new FailoverAppender(
224225
name, filter, primary, failovers, retryIntervalMillis, config, ignoreExceptions, null);
225226
}
227+
228+
@Override
229+
public boolean requiresLocation() {
230+
if (primary != null
231+
&& primary.getAppender() instanceof LocationAware
232+
&& ((LocationAware) primary.getAppender()).requiresLocation()) {
233+
return true;
234+
}
235+
for (final AppenderControl control : failoverAppenders) {
236+
final Appender appender = control.getAppender();
237+
if (appender instanceof LocationAware && ((LocationAware) appender).requiresLocation()) {
238+
return true;
239+
}
240+
}
241+
return false;
242+
}
226243
}
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 `FailoverAppender`.</description>
8+
</entry>

0 commit comments

Comments
 (0)