mirror of https://github.com/qos-ch/slf4j
print connected provider only if DEBUG level for internal reporting is enabled
Signed-off-by: Ceki Gulcu <ceki@qos.ch>
This commit is contained in:
parent
0df43e9e53
commit
3ff00870b3
|
|
@ -27,6 +27,7 @@ package org.slf4j;
|
|||
|
||||
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;
|
||||
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
@ -59,7 +62,7 @@ public class CompatibilityAssertionTest {
|
|||
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));
|
||||
|
|
|
|||
|
|
@ -55,11 +55,13 @@ public class MultiBindingAssertionTest {
|
|||
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 [");
|
||||
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.");
|
||||
//assertMsgContains(list, line++, "SLF4J(D): Connected with provider of type [");
|
||||
}
|
||||
|
||||
void assertMsgContains(List<String> strList, int index, String msg) {
|
||||
|
|
|
|||
|
|
@ -198,7 +198,7 @@ public final class LoggerFactory {
|
|||
// 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.");
|
||||
|
|
@ -405,8 +405,18 @@ public final class LoggerFactory {
|
|||
}
|
||||
}
|
||||
|
||||
private static void reportActualBinding(final 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() + "]");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -3,11 +3,13 @@ package org.slf4j.helpers;
|
|||
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>
|
||||
|
|
@ -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;
|
||||
|
||||
|
|
@ -40,6 +42,7 @@ public class Reporter {
|
|||
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): ";
|
||||
|
|
@ -62,11 +65,11 @@ public class Reporter {
|
|||
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()) {
|
||||
|
|
@ -88,6 +91,10 @@ public class Reporter {
|
|||
return Level.INFO;
|
||||
}
|
||||
|
||||
if(verbosityStr.equalsIgnoreCase("DEBUG")) {
|
||||
return Level.DEBUG;
|
||||
}
|
||||
|
||||
if(verbosityStr.equalsIgnoreCase("ERROR")) {
|
||||
return Level.ERROR;
|
||||
}
|
||||
|
|
@ -97,6 +104,7 @@ public class Reporter {
|
|||
return Level.WARN;
|
||||
}
|
||||
|
||||
// anything else including verbosityStr.equalsIgnoreCase("INFO")
|
||||
return Level.INFO;
|
||||
}
|
||||
|
||||
|
|
@ -114,13 +122,30 @@ public class Reporter {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
|
|
@ -136,8 +161,8 @@ public class Reporter {
|
|||
* (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
|
||||
*/
|
||||
|
|
@ -147,7 +172,6 @@ public class Reporter {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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.
|
||||
|
|
@ -164,10 +188,9 @@ public class Reporter {
|
|||
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.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -110,11 +110,12 @@ public class SLF4JPlatformLoggingTest {
|
|||
//java.lang.Exception
|
||||
// at org.slf4j.jdk.platform.logging/org.slf4j.jdk.platform.logging.SLF4JPlatformLoggingTest.throwTest(SLF4JPlatformLoggingTest.java:92)
|
||||
|
||||
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()));
|
||||
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()));
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
@ -58,12 +59,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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue