001/*
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2026, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v2.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014
015package ch.qos.logback.core.util;
016
017import ch.qos.logback.core.CoreConstants;
018
019import java.io.IOException;
020import java.io.InputStream;
021import java.util.Properties;
022
023/**
024 * Utility class for retrieving version information of the "logback-core" module.
025 *
026 * @since 1.5.26
027 */
028public class CoreVersionUtil {
029
030    static String CORE_MODULE_NAME = "logback-core";
031    static String CORE_MODULE_VERSION_PROPERTIES_FILE = CORE_MODULE_NAME + "-version.properties";
032    static String CORE_MODULE_VERSION_PROPERTY_KEY = CORE_MODULE_NAME + "-version";
033
034    /**
035     * Retrieves the version of the "logback-core" module using a properties file
036     * associated with the module.
037     *
038     * <p>The method locates and reads a properties file named "logback-core-version.properties"
039     * in the package of the {@code CoreConstants.class}. It then extracts the version
040     * information using the key "logback-core-version".
041     * </p>
042     *
043     * @return the version of the "logback-core" module as a string, or null if the version cannot be determined
044     * @since 1.5.26
045     */
046    static public String getCoreVersionBySelfDeclaredProperties() {
047        Properties props = new Properties();
048
049        try (InputStream is = CoreConstants.class.getResourceAsStream(CORE_MODULE_VERSION_PROPERTIES_FILE)) {
050            if (is != null) {
051                props.load(is);
052                return props.getProperty(CORE_MODULE_VERSION_PROPERTY_KEY);
053            } else {
054                return null;
055            }
056        } catch (IOException e) {
057            return null;
058        }
059    }
060}