LoggerFactory emits 'SLF4J(I): Connected with provider of type'

Signed-off-by: Ceki Gulcu <ceki@qos.ch>
This commit is contained in:
Ceki Gulcu 2024-08-05 22:10:08 +02:00
parent 982e4b067f
commit 42810107b0
8 changed files with 63 additions and 42 deletions

View File

@ -56,9 +56,13 @@ public class CompatibilityAssertionTest {
Logger logger = LoggerFactory.getLogger(this.getClass());
String msg = "hello world " + diff;
logger.info(msg);
assertEquals(1, sps.stringList.size());
assertEquals(2, sps.stringList.size());
String s0 = (String) sps.stringList.get(0);
assertTrue(s0.contains(msg));
assertTrue(s0.startsWith("SLF4J(I): Connected with provider of type [org.slf4j.simple.SimpleServiceProvider]"));
String s1 = (String) sps.stringList.get(1);
assertTrue(s1.contains(msg));
}
}

View File

@ -59,7 +59,7 @@ public class MultiBindingAssertionTest {
assertMsgContains(list, 1, "Found provider");
assertMsgContains(list, 2, "Found provider");
assertMsgContains(list, 3, "See https://www.slf4j.org/codes.html#multiple_bindings for an explanation.");
assertMsgContains(list, 4, "Actual provider is of type [");
assertMsgContains(list, 4, "SLF4J(I): Connected with provider of type [");
}
void assertMsgContains(List<String> strList, int index, String msg) {

View File

@ -82,6 +82,9 @@ public final class LoggerFactory {
static final String UNSUCCESSFUL_INIT_URL = CODES_PREFIX + "#unsuccessfulInit";
static final String UNSUCCESSFUL_INIT_MSG = "org.slf4j.LoggerFactory in failed state. Original exception was thrown EARLIER. See also "
+ UNSUCCESSFUL_INIT_URL;
static final String CONNECTED_WITH_MSG = "Connected with provider of type [";
/**
* System property for explicitly setting the provider class. If set and the provider could be instantiated,
* then the service loading mechanism will be bypassed.
@ -403,9 +406,9 @@ public final class LoggerFactory {
}
private static void reportActualBinding(List<SLF4JServiceProvider> providerList) {
// binderPathSet can be null under Android
if (!providerList.isEmpty() && isAmbiguousProviderList(providerList)) {
Reporter.info("Actual provider is of type [" + providerList.get(0) + "]");
if (!providerList.isEmpty()) {
SLF4JServiceProvider provider = providerList.get(0);
Reporter.info(CONNECTED_WITH_MSG + provider.getClass().getName() + "]");
}
}

View File

@ -40,11 +40,11 @@ abstract public class MultithreadedInitializationTest {
long expectedEventCount = eventCount.get() + extraLogEvents();
assertTrue(expectedEventCount + " >= " + recordedEventCount, expectedEventCount >= recordedEventCount);
assertTrue("unmet condition: " + expectedEventCount + " >= " + recordedEventCount, expectedEventCount >= recordedEventCount);
assertTrue("unmet condition: " + expectedEventCount + " < " + recordedEventCount + "+" + LENIENCY_COUNT, expectedEventCount < (recordedEventCount + LENIENCY_COUNT)); }
abstract protected long getRecordedEventCount();
protected int extraLogEvents() {
return 0;
}

View File

@ -39,6 +39,7 @@ import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.LoggerFactory;
/**
* The present test is fragile in the sense that it sets up SimpleLogger
@ -108,11 +109,12 @@ public class SLF4JPlatformLoggingTest {
//INFO throwTest - a problem
//java.lang.Exception
// at org.slf4j.jdk.platform.logging/org.slf4j.jdk.platform.logging.SLF4JPlatformLoggingTest.throwTest(SLF4JPlatformLoggingTest.java:92)
assertEquals("INFO throwTest - we have a problem", results.get(0));
assertEquals(Exception.class.getName(), results.get(1));
assertTrue(results.get(2).contains("at "));
assertTrue(results.get(2).contains(this.getClass().getName()));
assertTrue(results.get(0).startsWith("SLF4J(I): Connected with provider of type ["));
assertEquals("INFO throwTest - we have a problem", results.get(1));
assertEquals(Exception.class.getName(), results.get(2));
assertTrue(results.get(3).contains("at "));
assertTrue(results.get(3).contains(this.getClass().getName()));
}

View File

@ -29,6 +29,14 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<type>test-jar</type>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -40,9 +40,11 @@ import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.LoggerFactoryFriend;
import org.slf4j.helpers.StringPrintStream;
public class MultithreadedInitializationTest {
static int NUM_LINES_IN_SLF4J_CONNECTED_WITH_PROVIDER_INFO = 1;
final static int THREAD_COUNT = 4 + Runtime.getRuntime().availableProcessors() * 2;
private static final AtomicLong EVENT_COUNT = new AtomicLong(0);
@ -52,7 +54,7 @@ public class MultithreadedInitializationTest {
int diff = new Random().nextInt(10000);
String loggerName = "org.slf4j.impl.MultithreadedInitializationTest";
private final PrintStream oldErr = System.err;
StringPrintStream sps = new StringPrintStream(oldErr);
StringPrintStream sps = new StringPrintStream(oldErr, false);
@Before
public void setup() {
@ -80,7 +82,7 @@ public class MultithreadedInitializationTest {
logger.info("hello");
EVENT_COUNT.getAndIncrement();
assertEquals(0, sps.stringList.size());
assertEquals(NUM_LINES_IN_SLF4J_CONNECTED_WITH_PROVIDER_INFO, sps.stringList.size());
}
private static LoggerAccessingThread[] harness() throws InterruptedException, BrokenBarrierException {
@ -120,31 +122,31 @@ public class MultithreadedInitializationTest {
}
};
public static class StringPrintStream extends PrintStream {
public static final String LINE_SEP = System.getProperty("line.separator");
PrintStream other;
List<String> stringList = new ArrayList<>();
public StringPrintStream(PrintStream ps) {
super(ps);
other = ps;
}
public void print(String s) {
other.print(s);
stringList.add(s);
}
public void println(String s) {
other.println(s);
stringList.add(s);
}
public void println(Object o) {
other.println(o);
stringList.add(o.toString());
}
};
// public static class StringPrintStream extends PrintStream {
//
// public static final String LINE_SEP = System.getProperty("line.separator");
// PrintStream other;
// List<String> stringList = new ArrayList<>();
//
// public StringPrintStream(PrintStream ps) {
// super(ps);
// other = ps;
// }
//
// public void print(String s) {
// other.print(s);
// stringList.add(s);
// }
//
// public void println(String s) {
// other.println(s);
// stringList.add(s);
// }
//
// public void println(Object o) {
// other.println(o);
// stringList.add(o.toString());
// }
// };
}

View File

@ -41,6 +41,8 @@ public class SimpleLoggerMultithreadedInitializationTest extends MultithreadedIn
//
// final int diff = new Random().nextInt(10000);
static int NUM_LINES_IN_SLF4J_REPLAY_WARNING = 3;
static int NUM_LINES_IN_SLF4J_CONNECTED_WITH_PROVIDER_INFO = 1;
private final PrintStream oldErr = System.err;
final String loggerName = this.getClass().getName();
StringPrintStream sps = new StringPrintStream(oldErr, false);
@ -67,7 +69,7 @@ public class SimpleLoggerMultithreadedInitializationTest extends MultithreadedIn
@Override
protected int extraLogEvents() {
return NUM_LINES_IN_SLF4J_REPLAY_WARNING;
return NUM_LINES_IN_SLF4J_REPLAY_WARNING + NUM_LINES_IN_SLF4J_CONNECTED_WITH_PROVIDER_INFO;
}
}