Skip to content
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

Issue #1550: remove multiple isFiltered invocations #1552

Merged
merged 1 commit into from
Sep 15, 2023
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 @@ -20,23 +20,36 @@
import java.io.File;
import java.io.FileReader;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.config.AppenderRef;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.ConfigurationFactory;
import org.apache.logging.log4j.core.config.DefaultConfiguration;
import org.apache.logging.log4j.core.config.LoggerConfig;
import org.apache.logging.log4j.core.config.NullConfiguration;
import org.apache.logging.log4j.core.test.CoreLoggerContexts;
import org.apache.logging.log4j.core.test.categories.AsyncLoggers;
import org.apache.logging.log4j.message.SimpleMessage;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@Category(AsyncLoggers.class)
public class AsyncLoggerConfigTest {

private static final String FQCN = AsyncLoggerConfigTest.class.getName();

@BeforeClass
public static void beforeClass() {
System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, "AsyncLoggerConfigTest.xml");
Expand Down Expand Up @@ -81,4 +94,29 @@ public void testIncludeLocationDefaultsToFalse() {
assertFalse("Include location should default to false for async loggers",
loggerConfig.isIncludeLocation());
}

@Test
public void testSingleFilterInvocation() {
final Configuration configuration = new NullConfiguration();
final Filter filter = mock(Filter.class);
final LoggerConfig config = AsyncLoggerConfig.newAsyncBuilder()
.withLoggerName(FQCN)
.withConfig(configuration)
.withLevel(Level.INFO)
.withtFilter(filter)
.build();
final Appender appender = mock(Appender.class);
when(appender.isStarted()).thenReturn(true);
when(appender.getName()).thenReturn("test");
config.addAppender(appender, null, null);
final AsyncLoggerConfigDisruptor disruptor = (AsyncLoggerConfigDisruptor) configuration.getAsyncLoggerConfigDelegate();
disruptor.start();
try {
config.log(FQCN, FQCN, null, Level.INFO, new SimpleMessage(), null);
verify(appender, times(1)).append(any());
verify(filter, times(1)).filter(any());
} finally {
disruptor.stop();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,29 @@
import java.util.concurrent.atomic.AtomicReference;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.impl.Log4jLogEvent.Builder;
import org.apache.logging.log4j.message.SimpleMessage;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
* Tests for LoggerConfig.
*/
public class LoggerConfigTest {

private static final String FQCN = LoggerConfigTest.class.getName();

private static LoggerConfig createForProperties(final Property[] properties) {
return LoggerConfig.createLogger(true, Level.INFO, "name", "false", new AppenderRef[0], properties,
new NullConfiguration(), null);
Expand Down Expand Up @@ -109,4 +121,24 @@ public void testLevel() {
assertEquals(config2.getLevel(), Level.ERROR, "Unexpected Level");
assertNull(config2.getExplicitLevel(),"Unexpected explicit level");
}

@Test
public void testSingleFilterInvocation() {
final Configuration configuration = new NullConfiguration();
final Filter filter = mock(Filter.class);
final LoggerConfig config = LoggerConfig.newBuilder()
.withLoggerName(FQCN)
.withConfig(configuration)
.withLevel(Level.INFO)
.withtFilter(filter)
.build();
final Appender appender = mock(Appender.class);
when(appender.isStarted()).thenReturn(true);
when(appender.getName()).thenReturn("test");
config.addAppender(appender, null, null);

config.log(FQCN, FQCN, null, Level.INFO, new SimpleMessage(), null);
verify(appender, times(1)).append(any());
verify(filter, times(1)).filter(any());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,17 @@ protected void log(final LogEvent event, final LoggerConfigPredicate predicate)
// This is the first AsnycLoggerConfig encountered by this LogEvent
ASYNC_LOGGER_ENTERED.set(Boolean.TRUE);
try {
// Detect the first time we encounter an AsyncLoggerConfig. We must log
// to all non-async loggers first.
super.log(event, LoggerConfigPredicate.SYNCHRONOUS_ONLY);
// Then pass the event to the background thread where
// all async logging is executed. It is important this
// happens at most once and after all synchronous loggers
// have been invoked, because we lose parameter references
// from reusable messages.
logToAsyncDelegate(event);
if (!isFiltered(event)) {
// Detect the first time we encounter an AsyncLoggerConfig. We must log
// to all non-async loggers first.
processLogEvent(event, LoggerConfigPredicate.SYNCHRONOUS_ONLY);
// Then pass the event to the background thread where
// all async logging is executed. It is important this
// happens at most once and after all synchronous loggers
// have been invoked, because we lose parameter references
// from reusable messages.
logToAsyncDelegate(event);
}
} finally {
ASYNC_LOGGER_ENTERED.set(Boolean.FALSE);
}
Expand All @@ -149,13 +151,11 @@ protected void callAppenders(final LogEvent event) {
}

private void logToAsyncDelegate(final LogEvent event) {
if (!isFiltered(event)) {
// Passes on the event to a separate thread that will call
// asyncCallAppenders(LogEvent).
populateLazilyInitializedFields(event);
if (!delegate.tryEnqueue(event, this)) {
handleQueueFull(event);
}
// Passes on the event to a separate thread that will call
// asyncCallAppenders(LogEvent).
populateLazilyInitializedFields(event);
if (!delegate.tryEnqueue(event, this)) {
handleQueueFull(event);
}
}

Expand Down Expand Up @@ -187,7 +187,8 @@ void logInBackgroundThread(final LogEvent event) {
* default {@link LoggerConfig} definitions), which will be invoked on the <b>calling thread</b>.
*/
void logToAsyncLoggerConfigsOnCurrentThread(final LogEvent event) {
log(event, LoggerConfigPredicate.ASYNCHRONOUS_ONLY);
// skip the filter, which was already called on the logging thread
processLogEvent(event, LoggerConfigPredicate.ASYNCHRONOUS_ONLY);
}

private String displayName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -611,9 +611,9 @@ public void log(final LogEvent event) {
/**
* Logs an event.
*
* @param event The log event.
* @param predicate predicate for which LoggerConfig instances to append to.
* A null value is equivalent to a true predicate.
* @param event The log event.
* @param predicate predicate for which LoggerConfig instances to append to. A
* {@literal null} value is equivalent to a true predicate.
*/
protected void log(final LogEvent event, final LoggerConfigPredicate predicate) {
if (!isFiltered(event)) {
Expand All @@ -631,9 +631,16 @@ public ReliabilityStrategy getReliabilityStrategy() {
return reliabilityStrategy;
}

private void processLogEvent(final LogEvent event, final LoggerConfigPredicate predicate) {
/**
* Logs an event, bypassing filters.
*
* @param event The log event.
* @param predicate predicate for which LoggerConfig instances to append to. A
* {@literal null} value is equivalent to a true predicate.
*/
protected void processLogEvent(final LogEvent event, final LoggerConfigPredicate predicate) {
event.setIncludeLocation(isIncludeLocation());
if (predicate.allow(this)) {
if (predicate == null || predicate.allow(this)) {
callAppenders(event);
}
logParent(event, predicate);
Expand Down
25 changes: 25 additions & 0 deletions src/changelog/.2.x.x/1550_multiple_filter_invocations.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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.
-->
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://logging.apache.org/log4j/changelog"
xsi:schemaLocation="http://logging.apache.org/log4j/changelog https://logging.apache.org/log4j/changelog-0.1.1.xsd"
type="changed">
<issue id="1550" link="https://github.com/apache/logging-log4j2/pull/1550"/>
<author id="github:ppkarwasz"/>
<description format="asciidoc">Removes additional `isFiltered` checks in `AsyncLoggerConfig`.</description>
</entry>