From 3ff00870b32c2067d72fb83d6a9e9554dddd8130 Mon Sep 17 00:00:00 2001 From: Ceki Gulcu Date: Sat, 10 Aug 2024 09:49:56 +0200 Subject: [PATCH] print connected provider only if DEBUG level for internal reporting is enabled Signed-off-by: Ceki Gulcu --- .../org/slf4j/CompatibilityAssertionTest.java | 5 +- .../org/slf4j/MultiBindingAssertionTest.java | 12 +++-- .../main/java/org/slf4j/LoggerFactory.java | 16 ++++-- .../main/java/org/slf4j/helpers/Reporter.java | 51 ++++++++++++++----- .../test/SLF4JPlatformLoggingTest.java | 11 ++-- .../nop/MultithreadedInitializationTest.java | 3 ++ 6 files changed, 70 insertions(+), 28 deletions(-) diff --git a/integration/src/test/java/org/slf4j/CompatibilityAssertionTest.java b/integration/src/test/java/org/slf4j/CompatibilityAssertionTest.java index 59855a7a2..9ccd69fa0 100644 --- a/integration/src/test/java/org/slf4j/CompatibilityAssertionTest.java +++ b/integration/src/test/java/org/slf4j/CompatibilityAssertionTest.java @@ -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; @@ -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 void test() throws Exception { 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)); diff --git a/integration/src/test/java/org/slf4j/MultiBindingAssertionTest.java b/integration/src/test/java/org/slf4j/MultiBindingAssertionTest.java index cec7940fc..2eb3c7689 100644 --- a/integration/src/test/java/org/slf4j/MultiBindingAssertionTest.java +++ b/integration/src/test/java/org/slf4j/MultiBindingAssertionTest.java @@ -55,11 +55,13 @@ public void test() throws Exception { String msg = "hello world " + diff; logger.info(msg); List 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 strList, int index, String msg) { diff --git a/slf4j-api/src/main/java/org/slf4j/LoggerFactory.java b/slf4j-api/src/main/java/org/slf4j/LoggerFactory.java index 7c334934e..9ad6b8a13 100755 --- a/slf4j-api/src/main/java/org/slf4j/LoggerFactory.java +++ b/slf4j-api/src/main/java/org/slf4j/LoggerFactory.java @@ -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."); @@ -405,8 +405,18 @@ private static void reportMultipleBindingAmbiguity(List pr } } - private static void reportActualBinding(final SLF4JServiceProvider provider) { - Reporter.info(CONNECTED_WITH_MSG + provider.getClass().getName() + "]"); + private static void reportActualBinding(List 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() + "]"); + } } /** diff --git a/slf4j-api/src/main/java/org/slf4j/helpers/Reporter.java b/slf4j-api/src/main/java/org/slf4j/helpers/Reporter.java index 2f269969a..9091b2940 100644 --- a/slf4j-api/src/main/java/org/slf4j/helpers/Reporter.java +++ b/slf4j-api/src/main/java/org/slf4j/helpers/Reporter.java @@ -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. * *

- * 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. *

*

See {@link #SLF4J_INTERNAL_VERBOSITY_KEY} and {@link #SLF4J_INTERNAL_REPORT_STREAM_KEY} for * configuration options.

@@ -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 @@ 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): "; @@ -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()) { @@ -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; } @@ -97,6 +104,7 @@ static private Level initVerbosity() { return Level.WARN; } + // anything else including verbosityStr.equalsIgnoreCase("INFO") return Level.INFO; } @@ -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. + * + *

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".

+ * + * @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. * *

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".

+ * set to "DEBUG" or "INFO" and disabled when set to "WARN" or "ERROR". By default, + * {@link #SLF4J_INTERNAL_VERBOSITY_KEY} is set to "INFO".

* * @param msg the message text */ @@ -136,8 +161,8 @@ public static void info(String msg) { * (W) standing as a shorthand for WARN. * *

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".

+ * set to "DEBUG", "INFO" or "WARN" and disabled when set to "ERROR". By default, + * {@link #SLF4J_INTERNAL_VERBOSITY_KEY} is set to "INFO".

* * @param msg the message text */ @@ -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. @@ -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. * *

Messages of level ERROR are always enabled. * diff --git a/slf4j-jdk-platform-logging/src/test/java/org/slf4j/jdk/platform/logging/test/SLF4JPlatformLoggingTest.java b/slf4j-jdk-platform-logging/src/test/java/org/slf4j/jdk/platform/logging/test/SLF4JPlatformLoggingTest.java index b2f62ddd7..8778fd2fa 100644 --- a/slf4j-jdk-platform-logging/src/test/java/org/slf4j/jdk/platform/logging/test/SLF4JPlatformLoggingTest.java +++ b/slf4j-jdk-platform-logging/src/test/java/org/slf4j/jdk/platform/logging/test/SLF4JPlatformLoggingTest.java @@ -110,11 +110,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) - 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())); } diff --git a/slf4j-nop/src/test/java/org/slf4j/nop/MultithreadedInitializationTest.java b/slf4j-nop/src/test/java/org/slf4j/nop/MultithreadedInitializationTest.java index e1c7398ec..dabef97c3 100755 --- a/slf4j-nop/src/test/java/org/slf4j/nop/MultithreadedInitializationTest.java +++ b/slf4j-nop/src/test/java/org/slf4j/nop/MultithreadedInitializationTest.java @@ -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); }