Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add requiresLocation function for AsyncAppender (apache#3257)
Browse files Browse the repository at this point in the history
eldwrjwt committed Dec 1, 2024
1 parent e1715dc commit 68380f1
Showing 4 changed files with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -173,4 +173,11 @@ public void testShutdownTimeout(final LoggerContext context) {
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());
}
}
35 changes: 35 additions & 0 deletions log4j-core-test/src/test/resources/log4j-asynch-location.xml
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -47,6 +47,7 @@
import org.apache.logging.log4j.core.config.plugins.PluginElement;
import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
import org.apache.logging.log4j.core.filter.AbstractFilterable;
import org.apache.logging.log4j.core.impl.LocationAware;
import org.apache.logging.log4j.core.impl.Log4jLogEvent;
import org.apache.logging.log4j.spi.AbstractLogger;

@@ -471,4 +472,22 @@ public int getQueueRemainingCapacity() {
public int getQueueSize() {
return queue.size();
}

@Override
public boolean requiresLocation() {
if (!includeLocation) {
return false;
}
for (final Appender appender : this.getAppenders()) {
if (appender instanceof LocationAware && ((LocationAware) appender).requiresLocation()) {
return true;
}
}
if (errorAppender != null
&& errorAppender.getAppender() instanceof LocationAware
&& ((LocationAware) errorAppender.getAppender()).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 `AsyncAppender`.</description>
</entry>

0 comments on commit 68380f1

Please sign in to comment.