Skip to content

Commit 4e732f1

Browse files
committed
Remove instance of checks for async components
This PR removes all occurences of `instanceof Async*` that might prevent the separation of the async support. The removal requires a couple of new API methods and binary incompatible changes.
1 parent f5af863 commit 4e732f1

File tree

5 files changed

+36
-69
lines changed

5 files changed

+36
-69
lines changed

log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,4 +939,11 @@ public String toString() {
939939
protected Logger newInstance(final LoggerContext ctx, final String name, final MessageFactory messageFactory) {
940940
return new Logger(ctx, name, messageFactory);
941941
}
942+
943+
/**
944+
* If {@code true} loggers will include location by default.
945+
*/
946+
public boolean includeLocation() {
947+
return true;
948+
}
942949
}

log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerConfig.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.List;
2020
import java.util.concurrent.TimeUnit;
21+
import java.util.function.Predicate;
2122
import org.apache.logging.log4j.Level;
2223
import org.apache.logging.log4j.LogManager;
2324
import org.apache.logging.log4j.core.Filter;
@@ -79,6 +80,7 @@ public LoggerConfig build() {
7980
final String name = getLoggerName().equals(ROOT) ? Strings.EMPTY : getLoggerName();
8081
final LevelAndRefs container =
8182
LoggerConfig.getLevelAndRefs(getLevel(), getRefs(), getLevelAndRefs(), getConfig());
83+
final String includeLocationConfigValue = getIncludeLocation();
8284
return new AsyncLoggerConfig(
8385
name,
8486
container.refs,
@@ -87,7 +89,7 @@ public LoggerConfig build() {
8789
isAdditivity(),
8890
getProperties(),
8991
getConfig(),
90-
includeLocation(getIncludeLocation()),
92+
Boolean.parseBoolean(includeLocationConfigValue),
9193
getLogEventFactory());
9294
}
9395
}
@@ -116,9 +118,9 @@ public void initialize() {
116118
super.initialize();
117119
}
118120

