Skip to content

Commit 475281b

Browse files
committed
add version information at start time+ unit test
Signed-off-by: Ceki Gulcu <ceki@qos.ch>
1 parent a73ec12 commit 475281b

File tree

7 files changed

+76
-37
lines changed

7 files changed

+76
-37
lines changed

logback-classic/integration.xml

+23-22
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,21 @@
99
1010
ant -Dslf4jVersion=1.5.4-SNAPSHOT
1111
-->
12-
12+
13+
<echo message="project.version: ${project.version}" />
1314
<echo message="slf4j.version: ${slf4j.version}" />
1415
<echo message="hamcrest: ${org.hamcrest:hamcrest-core:jar}" />
1516

1617
<path id="basicClasspath">
1718
<!-- post junit version 4.12, we need to add hamcrest manually to the
1819
classpath, even though the m-ant-plugin declares a dependency on hamcrest! -->
1920
<pathelement location="${org.hamcrest:hamcrest-core:jar}" />
20-
<pathelement location="../logback-core/target/classes/" />
21+
<pathelement location="../logback-core/target/logback-core-${project.version}.jar/" />
2122
<pathelement location="../logback-core/target/test-classes/" />
2223
<pathelement location="./target/classes/" />
2324
<pathelement location="./target/test-classes/" />
2425
<pathelement location="./lib/slf4j-api-${slf4j.version}.jar" />
25-
<pathelement location="./src/test/input/integration/autoInit/" />
26+
<pathelement location="./src/test/input/integration/autoInit/" />
2627
</path >
2728

2829

@@ -33,28 +34,28 @@
3334
<target name="init" depends="copySLF4J_JAR">
3435
<echo message="in init"/>
3536
<mkdir dir="target/unit-reports" />
36-
</target>
37-
38-
<target name="copySLF4J_JAR">
39-
<echo>Making lib/ folder in case it does not already exist.</echo>
40-
<mkdir dir="lib/"/>
41-
<echo>Copying ${org.slf4j:slf4j-api:jar} to lib/</echo>
42-
<!-- sneak in a cleanup procedure of the ../logback-examples/lib/ folder-->
43-
<delete quiet="true">
44-
<fileset dir="../logback-examples/lib/" includes="slf4j-*SNAPSHOT.jar" />
45-
</delete>
46-
<copy file="${org.slf4j:slf4j-api:jar}" todir="lib/" />
47-
</target>
37+
</target>
4838

39+
<target name="copySLF4J_JAR">
40+
<echo>Making lib/ folder in case it does not already exist.</echo>
41+
<mkdir dir="lib/"/>
42+
<echo>Copying ${org.slf4j:slf4j-api:jar} to lib/</echo>
43+
<!-- sneak in a cleanup procedure of the ../logback-examples/lib/ folder-->
44+
<delete quiet="true">
45+
<fileset dir="../logback-examples/lib/" includes="slf4j-*SNAPSHOT.jar" />
46+
</delete>
47+
<copy file="${org.slf4j:slf4j-api:jar}" todir="lib/" />
48+
</target>
49+
4950

5051
<target name="testWithoutGroovy" unless="maven.test.skip">
51-
<junit printsummary="yes" fork="no" haltonfailure="yes">
52-
<classpath refid="basicClasspath" />
53-
<formatter type="plain" />
54-
<test fork="yes" todir="target/unit-reports"
55-
outfile="TEST-NoGroovy"
56-
name="ch.qos.logback.classic.util.InitializationIntegrationTest" />
57-
</junit>
52+
<junit printsummary="yes" fork="no" haltonfailure="yes">
53+
<classpath refid="basicClasspath" />
54+
<formatter type="plain" />
55+
<test fork="yes" todir="target/unit-reports"
56+
outfile="TEST-NoGroovy"
57+
name="ch.qos.logback.classic.util.InitializationIntegrationTest" />
58+
</junit>
5859
</target>
5960

6061

logback-classic/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@
208208
</goals>
209209
</execution>
210210
-->
211-
<execution>
211+
<execution>
212212
<id>ant-integration-test</id>
213213
<phase>package</phase>
214214
<configuration>

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

+4-9
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,20 @@
1313
*/
1414
package ch.qos.logback.classic.util;
1515

16-
import java.io.File;
17-
import java.io.IOException;
18-
import java.net.MalformedURLException;
1916
import java.net.URL;
2017
import java.util.Comparator;
2118
import java.util.List;
22-
import java.util.Set;
2319

