Skip to content

Add general ConfigurationExtension mechanism #2230

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 2 commits into from
Jan 24, 2024
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 @@ -101,12 +101,14 @@ public void testSingleFilterInvocation() {
.setLevel(Level.INFO)
.setFilter(filter)
.build();
config.initialize();
final Appender appender = mock(Appender.class);
when(appender.isStarted()).thenReturn(true);
when(appender.getName()).thenReturn("test");
config.addAppender(appender, null, null);
final DisruptorConfiguration disruptorConfig = configuration.getExtension(DisruptorConfiguration.class);
final AsyncLoggerConfigDisruptor disruptor =
(AsyncLoggerConfigDisruptor) configuration.getAsyncLoggerConfigDelegate();
(AsyncLoggerConfigDisruptor) disruptorConfig.getAsyncLoggerConfigDelegate();
disruptor.start();
try {
config.log(FQCN, FQCN, null, Level.INFO, new SimpleMessage(), null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,34 @@
*/
package org.apache.logging.log4j.core.async;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.impl.Log4jPropertyKey;
import org.apache.logging.log4j.core.test.junit.ContextSelectorType;
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.SetSystemProperty;

@Tag("async")
@ContextSelectorType(AsyncLoggerContextSelector.class)
@SetSystemProperty(
key = Log4jPropertyKey.Constant.CONFIG_LOCATION,
value = "AsyncWaitStrategyIncorrectFactoryConfigGlobalLoggerTest.xml")
public class AsyncWaitStrategyFactoryIncorrectConfigGlobalLoggersTest {
@LoggerContextSource
public class DisruptorConfigurationInvalidTest {

@Test
public void testIncorrectConfigWaitStrategyFactory() throws Exception {
final LoggerContext context = (LoggerContext) LogManager.getContext(false);
assertTrue(context instanceof AsyncLoggerContext, "context is AsyncLoggerContext");
public void testIncorrectConfigWaitStrategyFactory(final LoggerContext context) {
assertThat(context).isInstanceOf(AsyncLoggerContext.class);

final AsyncWaitStrategyFactory asyncWaitStrategyFactory =
context.getConfiguration().getAsyncWaitStrategyFactory();
assertNull(asyncWaitStrategyFactory);
final DisruptorConfiguration disruptorConfig =
context.getConfiguration().getExtension(DisruptorConfiguration.class);
assertThat(disruptorConfig).isNotNull();
final AsyncWaitStrategyFactory asyncWaitStrategyFactory = disruptorConfig.getWaitStrategyFactory();
assertThat(asyncWaitStrategyFactory).isNull();

final AsyncLogger logger = (AsyncLogger) context.getRootLogger();
final AsyncLoggerDisruptor delegate = logger.getAsyncLoggerDisruptor();
assertEquals(
TimeoutBlockingWaitStrategy.class, delegate.getWaitStrategy().getClass());
assertTrue(
delegate.getWaitStrategy() instanceof TimeoutBlockingWaitStrategy,
"waitstrategy is TimeoutBlockingWaitStrategy");
assertThat(delegate.getWaitStrategy()).isInstanceOf(TimeoutBlockingWaitStrategy.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,29 @@
import org.junit.jupiter.api.Test;

@Tag("async")
public class AsyncWaitStrategyFactoryConfigTest {
public class DisruptorConfigurationTest {

@Test
@LoggerContextSource("AsyncWaitStrategyFactoryConfigTest.xml")
void testAttributePriority() {
final DisruptorConfiguration disruptorConfig = DisruptorConfiguration.newBuilder()
.setFactoryClassName(DefaultAsyncWaitStrategyFactory.class.getName())
.setWaitFactory(YieldingWaitStrategyFactory.class.getName())
.build();
assertThat(disruptorConfig.getWaitStrategyFactory()).isInstanceOf(YieldingWaitStrategyFactory.class);
}

@Test
@LoggerContextSource
public void testConfigWaitStrategyFactory(final LoggerContext context) throws Exception {
final AsyncWaitStrategyFactory asyncWaitStrategyFactory =
context.getConfiguration().getAsyncWaitStrategyFactory();
final DisruptorConfiguration disruptorConfig =
context.getConfiguration().getExtension(DisruptorConfiguration.class);
final AsyncWaitStrategyFactory asyncWaitStrategyFactory = disruptorConfig.getWaitStrategyFactory();
assertThat(asyncWaitStrategyFactory.getClass()).isEqualTo(YieldingWaitStrategyFactory.class);
assertThat(asyncWaitStrategyFactory).isInstanceOf(YieldingWaitStrategyFactory.class);
}

@Test
@LoggerContextSource("AsyncWaitStrategyFactoryConfigTest.xml")
@LoggerContextSource
public void testWaitStrategy(final LoggerContext context) throws Exception {

final org.apache.logging.log4j.Logger logger = context.getRootLogger();
Expand All @@ -56,8 +66,9 @@ public void testWaitStrategy(final LoggerContext context) throws Exception {
@Test
@LoggerContextSource("AsyncWaitStrategyIncorrectFactoryConfigTest.xml")
public void testIncorrectConfigWaitStrategyFactory(final LoggerContext context) throws Exception {
final AsyncWaitStrategyFactory asyncWaitStrategyFactory =
context.getConfiguration().getAsyncWaitStrategyFactory();
final DisruptorConfiguration disruptorConfig =
context.getConfiguration().getExtension(DisruptorConfiguration.class);
final AsyncWaitStrategyFactory asyncWaitStrategyFactory = disruptorConfig.getWaitStrategyFactory();
assertThat(asyncWaitStrategyFactory).isNull(); // because invalid configuration
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,9 @@ protected static void assertAsyncLoggerConfig(final LoggerContext ctx, final int
final Configuration config = ctx.getConfiguration();
assertThat(config).isNotNull();
assertThat(config.getRootLogger()).isInstanceOf(AsyncLoggerConfig.class);
final AsyncLoggerConfigDisruptor disruptor = (AsyncLoggerConfigDisruptor) config.getAsyncLoggerConfigDelegate();
final DisruptorConfiguration disruptorConfig = config.getExtension(DisruptorConfiguration.class);
final AsyncLoggerConfigDisruptor disruptor =
(AsyncLoggerConfigDisruptor) disruptorConfig.getAsyncLoggerConfigDelegate();
assertThat(disruptor.getRingBuffer().getBufferSize()).isEqualTo(expectedBufferSize);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<Configuration name="asyncwait-global2">
<Configuration name="AsyncWaitStrategyFactoryIncorrectConfigGlobalLoggerTest">

<AsyncWaitStrategyFactory
class="nonexisting.Factory" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<Configuration name="async-wait-1" >

<AsyncWaitStrategyFactory
class="org.apache.logging.log4j.core.async.AsyncWaitStrategyFactoryConfigTest$YieldingWaitStrategyFactory" />

<Configuration name="AsyncWaitStrategyFactoryConfigTest" >
<Appenders>
<List name="WaitStrategyAppenderList" />
</Appenders>
Expand All @@ -29,4 +25,7 @@
<AppenderRef ref="WaitStrategyAppenderList"/>
</AsyncRoot>
</Loggers>

<AsyncWaitStrategyFactory
class="org.apache.logging.log4j.core.async.DisruptorConfigurationTest$YieldingWaitStrategyFactory" />
</Configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
public class AsyncLoggerConfig extends LoggerConfig {

private static final ThreadLocal<Boolean> ASYNC_LOGGER_ENTERED = ThreadLocal.withInitial(() -> Boolean.FALSE);
private final AsyncLoggerConfigDelegate delegate;
private AsyncLoggerConfigDelegate delegate;

@PluginFactory
public static <B extends Builder<B>> B newAsyncBuilder() {
Expand Down Expand Up @@ -103,8 +103,17 @@ protected AsyncLoggerConfig(
final boolean includeLocation,
final LogEventFactory logEventFactory) {
super(name, appenders, filter, level, additive, properties, config, includeLocation, logEventFactory);
delegate = config.getAsyncLoggerConfigDelegate();
}

@Override
public void initialize() {
final Configuration configuration = getConfiguration();
final DisruptorConfiguration disruptorConfig = configuration.addExtensionIfAbsent(
DisruptorConfiguration.class,
() -> DisruptorConfiguration.newBuilder().build());
delegate = disruptorConfig.getAsyncLoggerConfigDelegate();
delegate.setLogEventFactory(getLogEventFactory());
super.initialize();
}

protected void log(final LogEvent event, final LoggerConfigPredicate predicate) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,17 @@ public class AsyncLoggerContext extends LoggerContext {

public AsyncLoggerContext(final String name) {
super(name);
loggerDisruptor =
new AsyncLoggerDisruptor(name, () -> getConfiguration().getAsyncWaitStrategyFactory());
loggerDisruptor = new AsyncLoggerDisruptor(name, this::createAsyncWaitStrategyFactory);
}

public AsyncLoggerContext(final String name, final Object externalContext) {
super(name, externalContext);
loggerDisruptor =
new AsyncLoggerDisruptor(name, () -> getConfiguration().getAsyncWaitStrategyFactory());
loggerDisruptor = new AsyncLoggerDisruptor(name, this::createAsyncWaitStrategyFactory);
}

public AsyncLoggerContext(final String name, final Object externalContext, final URI configLocn) {
super(name, externalContext, configLocn);
loggerDisruptor =
new AsyncLoggerDisruptor(name, () -> getConfiguration().getAsyncWaitStrategyFactory());
loggerDisruptor = new AsyncLoggerDisruptor(name, this::createAsyncWaitStrategyFactory);
}

public AsyncLoggerContext(
Expand All @@ -57,14 +54,12 @@ public AsyncLoggerContext(
final URI configLocn,
final ConfigurableInstanceFactory instanceFactory) {
super(name, externalContext, configLocn, instanceFactory);
loggerDisruptor =
new AsyncLoggerDisruptor(name, () -> getConfiguration().getAsyncWaitStrategyFactory());
loggerDisruptor = new AsyncLoggerDisruptor(name, this::createAsyncWaitStrategyFactory);
}

public AsyncLoggerContext(final String name, final Object externalContext, final String configLocn) {
super(name, externalContext, configLocn);
loggerDisruptor =
new AsyncLoggerDisruptor(name, () -> getConfiguration().getAsyncWaitStrategyFactory());
loggerDisruptor = new AsyncLoggerDisruptor(name, this::createAsyncWaitStrategyFactory);
}

public AsyncLoggerContext(
Expand All @@ -73,8 +68,13 @@ public AsyncLoggerContext(
final String configLocn,
final ConfigurableInstanceFactory instanceFactory) {
super(name, externalContext, configLocn, instanceFactory);
loggerDisruptor =
new AsyncLoggerDisruptor(name, () -> getConfiguration().getAsyncWaitStrategyFactory());
loggerDisruptor = new AsyncLoggerDisruptor(name, this::createAsyncWaitStrategyFactory);
}

private AsyncWaitStrategyFactory createAsyncWaitStrategyFactory() {
final DisruptorConfiguration disruptorConfiguration =
getConfiguration().getExtension(DisruptorConfiguration.class);
return disruptorConfiguration != null ? disruptorConfiguration.getWaitStrategyFactory() : null;
}

@Override
Expand Down

This file was deleted.

Loading