Skip to content

Add requiresLocation function for FailoverAppender #3257 #3259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.List;
import org.apache.logging.log4j.Logger;
Expand All @@ -27,33 +28,14 @@
import org.apache.logging.log4j.core.test.appender.ListAppender;
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
import org.apache.logging.log4j.core.test.junit.Named;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

@LoggerContextSource("log4j-failover.xml")
public class FailoverAppenderTest {
private final ListAppender app;
private final FailOnceAppender foApp;
private final Logger logger;
private final Logger onceLogger;

public FailoverAppenderTest(
final LoggerContext context,
@Named("List") final ListAppender app,
@Named("Once") final FailOnceAppender foApp) {
this.app = app;
this.foApp = foApp;
logger = context.getLogger("LoggerTest");
onceLogger = context.getLogger("Once");
}

@AfterEach
void tearDown() {
app.clear();
}

@Test
void testFailover() {
@LoggerContextSource("log4j-failover.xml")
void testFailover(final LoggerContext context, @Named("List") final ListAppender app) {
final Logger logger = context.getLogger("LoggerTest");
logger.error("This is a test");
List<LogEvent> events = app.getEvents();
assertNotNull(events);
Expand All @@ -66,7 +48,13 @@ void testFailover() {
}

@Test
void testRecovery() throws Exception {
@LoggerContextSource("log4j-failover.xml")
void testRecovery(
final LoggerContext context,
@Named("List") final ListAppender app,
@Named("Once") final FailOnceAppender foApp)
throws Exception {
final Logger onceLogger = context.getLogger("Once");
onceLogger.error("Fail once");
onceLogger.error("Fail again");
List<LogEvent> events = app.getEvents();
Expand All @@ -81,4 +69,11 @@ void testRecovery() throws Exception {
events = foApp.drainEvents();
assertEquals(2, events.size(), "Incorrect number of events in primary appender");
}

@Test
@LoggerContextSource("log4j-failover-location.xml")
void testRequiresLocation(final LoggerContext context) {
final FailoverAppender appender = context.getConfiguration().getAppender("Failover");
assertTrue(appender.requiresLocation());
}
}
40 changes: 40 additions & 0 deletions log4j-core-test/src/test/resources/log4j-failover-location.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?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" name="RoutingTest">

<Appenders>
<Console name="CONSOLE">
<PatternLayout pattern="%m%L%n"/>
</Console>
<Console name="CONSOLE2">
<PatternLayout pattern="%m%L%n"/>
</Console>
<Failover name="Failover" primary="CONSOLE">
<Failovers>
<AppenderRef ref="CONSOLE2"/>
</Failovers>
</Failover>
</Appenders>

<Loggers>
<Root level="debug" includeLocation="true">
<AppenderRef ref="Failover"/>
</Root>
</Loggers>

</Configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
import org.apache.logging.log4j.core.config.plugins.PluginElement;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
import org.apache.logging.log4j.core.impl.LocationAware;
import org.apache.logging.log4j.core.util.Booleans;
import org.apache.logging.log4j.core.util.Constants;

Expand Down Expand Up @@ -223,4 +224,20 @@ public static FailoverAppender createAppender(
return new FailoverAppender(
name, filter, primary, failovers, retryIntervalMillis, config, ignoreExceptions, null);
}

@Override
public boolean requiresLocation() {
if (primary != null
&& primary.getAppender() instanceof LocationAware
&& ((LocationAware) primary.getAppender()).requiresLocation()) {
return true;
}
for (final AppenderControl control : failoverAppenders) {
final Appender appender = control.getAppender();
if (appender instanceof LocationAware && ((LocationAware) appender).requiresLocation()) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -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 `FailoverAppender`.</description>
</entry>
Loading