Skip to content

Commit

Permalink
cherry pick changes 3ff0087 from 2.0, print connected provider only i…
Browse files Browse the repository at this point in the history
…f DEBUG level for internal reporting is enabled

Signed-off-by: Ceki Gulcu <ceki@qos.ch>
  • Loading branch information
ceki committed Aug 10, 2024
1 parent caeccff commit 914e8c8
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.slf4j.helpers.Reporter.SLF4J_INTERNAL_VERBOSITY_KEY;

import java.io.PrintStream;
import java.util.Random;
Expand All @@ -43,11 +44,13 @@ public class CompatibilityAssertionTest {

@Before
public void setUp() throws Exception {
System.setProperty(SLF4J_INTERNAL_VERBOSITY_KEY, "debug");
System.setErr(sps);
}

@After
public void tearDown() throws Exception {
System.clearProperty(SLF4J_INTERNAL_VERBOSITY_KEY);
System.setErr(old);
}

Expand All @@ -57,8 +60,10 @@ public void test() throws Exception {
String msg = "hello world " + diff;
logger.info(msg);
assertEquals(2, sps.stringList.size());

String s0 = (String) sps.stringList.get(0);
assertTrue(s0.startsWith("SLF4J(I): Connected with provider of type [org.slf4j.simple.SimpleServiceProvider"));
assertTrue(s0.startsWith("SLF4J(D): Connected with provider of type [org.slf4j.simple.SimpleServiceProvider]"));

String s1 = (String) sps.stringList.get(1);
assertTrue(s1.contains(msg));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,12 @@ public void test() throws Exception {
String msg = "hello world " + diff;
logger.info(msg);
List<String> list = sps.stringList;
assertMsgContains(list, 0, "Class path contains multiple SLF4J providers.");
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, "SLF4J(I): Connected with provider of type [org.slf4j.simple.SimpleServiceProvider");
int line = 0;

assertMsgContains(list, line++, "Class path contains multiple SLF4J providers.");
assertMsgContains(list, line++, "Found provider");
assertMsgContains(list, line++, "Found provider");
assertMsgContains(list, line++, "See https://www.slf4j.org/codes.html#multiple_bindings for an explanation.");
}

void assertMsgContains(List<String> strList, int index, String msg) {
Expand Down
16 changes: 13 additions & 3 deletions slf4j-api/src/main/java/org/slf4j/LoggerFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ private final static void bind() {
// SLF4JServiceProvider.initialize() is intended to be called here and nowhere else.
PROVIDER.initialize();
INITIALIZATION_STATE = SUCCESSFUL_INITIALIZATION;
reportActualBinding(PROVIDER);
reportActualBinding(providersList);
} else {
INITIALIZATION_STATE = NOP_FALLBACK_INITIALIZATION;
Reporter.warn("No SLF4J providers were found.");
Expand Down Expand Up @@ -405,8 +405,18 @@ private static void reportMultipleBindingAmbiguity(List<SLF4JServiceProvider> pr
}
}

private static void reportActualBinding(SLF4JServiceProvider provider) {
Reporter.info(CONNECTED_WITH_MSG + provider.getClass().getName() + "]");
private static void reportActualBinding(List<SLF4JServiceProvider> providerList) {
// impossible since a provider has been found
if (providerList.isEmpty()) {
throw new IllegalStateException("No providers were found which is impossible after successful initialization.");
}

if (isAmbiguousProviderList(providerList)) {
Reporter.info("Actual provider is of type [" + providerList.get(0) + "]");
} else {
SLF4JServiceProvider provider = providerList.get(0);
Reporter.debug(CONNECTED_WITH_MSG + provider.getClass().getName() + "]");
}
}

/**
Expand Down
51 changes: 37 additions & 14 deletions slf4j-api/src/main/java/org/slf4j/helpers/Reporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import java.io.PrintStream;

/**
* An internally used class for reporting internal messages generated by SLF4J itself during initialization.
* An internally used class for reporting internal messages generated by SLF4J itself, typically
* during initialization.
*
* <p>
* Internal reporting is performed by calling the {@link #info(String)}, {@link #warn(String)} (String)}
* {@link #error(String)} (String)} and {@link #error(String, Throwable)} methods.
* Internal reporting is performed by calling the {@link #debug(String)}, {@link #info(String)},
* {@link #warn(String)} (String)} {@link #error(String)} (String)} and
* {@link #error(String, Throwable)} methods.
* </p>
* <p>See {@link #SLF4J_INTERNAL_VERBOSITY_KEY} and {@link #SLF4J_INTERNAL_REPORT_STREAM_KEY} for
* configuration options.</p>
Expand All @@ -23,7 +25,7 @@ public class Reporter {
* this class is used internally by Reporter
*/
private enum Level {
INFO(1), WARN(2), ERROR(3);
DEBUG(0), INFO(1), WARN(2), ERROR(3);

int levelInt;

Expand All @@ -40,6 +42,7 @@ private enum TargetChoice {
Stderr, Stdout;
}

static final String SLF4J_DEBUG_PREFIX = "SLF4J(D): ";
static final String SLF4J_INFO_PREFIX = "SLF4J(I): ";
static final String SLF4J_WARN_PREFIX = "SLF4J(W): ";
static final String SLF4J_ERROR_PREFIX = "SLF4J(E): ";
Expand All @@ -62,11 +65,11 @@ private enum TargetChoice {
public static final String SLF4J_INTERNAL_VERBOSITY_KEY = "slf4j.internal.verbosity";


static private final TargetChoice TARGET_CHOICE = initTargetChoice();
static private final TargetChoice TARGET_CHOICE = getTargetChoice();

static private final Level INTERNAL_VERBOSITY = initVerbosity();

static private TargetChoice initTargetChoice() {
static private TargetChoice getTargetChoice() {
String reportStreamStr = System.getProperty(SLF4J_INTERNAL_REPORT_STREAM_KEY);

if(reportStreamStr == null || reportStreamStr.isEmpty()) {
Expand All @@ -88,6 +91,10 @@ static private Level initVerbosity() {
return Level.INFO;
}

if(verbosityStr.equalsIgnoreCase("DEBUG")) {
return Level.DEBUG;
}

if(verbosityStr.equalsIgnoreCase("ERROR")) {
return Level.ERROR;
}
Expand All @@ -97,6 +104,7 @@ static private Level initVerbosity() {
return Level.WARN;
}

// anything else including verbosityStr.equalsIgnoreCase("INFO")
return Level.INFO;
}

Expand All @@ -114,13 +122,30 @@ static private PrintStream getTarget() {
}
}

/**
* Report an internal message of level DEBUG. Message text is prefixed with the string "SLF4J(D)",
* with (D) standing as a shorthand for DEBUG.
*
* <p>Messages of level DEBUG are be enabled when the {@link #SLF4J_INTERNAL_VERBOSITY_KEY}
* system property is set to "DEBUG" and disabled when set to "INFO", "WARN" or "ERROR". By default,
* {@link #SLF4J_INTERNAL_VERBOSITY_KEY} is set to "INFO".</p>
*
* @param msg the message text
* @since 2.0.16
*/
public static void debug(String msg) {
if(isEnabledFor(Level.DEBUG)) {
getTarget().println(SLF4J_DEBUG_PREFIX + msg);
}
}

/**
* Report an internal message of level INFO. Message text is prefixed with the string "SLF4J(I)", with
* (I) standing as a shorthand for INFO.
*
* <p>Messages of level INFO are be enabled when the {@link #SLF4J_INTERNAL_VERBOSITY_KEY} system property is
* set to "INFO" and disabled when set to "WARN" or "ERROR". By default, {@link #SLF4J_INTERNAL_VERBOSITY_KEY} is
* set to "INFO".</p>
* set to "DEBUG" or "INFO" and disabled when set to "WARN" or "ERROR". By default,
* {@link #SLF4J_INTERNAL_VERBOSITY_KEY} is set to "INFO".</p>
*
* @param msg the message text
*/
Expand All @@ -136,8 +161,8 @@ public static void info(String msg) {
* (W) standing as a shorthand for WARN.
*
* <p>Messages of level WARN are be enabled when the {@link #SLF4J_INTERNAL_VERBOSITY_KEY} system property is
* set to "INFO" or "WARN" and disabled when set to "ERROR". By default, {@link #SLF4J_INTERNAL_VERBOSITY_KEY} is
* set to "INFO".</p>
* set to "DEBUG", "INFO" or "WARN" and disabled when set to "ERROR". By default,
* {@link #SLF4J_INTERNAL_VERBOSITY_KEY} is set to "INFO".</p>
*
* @param msg the message text
*/
Expand All @@ -147,7 +172,6 @@ static final public void warn(String msg) {
}
}


/**
* Report an internal message of level "ERROR accompanied by a {@link Throwable}.
* Message text is prefixed with the string "SLF4J(E)", with (E) standing as a shorthand for ERROR.
Expand All @@ -164,10 +188,9 @@ static final public void error(String msg, Throwable t) {
t.printStackTrace(getTarget());
}


/**
* Report an internal message of level "ERROR". Message text is prefixed with the string "SLF4J(E)", with
* (E) standing as a shorthand for ERROR.
* Report an internal message of level "ERROR". Message text is prefixed with the string "SLF4J(E)",
* with (E) standing as a shorthand for ERROR.
*
* <p>Messages of level ERROR are always enabled.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,12 @@ public void throwTest() throws IOException {
// java.lang.Exception
// at org.slf4j.jdk.platform.logging/org.slf4j.jdk.platform.logging.SLF4JPlatformLoggingTest.throwTest(SLF4JPlatformLoggingTest.java:92)

String firstLine = results.get(0);
assertTrue(firstLine.startsWith("SLF4J(I): Connected with provider of type [org.slf4j.simple.SimpleServiceProvider"));
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()));
int line = 0;
//assertTrue(results.get(0).startsWith("SLF4J(I): Connected with provider of type ["));
assertEquals("INFO throwTest - we have a problem", results.get(line++));
assertEquals(Exception.class.getName(), results.get(line++));
assertTrue(results.get(line).contains("at "));
assertTrue(results.get(line++).contains(this.getClass().getName()));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package org.slf4j.nop;

import static org.junit.Assert.assertEquals;
import static org.slf4j.helpers.Reporter.SLF4J_INTERNAL_VERBOSITY_KEY;

import java.io.PrintStream;
import java.util.ArrayList;
Expand Down Expand Up @@ -56,12 +57,14 @@ public class MultithreadedInitializationTest {

@Before
public void setup() {
System.setProperty(SLF4J_INTERNAL_VERBOSITY_KEY, "debug");
LoggerFactoryFriend.reset();
System.setErr(sps);
}

@After
public void tearDown() throws Exception {
System.clearProperty(SLF4J_INTERNAL_VERBOSITY_KEY);
LoggerFactoryFriend.reset();
System.setErr(oldErr);
}
Expand Down

0 comments on commit 914e8c8

Please sign in to comment.