119-
protected void log(final LogEvent event, final LoggerConfigPredicate predicate) {
121+
protected void log(final LogEvent event, final Predicate<LoggerConfig> predicate) {
120122
// See LOG4J2-2301
121-
if (predicate == LoggerConfigPredicate.ALL
123+
if (predicate == null
122124
&& ASYNC_LOGGER_ENTERED.get() == Boolean.FALSE
123125
&&
124126
// Optimization: AsyncLoggerConfig is identical to LoggerConfig
@@ -132,7 +134,7 @@ protected void log(final LogEvent event, final LoggerConfigPredicate predicate)
132134
if (!isFiltered(event)) {
133135
// Detect the first time we encounter an AsyncLoggerConfig. We must log
134136
// to all non-async loggers first.
135-
processLogEvent(event, LoggerConfigPredicate.SYNCHRONOUS_ONLY);
137+
processLogEvent(event, lc -> !(lc instanceof AsyncLoggerConfig));
136138
// Then pass the event to the background thread where
137139
// all async logging is executed. It is important this
138140
// happens at most once and after all synchronous loggers
@@ -206,7 +208,7 @@ void logInBackgroundThread(final LogEvent event) {
206208
*/
207209
void logToAsyncLoggerConfigsOnCurrentThread(final LogEvent event) {
208210
// skip the filter, which was already called on the logging thread
209-
processLogEvent(event, LoggerConfigPredicate.ASYNCHRONOUS_ONLY);
211+
processLogEvent(event, lc -> lc instanceof AsyncLoggerConfig);
210212
}
211213

212214
private String displayName() {
@@ -228,11 +230,6 @@ public boolean stop(final long timeout, final TimeUnit timeUnit) {
228230
return true;
229231
}
230232

231-
// Note: for asynchronous loggers, includeLocation default is FALSE
232-
protected static boolean includeLocation(final String includeLocationConfigValue) {
233-
return Boolean.parseBoolean(includeLocationConfigValue);
234-
}
235-
236233
/**
237234
* An asynchronous root Logger.
238235
*/
@@ -251,6 +248,7 @@ public static class Builder<B extends Builder<B>> extends RootLogger.Builder<B>
251248
public LoggerConfig build() {
252249
final LevelAndRefs container =
253250
LoggerConfig.getLevelAndRefs(getLevel(), getRefs(), getLevelAndRefs(), getConfig());
251+
final String includeLocationConfigValue = getIncludeLocation();
254252
return new AsyncLoggerConfig(
255253
LogManager.ROOT_LOGGER_NAME,
256254
container.refs,
@@ -259,7 +257,7 @@ public LoggerConfig build() {
259257
isAdditivity(),
260258
getProperties(),
261259
getConfig(),
262-
AsyncLoggerConfig.includeLocation(getIncludeLocation()),
260+
Boolean.parseBoolean(includeLocationConfigValue),
263261
getLogEventFactory());
264262
}
265263
}

log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerContext.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ public boolean stop(final long timeout, final TimeUnit timeUnit) {
130130
return true;
131131
}
132132

133+
@Override
134+
public boolean includeLocation() {
135+
return false;
136+
}
137+
133138
// package-protected for tests
134139
AsyncLoggerDisruptor getAsyncLoggerDisruptor() {
135140
return loggerDisruptor;

log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerContextSelector.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@
1818

1919
import java.net.URI;
2020
import org.apache.logging.log4j.core.LoggerContext;
21-
import org.apache.logging.log4j.core.impl.Log4jPropertyKey;
2221
import org.apache.logging.log4j.core.selector.ClassLoaderContextSelector;
2322
import org.apache.logging.log4j.plugins.Inject;
2423
import org.apache.logging.log4j.plugins.Singleton;
2524
import org.apache.logging.log4j.plugins.di.ConfigurableInstanceFactory;
26-
import org.apache.logging.log4j.util.PropertiesUtil;
2725

2826
/**
2927
* {@code ContextSelector} that manages {@code AsyncLoggerContext} instances.
@@ -33,19 +31,6 @@
3331
@Singleton
3432
public class AsyncLoggerContextSelector extends ClassLoaderContextSelector {
3533

36-
/**
37-
* Returns {@code true} if the user specified this selector as the Log4jContextSelector, to make all loggers
38-
* asynchronous.
39-
*
40-
* @return {@code true} if all loggers are asynchronous, {@code false} otherwise.
41-
*/
42-
public static boolean isSelected() {
43-
// FIXME(ms): this should check Injector bindings
44-
return AsyncLoggerContextSelector.class
45-
.getName()
46-
.equals(PropertiesUtil.getProperties().getStringProperty(Log4jPropertyKey.CONTEXT_SELECTOR_CLASS_NAME));
47-
}
48-
4934
@Inject
5035
public AsyncLoggerContextSelector(final ConfigurableInstanceFactory instanceFactory) {
5136
super(instanceFactory);

log4j-core/src/main/java/org/apache/logging/log4j/core/config/LoggerConfig.java

Lines changed: 15 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,14 @@
2020
import java.util.Arrays;
2121
import java.util.List;
2222
import java.util.Map;
23+
import java.util.function.Predicate;
2324
import org.apache.logging.log4j.Level;
2425
import org.apache.logging.log4j.LogManager;
2526
import org.apache.logging.log4j.Marker;
2627
import org.apache.logging.log4j.core.Appender;
2728
import org.apache.logging.log4j.core.Filter;
2829
import org.apache.logging.log4j.core.LogEvent;
2930
import org.apache.logging.log4j.core.LoggerContext;
30-
import org.apache.logging.log4j.core.async.AsyncLoggerConfig;
31-
import org.apache.logging.log4j.core.async.AsyncLoggerContext;
32-
import org.apache.logging.log4j.core.async.AsyncLoggerContextSelector;
3331
import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
3432
import org.apache.logging.log4j.core.filter.AbstractFilterable;
3533
import org.apache.logging.log4j.core.impl.Log4jLogEvent;
@@ -495,7 +493,7 @@ public void log(
495493
final LogEvent logEvent =
496494
logEventFactory.createEvent(loggerName, marker, fqcn, location(fqcn), level, data, props, t);
497495
try {
498-
log(logEvent, LoggerConfigPredicate.ALL);
496+
log(logEvent, null);
499497
} finally {
500498
// LOG4J2-1583 prevent scrambled logs when logging calls are nested (logging in toString())
501499
logEventFactory.recycle(logEvent);
@@ -530,7 +528,7 @@ public void log(
530528
final LogEvent logEvent =
531529
logEventFactory.createEvent(loggerName, marker, fqcn, location, level, data, props, t);
532530
try {
533-
log(logEvent, LoggerConfigPredicate.ALL);
531+
log(logEvent, null);
534532
} finally {
535533
// LOG4J2-1583 prevent scrambled logs when logging calls are nested (logging in toString())
536534
logEventFactory.recycle(logEvent);
@@ -584,17 +582,17 @@ private List<Property> getPropertiesWithLookups(
584582
* @param event The log event.
585583
*/
586584
public void log(final LogEvent event) {
587-
log(event, LoggerConfigPredicate.ALL);
585+
log(event, null);
588586
}
589587

590588
/**
591589
* Logs an event.
592590
*
593591
* @param event The log event.
594-
* @param predicate predicate for which LoggerConfig instances to append to. A
595-
* {@literal null} value is equivalent to a true predicate.
592+
* @param predicate predicate for which LoggerConfig instances to append to.
593+
* Use a {@literal null} value instead of a true predicate.
596594
*/
597-
protected void log(final LogEvent event, final LoggerConfigPredicate predicate) {
595+
protected void log(final LogEvent event, final Predicate<LoggerConfig> predicate) {
598596
if (!isFiltered(event)) {
599597
processLogEvent(event, predicate);
600598
}
@@ -614,12 +612,12 @@ public ReliabilityStrategy getReliabilityStrategy() {
614612
* Logs an event, bypassing filters.
615613
*
616614
* @param event The log event.
617-
* @param predicate predicate for which LoggerConfig instances to append to. A
618-
* {@literal null} value is equivalent to a true predicate.
615+
* @param predicate predicate for which LoggerConfig instances to append to.
616+
* Use a {@literal null} value instead of a true predicate.
619617
*/
620-
protected void processLogEvent(final LogEvent event, final LoggerConfigPredicate predicate) {
618+
protected void processLogEvent(final LogEvent event, final Predicate<LoggerConfig> predicate) {
621619
event.setIncludeLocation(isIncludeLocation());
622-
if (predicate == null || predicate.allow(this)) {
620+
if (predicate == null || predicate.test(this)) {
623621
callAppenders(event);
624622
}
625623
logParent(event, predicate);
@@ -649,7 +647,7 @@ public boolean requiresLocation() {
649647
return false;
650648
}
651649

652-
private void logParent(final LogEvent event, final LoggerConfigPredicate predicate) {
650+
private void logParent(final LogEvent event, final Predicate<LoggerConfig> predicate) {
653651
if (additive && parent != null) {
654652
parent.log(event, predicate);
655653
}
@@ -674,14 +672,11 @@ public String toString() {
674672
protected static boolean includeLocation(
675673
final String includeLocationConfigValue, final Configuration configuration) {
676674
if (includeLocationConfigValue == null) {
677-
LoggerContext context = null;
678675
if (configuration != null) {
679-
context = configuration.getLoggerContext();
680-
}
681-
if (context != null) {
682-
return !(context instanceof AsyncLoggerContext);
676+
final LoggerContext context = configuration.getLoggerContext();
677+
return context != null ? context.includeLocation() : false;
683678
} else {
684-
return !AsyncLoggerContextSelector.isSelected();
679+
return false;
685680
}
686681
}
687682
return Boolean.parseBoolean(includeLocationConfigValue);
@@ -860,27 +855,4 @@ protected static class LevelAndRefs {
860855
public Level level;
861856
public List<AppenderRef> refs;
862857
}
863-
864-
protected enum LoggerConfigPredicate {
865-
ALL() {
866-
@Override
867-
boolean allow(final LoggerConfig config) {
868-
return true;
869-
}
870-
},
871-
ASYNCHRONOUS_ONLY() {
872-
@Override
873-
boolean allow(final LoggerConfig config) {
874-
return config instanceof AsyncLoggerConfig;
875-
}
876-
},
877-
SYNCHRONOUS_ONLY() {
878-
@Override
879-
boolean allow(final LoggerConfig config) {
880-
return !ASYNCHRONOUS_ONLY.allow(config);
881-
}
882-
};
883-
884-
abstract boolean allow(LoggerConfig config);
885-
}
886858
}

0 commit comments

Comments
 (0)