Skip to content

Commit

Permalink
LoggerFactory emits 'SLF4J(I): Connected with provider of type'
Browse files Browse the repository at this point in the history
Signed-off-by: Ceki Gulcu <ceki@qos.ch>
  • Loading branch information
ceki committed Aug 5, 2024
1 parent 982e4b0 commit 4281010
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,13 @@ public void test() throws Exception {
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));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void test() throws Exception {
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) {
Expand Down
9 changes: 6 additions & 3 deletions slf4j-api/src/main/java/org/slf4j/LoggerFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -403,9 +406,9 @@ private static void reportMultipleBindingAmbiguity(List<SLF4JServiceProvider> pr
}

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() + "]");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ public void multiThreadedInitialization() throws InterruptedException, BrokenBar

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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
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
Expand Down Expand Up @@ -108,11 +109,12 @@ public void throwTest() throws IOException {
//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()));
}


Expand Down
8 changes: 8 additions & 0 deletions slf4j-nop/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@
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);
Expand All @@ -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() {
Expand Down Expand Up @@ -80,7 +82,7 @@ public void multiThreadedInitialization() throws InterruptedException, BrokenBar
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 {
Expand Down Expand Up @@ -120,31 +122,31 @@ public void run() {
}
};

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());
// }
// };

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -67,7 +69,7 @@ protected long getRecordedEventCount() {

@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;
}

}

0 comments on commit 4281010

Please sign in to comment.