001package org.slf4j.helpers;
002
003import java.lang.module.ModuleDescriptor;
004import java.util.Optional;
005
006public class Slf4jEnvUtil {
007
008
009    /**
010     * <p>Returns the current version of logback, or null if data is not
011     * available.
012     * </p>
013     *
014     * @return current version or null if missing version data
015     * @since 1.3.0
016     */
017    static public String slf4jVersion() {
018        String moduleVersion = slf4jVersionByModule();
019        if(moduleVersion != null)
020            return moduleVersion;
021
022        Package pkg = Slf4jEnvUtil.class.getPackage();
023        if(pkg == null) {
024            return null;
025        }
026        final String pkgVersion = pkg.getImplementationVersion();
027        return pkgVersion;
028    }
029
030    /**
031     * <p>Returns the current version of logback via class.getModule() or null if data is not
032     * available.
033     * </p>
034     *
035     * @since 1.3.0
036     * @return current version or null if missing version data
037     */
038    static private String slf4jVersionByModule() {
039        Module module = Slf4jEnvUtil.class.getModule();
040        if (module == null)
041            return null;
042
043        ModuleDescriptor md = module.getDescriptor();
044        if (md == null)
045            return null;
046        Optional<String> opt = md.rawVersion();
047        return opt.orElse(null);
048    }
049
050}