Skip to content

Commit 402fc67

Browse files
committed
check that logback-classic and logback-core are identical
Signed-off-by: Ceki Gulcu <ceki@qos.ch>
1 parent 020610a commit 402fc67

File tree

7 files changed

+127
-48
lines changed

7 files changed

+127
-48
lines changed

logback-classic/src/main/java/ch/qos/logback/classic/ClassicConstants.java

+4
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,8 @@ public class ClassicConstants {
6262
public static final Marker FINALIZE_SESSION_MARKER = MarkerFactory.getMarker(FINALIZE_SESSION);
6363
final public static String AUTOCONFIG_FILE = "logback.xml";
6464
final public static String TEST_AUTOCONFIG_FILE = "logback-test.xml";
65+
66+
public static final String LOGBACK_CLASSIC_VERSION_MESSAGE = "This is logback-classic version ";
67+
public static final String LOGBACK_VERSIONS_MISMATCH = "Versions of logback-core and logback-classic are different!";
68+
6569
}

logback-classic/src/main/java/ch/qos/logback/classic/util/ClassicEnvUtil.java

+41-5
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,10 @@
1313
*/
1414
package ch.qos.logback.classic.util;
1515

16-
import java.util.ArrayList;
17-
import java.util.Iterator;
18-
import java.util.List;
19-
import java.util.ServiceLoader;
16+
import java.lang.module.ModuleDescriptor;
17+
import java.util.*;
2018

2119
import ch.qos.logback.core.util.EnvUtil;
22-
import ch.qos.logback.core.util.Loader;
2320

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

54+
/**
55+
* <p>Returns the current version of logback-classic, or null if data is not
56+
* available.
57+
* </p>
58+
*
59+
* @since 1.5.15
60+
* @return current version or null if missing version data
61+
*/
62+
static public String getVersionOfLogbackClassic() {
63+
String moduleVersion = getVersionOfLogbackClassicByModule();
64+
if (moduleVersion != null)
65+
return moduleVersion;
66+
67+
Package pkg = ClassicEnvUtil.class.getPackage();
68+
if (pkg == null) {
69+
return null;
70+
}
71+
return pkg.getImplementationVersion();
72+
}
73+
74+
/**
75+
* <p>Returns the current version of logback-classic via class.getModule() or null
76+
* if data is not available.
77+
* </p>
78+
*
79+
* @since 1.5.15
80+
* @return current version or null if missing version data
81+
*/
82+
static private String getVersionOfLogbackClassicByModule() {
83+
Module module = ClassicEnvUtil.class.getModule();
84+
if (module == null)
85+
return null;
86+
87+
ModuleDescriptor md = module.getDescriptor();
88+
if (md == null)
89+
return null;
90+
Optional<String> opt = md.rawVersion();
91+
return opt.orElse(null);
92+
}
5793
}

logback-classic/src/main/java/ch/qos/logback/classic/util/ContextInitializer.java

+19-5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import ch.qos.logback.core.spi.ContextAware;
2121
import ch.qos.logback.core.spi.ContextAwareImpl;
2222
import ch.qos.logback.core.status.InfoStatus;
23+
import ch.qos.logback.core.status.WarnStatus;
2324
import ch.qos.logback.core.util.EnvUtil;
2425
import ch.qos.logback.core.util.Loader;
2526
import ch.qos.logback.core.util.StatusListenerConfigHelper;
@@ -72,11 +73,8 @@ public void autoConfig(ClassLoader classLoader) throws JoranException {
7273
// see https://github.com/qos-ch/logback/issues/715
7374
classLoader = Loader.systemClassloaderIfNull(classLoader);
7475

75-
String versionStr = EnvUtil.logbackVersion();
76-
if (versionStr == null) {
77-
versionStr = CoreConstants.NA;
78-
}
79-
loggerContext.getStatusManager().add(new InfoStatus(CoreConstants.LOGBACK_CLASSIC_VERSION_MESSAGE + versionStr, loggerContext));
76+
checkVersions();
77+
8078
StatusListenerConfigHelper.installIfAsked(loggerContext);
8179

8280

@@ -105,6 +103,22 @@ public void autoConfig(ClassLoader classLoader) throws JoranException {
105103
}
106104
}
107105

