Skip to content

Commit

Permalink
added method returning slf4j version
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 6, 2024
1 parent 4281010 commit 7c3b0ef
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 6 deletions.
9 changes: 3 additions & 6 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(providersList);
reportActualBinding(PROVIDER);
} else {
INITIALIZATION_STATE = NOP_FALLBACK_INITIALIZATION;
Reporter.warn("No SLF4J providers were found.");
Expand Down Expand Up @@ -405,11 +405,8 @@ private static void reportMultipleBindingAmbiguity(List<SLF4JServiceProvider> pr
}
}

private static void reportActualBinding(List<SLF4JServiceProvider> providerList) {
if (!providerList.isEmpty()) {
SLF4JServiceProvider provider = providerList.get(0);
Reporter.info(CONNECTED_WITH_MSG + provider.getClass().getName() + "]");
}
private static void reportActualBinding(final SLF4JServiceProvider provider) {
Reporter.info(CONNECTED_WITH_MSG + provider.getClass().getName() + "]");
}

/**
Expand Down
50 changes: 50 additions & 0 deletions slf4j-api/src/main/java/org/slf4j/helpers/Slf4jEnvUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.slf4j.helpers;

import java.lang.module.ModuleDescriptor;
import java.util.Optional;

public class Slf4jEnvUtil {


/**
* <p>Returns the current version of logback, or null if data is not
* available.
* </p>
*
* @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;
}

/**
* <p>Returns the current version of logback via class.getModule() or null if data is not
* available.
* </p>
*
* @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<String> opt = md.rawVersion();
return opt.orElse(null);
}

}
21 changes: 21 additions & 0 deletions slf4j-simple/src/test/java/org/slf4j/simple/Slf4jVersionTest.java
Original file line number Diff line number Diff line change
@@ -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"));

}

}

0 comments on commit 7c3b0ef

Please sign in to comment.