Skip to content

Commit 43a3630

Browse files
committed
use the class loader that loaded LoggerFactory (instead of the threadContextClassLoader) to find providers
Signed-off-by: Ceki Gulcu <ceki@qos.ch>
1 parent 557bf7c commit 43a3630

File tree

3 files changed

+14
-226
lines changed

3 files changed

+14
-226
lines changed

slf4j-api/src/main/java/org/slf4j/LoggerFactory.java

+14-14
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@
2626

2727
import java.io.IOException;
2828
import java.net.URL;
29-
import java.security.AccessControlException;
30-
import java.security.AccessController;
31-
import java.security.PrivilegedAction;
3229
import java.util.ArrayList;
3330
import java.util.Arrays;
3431
import java.util.Enumeration;
@@ -104,24 +101,27 @@ public final class LoggerFactory {
104101

105102
// Package access for tests
106103
static List<SLF4JServiceProvider> findServiceProviders() {
107-
final ClassLoader cl = LoggerFactory.class.getClassLoader();
108-
final PrivilegedAction<ServiceLoader<SLF4JServiceProvider>> action = () -> ServiceLoader.load(SLF4JServiceProvider.class, cl);
109-
final ServiceLoader<SLF4JServiceProvider> serviceLoader = System.getSecurityManager() != null
110-
? AccessController.doPrivileged(action)
111-
: action.run();
104+
// retain behaviour similar to that of 1.7 series and earlier. More specifically, use the class loader that
105+
// loaded the present class to search for services
106+
final ClassLoader classLoaderOfLoggerFactory = LoggerFactory.class.getClassLoader();
107+
ServiceLoader<SLF4JServiceProvider> serviceLoader = ServiceLoader.load(SLF4JServiceProvider.class, classLoaderOfLoggerFactory);
112108
List<SLF4JServiceProvider> providerList = new ArrayList<>();
113109
Iterator<SLF4JServiceProvider> iterator = serviceLoader.iterator();
114110
while (iterator.hasNext()) {
115-
try {
116-
providerList.add(iterator.next());
117-
} catch (ServiceConfigurationError | AccessControlException e) {
118-
// Short warning
119-
Util.report("A SLF4J service provider failed to instantiate:\n" + e.getMessage());
120-
}
111+
safelyInstantiate(providerList, iterator);
121112
}
122113
return providerList;
123114
}
124115

116+
private static void safelyInstantiate(List<SLF4JServiceProvider> providerList, Iterator<SLF4JServiceProvider> iterator) {
117+
try {
118+
SLF4JServiceProvider provider = iterator.next();
119+
providerList.add(provider);
120+
} catch (ServiceConfigurationError e) {
121+
Util.report("A SLF4J service provider failed to instantiate:\n" + e.getMessage());
122+
}
123+
}
124+
125125
/**
126126
* It is LoggerFactory's responsibility to track version changes and manage
127127
* the compatibility list.

slf4j-api/src/test/java/org/slf4j/LoggerFactoryTest.java

-210
This file was deleted.

slf4j-api/src/test/resources/org/slf4j/substituteServiceProvider.txt

-2
This file was deleted.

0 commit comments

Comments
 (0)