Skip to content

Commit

Permalink
use the class loader that loaded LoggerFactory (instead of the thread…
Browse files Browse the repository at this point in the history
…ContextClassLoader) to find providers

Signed-off-by: Ceki Gulcu <[email protected]>
  • Loading branch information
ceki committed Nov 17, 2022
1 parent 557bf7c commit 43a3630
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 226 deletions.
28 changes: 14 additions & 14 deletions slf4j-api/src/main/java/org/slf4j/LoggerFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@

import java.io.IOException;
import java.net.URL;
import java.security.AccessControlException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
Expand Down Expand Up @@ -104,24 +101,27 @@ public final class LoggerFactory {

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

private static void safelyInstantiate(List<SLF4JServiceProvider> providerList, Iterator<SLF4JServiceProvider> iterator) {
try {
SLF4JServiceProvider provider = iterator.next();
providerList.add(provider);
} catch (ServiceConfigurationError e) {
Util.report("A SLF4J service provider failed to instantiate:\n" + e.getMessage());
}
}

/**
* It is LoggerFactory's responsibility to track version changes and manage
* the compatibility list.
Expand Down
210 changes: 0 additions & 210 deletions slf4j-api/src/test/java/org/slf4j/LoggerFactoryTest.java

This file was deleted.

This file was deleted.

0 comments on commit 43a3630

Please sign in to comment.