-
-
Notifications
You must be signed in to change notification settings - Fork 997
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added method returning slf4j version
Signed-off-by: Ceki Gulcu <[email protected]>
- Loading branch information
Showing
3 changed files
with
74 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
slf4j-api/src/main/java/org/slf4j/helpers/Slf4jEnvUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
slf4j-simple/src/test/java/org/slf4j/simple/Slf4jVersionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
|
||
} | ||
|
||
} |