106+
private void checkVersions() {
107+
String versionOfLogbackClassic = ClassicEnvUtil.getVersionOfLogbackClassic();
108+
if (versionOfLogbackClassic == null) {
109+
versionOfLogbackClassic = CoreConstants.NA;
110+
}
111+
String versionOfLogbackCore = EnvUtil.logbackVersion();
112+
if (versionOfLogbackCore == null) {
113+
versionOfLogbackCore = CoreConstants.NA;
114+
}
115+
loggerContext.getStatusManager().add(new InfoStatus(ClassicConstants.LOGBACK_CLASSIC_VERSION_MESSAGE + versionOfLogbackClassic, loggerContext));
116+
if(!versionOfLogbackCore.equals(versionOfLogbackClassic)) {
117+
loggerContext.getStatusManager().add(new InfoStatus(CoreConstants.LOGBACK_CORE_VERSION_MESSAGE + versionOfLogbackCore, loggerContext));
118+
loggerContext.getStatusManager().add(new WarnStatus(ClassicConstants.LOGBACK_VERSIONS_MISMATCH, loggerContext));
119+
}
120+
}
121+
108122
private Configurator instantiateConfiguratorByClassName(String configuratorClassName, ClassLoader classLoader) {
109123
try {
110124
Class<?> classObj = classLoader.loadClass(configuratorClassName);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Logback: the reliable, generic, fast and flexible logging framework.
3+
* Copyright (C) 1999-2024, QOS.ch. All rights reserved.
4+
*
5+
* This program and the accompanying materials are dual-licensed under
6+
* either the terms of the Eclipse Public License v1.0 as published by
7+
* the Eclipse Foundation
8+
*
9+
* or (per the licensee's choosing)
10+
*
11+
* under the terms of the GNU Lesser General Public License version 2.1
12+
* as published by the Free Software Foundation.
13+
*/
14+
15+
package ch.qos.logback.classic.blackbox.util;
16+
17+
import ch.qos.logback.classic.util.ClassicEnvUtil;
18+
import ch.qos.logback.core.util.EnvUtil;
19+
import org.junit.jupiter.api.BeforeEach;
20+
import org.junit.jupiter.api.Test;
21+
22+
import static org.junit.jupiter.api.Assertions.*;
23+
24+
public class EnvUtilTest {
25+
26+
// Beware: ----------------------------------------
27+
// Beware: needs to be updated upon version change
28+
// Beware: ----------------------------------------
29+
static final String EXPECTED_VERSION = "1.5";
30+
31+
32+
@BeforeEach
33+
public void setUp() throws Exception {
34+
35+
}
36+
37+
// this test runs fine if run from logback-classic but fails when
38+
// run from logback-core. This is due to the fact that package information
39+
// is added when creating the jar.
40+
@Test
41+
public void versionTest() {
42+
String versionStr = EnvUtil.logbackVersion();
43+
assertNotNull(versionStr);
44+
assertTrue(versionStr.startsWith(EXPECTED_VERSION));
45+
}
46+
47+
@Test
48+
public void versionCompare() {
49+
String coreVersionStr = EnvUtil.logbackVersion();
50+
String versionOfLogbackClassic = ClassicEnvUtil.getVersionOfLogbackClassic();
51+
assertNotNull(coreVersionStr);
52+
assertNotNull(versionOfLogbackClassic);
53+
54+
assertEquals(coreVersionStr, versionOfLogbackClassic);
55+
}
56+
57+
58+
}

logback-classic/src/test/java/ch/qos/logback/classic/util/EnvUtilTest.java

-34
This file was deleted.

logback-core/src/main/java/ch/qos/logback/core/CoreConstants.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -252,11 +252,11 @@ public class CoreConstants {
252252
//public static final String RECONFIGURE_ON_CHANGE_TASK = "RECONFIGURE_ON_CHANGE_TASK";
253253
public static final String SIZE_AND_TIME_BASED_FNATP_IS_DEPRECATED = "SizeAndTimeBasedFileNamingAndTriggeringPolicy is deprecated. Use SizeAndTimeBasedRollingPolicy instead";
254254

255-
public static final String LOGBACK_CLASSIC_VERSION_MESSAGE = "This is logback-classic version ";
256255
public static final char JSON_LINE_SEPARATOR = '\n';
257256
final public static String MODEL_CONFIG_FILE_EXTENSION = ".scmo";
258257
/**
259258
* since 1.5.8
260259
*/
261260
final public static String PROPERTIES_FILE_EXTENSION = ".properties";
261+
public static final String LOGBACK_CORE_VERSION_MESSAGE = "This is logback-core version ";
262262
}

logback-core/src/main/java/ch/qos/logback/core/util/EnvUtil.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class EnvUtil {
2424
private EnvUtil() {
2525
}
2626

27+
2728
/**
2829
* <p>Returns the current version of logback, or null if data is not
2930
* available.
@@ -33,8 +34,8 @@ private EnvUtil() {
3334
* @return current version or null if missing version data
3435
*/
3536
static public String logbackVersion() {
36-
String moduleVersion = logbackVersionByModule();
37-
if(moduleVersion != null)
37+
String moduleVersion = getVersionOfLogbackCoreByModule();
38+
if (moduleVersion != null)
3839
return moduleVersion;
3940

4041
Package pkg = EnvUtil.class.getPackage();
@@ -52,7 +53,7 @@ static public String logbackVersion() {
5253
* @since 1.3.0
5354
* @return current version or null if missing version data
5455
*/
55-
static private String logbackVersionByModule() {
56+
static private String getVersionOfLogbackCoreByModule() {
5657
Module module = EnvUtil.class.getModule();
5758
if (module == null)
5859
return null;

0 commit comments

Comments
 (0)