2420
import ch.qos.logback.classic.BasicConfigurator;
2521
import ch.qos.logback.classic.ClassicConstants;
2622
import ch.qos.logback.classic.LoggerContext;
2723
import ch.qos.logback.classic.joran.JoranConfigurator;
2824
import ch.qos.logback.classic.spi.Configurator;
25+
import ch.qos.logback.core.CoreConstants;
2926
import ch.qos.logback.core.LogbackException;
3027
import ch.qos.logback.core.joran.spi.JoranException;
31-
import ch.qos.logback.core.status.ErrorStatus;
3228
import ch.qos.logback.core.status.InfoStatus;
33-
import ch.qos.logback.core.status.StatusManager;
34-
import ch.qos.logback.core.status.WarnStatus;
35-
import ch.qos.logback.core.util.Loader;
36-
import ch.qos.logback.core.util.OptionHelper;
29+
import ch.qos.logback.core.util.EnvUtil;
3730
import ch.qos.logback.core.util.StatusListenerConfigHelper;
3831

3932
// contributors
@@ -85,6 +78,8 @@ public void autoConfig() throws JoranException {
8578
}
8679

8780
public void autoConfig(ClassLoader classLoader) throws JoranException {
81+
String versionStr = EnvUtil.ourVersion();
82+
loggerContext.getStatusManager().add(new InfoStatus(CoreConstants.LOGBACK_CLASSIC_VERSION_MESSAGE + versionStr, loggerContext));
8883
StatusListenerConfigHelper.installIfAsked(loggerContext);
8984
List<Configurator> configuratorList = ClassicEnvUtil.loadFromServiceLoader(Configurator.class, classLoader);
9085
sortByPriority(configuratorList);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package ch.qos.logback.classic.util;
2+
3+
import ch.qos.logback.core.util.EnvUtil;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.*;
7+
8+
public class EnvUtilTest {
9+
10+
11+
// this test runs fine if run from logback-classic but fails when
12+
// run from logback-core. This is due to the fact that package information
13+
// is added when creating the jar.
14+
@Test
15+
public void versionTest() {
16+
String versionStr = EnvUtil.ourVersion();
17+
assertNotNull(versionStr);
18+
assertTrue(versionStr.startsWith("1.3"));
19+
}
20+
21+
22+
23+
24+
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,12 @@ public class CoreConstants {
217217
* Replaced by {@link CoreConstants#UNBOUNDED_HISTORY} with the same identical value.
218218
219219
* @deprecated
220-
* @see UNBOUNDED_HISTORY
220+
* @see #UNBOUNDED_HISTORY
221221
*/
222222
public static final int UNBOUND_HISTORY = UNBOUNDED_HISTORY;
223223

224224
public static final String RECONFIGURE_ON_CHANGE_TASK = "RECONFIGURE_ON_CHANGE_TASK";
225225
public static final String SIZE_AND_TIME_BASED_FNATP_IS_DEPRECATED = "SizeAndTimeBasedFNATP is deprecated. Use SizeAndTimeBasedRollingPolicy instead";
226226

227+
public static final String LOGBACK_CLASSIC_VERSION_MESSAGE = "This is logback-classic version ";
227228
}

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

+18
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
*/
1414
package ch.qos.logback.core.util;
1515

16+
import ch.qos.logback.core.CoreConstants;
17+
1618
/**
1719
* @author Ceki G&uuml;lc&uuml;
1820
*/
@@ -21,6 +23,22 @@ public class EnvUtil {
2123
private EnvUtil() {
2224
}
2325

26+
/**
27+
* <p>Returns the current version of logback, or null if data is not
28+
* available.
29+
* </p>
30+
*
31+
* @since 1.3.0
32+
* @return current version or null if missing version data
33+
*/
34+
static public String ourVersion() {
35+
Package pkg = EnvUtil.class.getPackage();
36+
if(pkg == null) {
37+
return null;
38+
}
39+
return pkg.getImplementationVersion();
40+
}
41+
2442
static public int getJDKVersion(String javaVersionStr) {
2543
int version = 0;
2644

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
*/
1414
package ch.qos.logback.core.util;
1515

16-
import static org.junit.Assert.assertEquals;
17-
import static org.junit.Assert.assertFalse;
18-
import static org.junit.Assert.assertTrue;
19-
16+
import ch.qos.logback.core.CoreConstants;
2017
import org.junit.After;
2118
import org.junit.Test;
2219
import org.mockito.Mock;
2320

21+
import static org.junit.Assert.*;
22+
import static org.junit.Assert.assertNotEquals;
23+
2424
/**
2525
* @author Patrick Reinhart
2626
*/

0 commit comments

Comments
 (0)