From 19ca97d7512e263d06983b0c23ac9da9c6afea14 Mon Sep 17 00:00:00 2001 From: Ceki Gulcu Date: Wed, 7 Aug 2024 22:47:51 +0200 Subject: [PATCH] align with version 2.0.14 Signed-off-by: Ceki Gulcu --- .../main/java/org/slf4j/LoggerFactory.java | 9 ++-- .../java/org/slf4j/helpers/Slf4jEnvUtil.java | 50 +++++++++++++++++++ .../org/slf4j/simple/Slf4jVersionTest.java | 21 ++++++++ 3 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 slf4j-api/src/main/java/org/slf4j/helpers/Slf4jEnvUtil.java create mode 100644 slf4j-simple/src/test/java/org/slf4j/simple/Slf4jVersionTest.java diff --git a/slf4j-api/src/main/java/org/slf4j/LoggerFactory.java b/slf4j-api/src/main/java/org/slf4j/LoggerFactory.java index 4fb847f1e..cfd96af50 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(providersList); + reportActualBinding(PROVIDER); } else { INITIALIZATION_STATE = NOP_FALLBACK_INITIALIZATION; Reporter.warn("No SLF4J providers were found."); @@ -405,11 +405,8 @@ private static void reportMultipleBindingAmbiguity(List pr } } - private static void reportActualBinding(List providerList) { - if (!providerList.isEmpty()) { - SLF4JServiceProvider provider = providerList.get(0); - Reporter.info(CONNECTED_WITH_MSG + provider.getClass().getName() + "]"); - } + private static void reportActualBinding(SLF4JServiceProvider provider) { + Reporter.info(CONNECTED_WITH_MSG + provider.getClass().getName() + "]"); } /** diff --git a/slf4j-api/src/main/java/org/slf4j/helpers/Slf4jEnvUtil.java b/slf4j-api/src/main/java/org/slf4j/helpers/Slf4jEnvUtil.java new file mode 100644 index 000000000..9921b02ed --- /dev/null +++ b/slf4j-api/src/main/java/org/slf4j/helpers/Slf4jEnvUtil.java @@ -0,0 +1,50 @@ +package org.slf4j.helpers; + +import java.lang.module.ModuleDescriptor; +import java.util.Optional; + +public class Slf4jEnvUtil { + + + /** + *

Returns the current version of logback, or null if data is not + * available. + *

+ * + * @return current version or null if missing version data + * @since 1.3.0 + */ + static public String slf4jVersion() { + String moduleVersion = slf4jVersionByModule(); + if(moduleVersion != null) + return moduleVersion; + + Package pkg = Slf4jEnvUtil.class.getPackage(); + if(pkg == null) { + return null; + } + final String pkgVersion = pkg.getImplementationVersion(); + return pkgVersion; + } + + /** + *

Returns the current version of logback via class.getModule() or null if data is not + * available. + *

+ * + * @since 1.3.0 + * @return current version or null if missing version data + */ + static private String slf4jVersionByModule() { + Module module = Slf4jEnvUtil.class.getModule(); + if (module == null) + return null; + + ModuleDescriptor md = module.getDescriptor(); + if (md == null) + return null; + Optional opt = md.rawVersion(); + return opt.orElse(null); + } + +} diff --git a/slf4j-simple/src/test/java/org/slf4j/simple/Slf4jVersionTest.java b/slf4j-simple/src/test/java/org/slf4j/simple/Slf4jVersionTest.java new file mode 100644 index 000000000..fbff09b74 --- /dev/null +++ b/slf4j-simple/src/test/java/org/slf4j/simple/Slf4jVersionTest.java @@ -0,0 +1,21 @@ +package org.slf4j.simple; + +import org.junit.Test; +import org.slf4j.helpers.Slf4jEnvUtil; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +public class Slf4jVersionTest { + + + @Test + public void slf4jVersionTest() { + + String version = Slf4jEnvUtil.slf4jVersion(); + assertNotNull(version); + assertTrue(version.startsWith("2")); + + } + +}