Skip to content

Commit

Permalink
check that logback-classic and logback-core are identical
Browse files Browse the repository at this point in the history
Signed-off-by: Ceki Gulcu <[email protected]>
  • Loading branch information
ceki committed Dec 20, 2024
1 parent 020610a commit 402fc67
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,8 @@ public class ClassicConstants {
public static final Marker FINALIZE_SESSION_MARKER = MarkerFactory.getMarker(FINALIZE_SESSION);
final public static String AUTOCONFIG_FILE = "logback.xml";
final public static String TEST_AUTOCONFIG_FILE = "logback-test.xml";

public static final String LOGBACK_CLASSIC_VERSION_MESSAGE = "This is logback-classic version ";
public static final String LOGBACK_VERSIONS_MISMATCH = "Versions of logback-core and logback-classic are different!";

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@
*/
package ch.qos.logback.classic.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ServiceLoader;
import java.lang.module.ModuleDescriptor;
import java.util.*;

import ch.qos.logback.core.util.EnvUtil;
import ch.qos.logback.core.util.Loader;

/**
* @author Ceki G&uuml;lc&uuml;
Expand Down Expand Up @@ -54,4 +51,43 @@ public static <T> List<T> loadFromServiceLoader(Class<T> c, ClassLoader classLoa
return listOfT;
}

/**
* <p>Returns the current version of logback-classic, or null if data is not
* available.
* </p>
*
* @since 1.5.15
* @return current version or null if missing version data
*/
static public String getVersionOfLogbackClassic() {
String moduleVersion = getVersionOfLogbackClassicByModule();
if (moduleVersion != null)
return moduleVersion;

Package pkg = ClassicEnvUtil.class.getPackage();
if (pkg == null) {
return null;
}
return pkg.getImplementationVersion();
}

/**
* <p>Returns the current version of logback-classic via class.getModule() or null
* if data is not available.
* </p>
*
* @since 1.5.15
* @return current version or null if missing version data
*/
static private String getVersionOfLogbackClassicByModule() {
Module module = ClassicEnvUtil.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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import ch.qos.logback.core.spi.ContextAware;
import ch.qos.logback.core.spi.ContextAwareImpl;
import ch.qos.logback.core.status.InfoStatus;
import ch.qos.logback.core.status.WarnStatus;
import ch.qos.logback.core.util.EnvUtil;
import ch.qos.logback.core.util.Loader;
import ch.qos.logback.core.util.StatusListenerConfigHelper;
Expand Down Expand Up @@ -72,11 +73,8 @@ public void autoConfig(ClassLoader classLoader) throws JoranException {
// see https://github.com/qos-ch/logback/issues/715
classLoader = Loader.systemClassloaderIfNull(classLoader);

String versionStr = EnvUtil.logbackVersion();
if (versionStr == null) {
versionStr = CoreConstants.NA;
}
loggerContext.getStatusManager().add(new InfoStatus(CoreConstants.LOGBACK_CLASSIC_VERSION_MESSAGE + versionStr, loggerContext));
checkVersions();

StatusListenerConfigHelper.installIfAsked(loggerContext);


Expand Down Expand Up @@ -105,6 +103,22 @@ public void autoConfig(ClassLoader classLoader) throws JoranException {
}
}

private void checkVersions() {
String versionOfLogbackClassic = ClassicEnvUtil.getVersionOfLogbackClassic();
if (versionOfLogbackClassic == null) {
versionOfLogbackClassic = CoreConstants.NA;
}
String versionOfLogbackCore = EnvUtil.logbackVersion();
if (versionOfLogbackCore == null) {
versionOfLogbackCore = CoreConstants.NA;
}
loggerContext.getStatusManager().add(new InfoStatus(ClassicConstants.LOGBACK_CLASSIC_VERSION_MESSAGE + versionOfLogbackClassic, loggerContext));
if(!versionOfLogbackCore.equals(versionOfLogbackClassic)) {
loggerContext.getStatusManager().add(new InfoStatus(CoreConstants.LOGBACK_CORE_VERSION_MESSAGE + versionOfLogbackCore, loggerContext));
loggerContext.getStatusManager().add(new WarnStatus(ClassicConstants.LOGBACK_VERSIONS_MISMATCH, loggerContext));
}
}

private Configurator instantiateConfiguratorByClassName(String configuratorClassName, ClassLoader classLoader) {
try {
Class<?> classObj = classLoader.loadClass(configuratorClassName);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Logback: the reliable, generic, fast and flexible logging framework.
* Copyright (C) 1999-2024, QOS.ch. All rights reserved.
*
* This program and the accompanying materials are dual-licensed under
* either the terms of the Eclipse Public License v1.0 as published by
* the Eclipse Foundation
*
* or (per the licensee's choosing)
*
* under the terms of the GNU Lesser General Public License version 2.1
* as published by the Free Software Foundation.
*/

package ch.qos.logback.classic.blackbox.util;

import ch.qos.logback.classic.util.ClassicEnvUtil;
import ch.qos.logback.core.util.EnvUtil;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class EnvUtilTest {

// Beware: ----------------------------------------
// Beware: needs to be updated upon version change
// Beware: ----------------------------------------
static final String EXPECTED_VERSION = "1.5";


@BeforeEach
public void setUp() throws Exception {

}

// this test runs fine if run from logback-classic but fails when
// run from logback-core. This is due to the fact that package information
// is added when creating the jar.
@Test
public void versionTest() {
String versionStr = EnvUtil.logbackVersion();
assertNotNull(versionStr);
assertTrue(versionStr.startsWith(EXPECTED_VERSION));
}

@Test
public void versionCompare() {
String coreVersionStr = EnvUtil.logbackVersion();
String versionOfLogbackClassic = ClassicEnvUtil.getVersionOfLogbackClassic();
assertNotNull(coreVersionStr);
assertNotNull(versionOfLogbackClassic);

assertEquals(coreVersionStr, versionOfLogbackClassic);
}


}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,11 @@ public class CoreConstants {
//public static final String RECONFIGURE_ON_CHANGE_TASK = "RECONFIGURE_ON_CHANGE_TASK";
public static final String SIZE_AND_TIME_BASED_FNATP_IS_DEPRECATED = "SizeAndTimeBasedFileNamingAndTriggeringPolicy is deprecated. Use SizeAndTimeBasedRollingPolicy instead";

public static final String LOGBACK_CLASSIC_VERSION_MESSAGE = "This is logback-classic version ";
public static final char JSON_LINE_SEPARATOR = '\n';
final public static String MODEL_CONFIG_FILE_EXTENSION = ".scmo";
/**
* since 1.5.8
*/
final public static String PROPERTIES_FILE_EXTENSION = ".properties";
public static final String LOGBACK_CORE_VERSION_MESSAGE = "This is logback-core version ";
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class EnvUtil {
private EnvUtil() {
}


/**
* <p>Returns the current version of logback, or null if data is not
* available.
Expand All @@ -33,8 +34,8 @@ private EnvUtil() {
* @return current version or null if missing version data
*/
static public String logbackVersion() {
String moduleVersion = logbackVersionByModule();
if(moduleVersion != null)
String moduleVersion = getVersionOfLogbackCoreByModule();
if (moduleVersion != null)
return moduleVersion;

Package pkg = EnvUtil.class.getPackage();
Expand All @@ -52,7 +53,7 @@ static public String logbackVersion() {
* @since 1.3.0
* @return current version or null if missing version data
*/
static private String logbackVersionByModule() {
static private String getVersionOfLogbackCoreByModule() {
Module module = EnvUtil.class.getModule();
if (module == null)
return null;
Expand Down

0 comments on commit 402fc67

Please sign in to comment.