Skip to content

Commit

Permalink
add version information at start time+ unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Ceki Gulcu <ceki@qos.ch>
  • Loading branch information
ceki committed Aug 21, 2022
1 parent a73ec12 commit 475281b
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 37 deletions.
45 changes: 23 additions & 22 deletions logback-classic/integration.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,21 @@
ant -Dslf4jVersion=1.5.4-SNAPSHOT
-->


<echo message="project.version: ${project.version}" />
<echo message="slf4j.version: ${slf4j.version}" />
<echo message="hamcrest: ${org.hamcrest:hamcrest-core:jar}" />

<path id="basicClasspath">
<!-- post junit version 4.12, we need to add hamcrest manually to the
classpath, even though the m-ant-plugin declares a dependency on hamcrest! -->
<pathelement location="${org.hamcrest:hamcrest-core:jar}" />
<pathelement location="../logback-core/target/classes/" />
<pathelement location="../logback-core/target/logback-core-${project.version}.jar/" />
<pathelement location="../logback-core/target/test-classes/" />
<pathelement location="./target/classes/" />
<pathelement location="./target/test-classes/" />
<pathelement location="./lib/slf4j-api-${slf4j.version}.jar" />
<pathelement location="./src/test/input/integration/autoInit/" />
<pathelement location="./src/test/input/integration/autoInit/" />
</path >


Expand All @@ -33,28 +34,28 @@
<target name="init" depends="copySLF4J_JAR">
<echo message="in init"/>
<mkdir dir="target/unit-reports" />
</target>

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

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


<target name="testWithoutGroovy" unless="maven.test.skip">
<junit printsummary="yes" fork="no" haltonfailure="yes">
<classpath refid="basicClasspath" />
<formatter type="plain" />
<test fork="yes" todir="target/unit-reports"
outfile="TEST-NoGroovy"
name="ch.qos.logback.classic.util.InitializationIntegrationTest" />
</junit>
<junit printsummary="yes" fork="no" haltonfailure="yes">
<classpath refid="basicClasspath" />
<formatter type="plain" />
<test fork="yes" todir="target/unit-reports"
outfile="TEST-NoGroovy"
name="ch.qos.logback.classic.util.InitializationIntegrationTest" />
</junit>
</target>


Expand Down
2 changes: 1 addition & 1 deletion logback-classic/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@
</goals>
</execution>
-->
<execution>
<execution>
<id>ant-integration-test</id>
<phase>package</phase>
<configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,20 @@
*/
package ch.qos.logback.classic.util;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Comparator;
import java.util.List;
import java.util.Set;

import ch.qos.logback.classic.BasicConfigurator;
import ch.qos.logback.classic.ClassicConstants;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.classic.spi.Configurator;
import ch.qos.logback.core.CoreConstants;
import ch.qos.logback.core.LogbackException;
import ch.qos.logback.core.joran.spi.JoranException;
import ch.qos.logback.core.status.ErrorStatus;
import ch.qos.logback.core.status.InfoStatus;
import ch.qos.logback.core.status.StatusManager;
import ch.qos.logback.core.status.WarnStatus;
import ch.qos.logback.core.util.Loader;
import ch.qos.logback.core.util.OptionHelper;
import ch.qos.logback.core.util.EnvUtil;
import ch.qos.logback.core.util.StatusListenerConfigHelper;

// contributors
Expand Down Expand Up @@ -85,6 +78,8 @@ public void autoConfig() throws JoranException {
}

public void autoConfig(ClassLoader classLoader) throws JoranException {
String versionStr = EnvUtil.ourVersion();
loggerContext.getStatusManager().add(new InfoStatus(CoreConstants.LOGBACK_CLASSIC_VERSION_MESSAGE + versionStr, loggerContext));
StatusListenerConfigHelper.installIfAsked(loggerContext);
List<Configurator> configuratorList = ClassicEnvUtil.loadFromServiceLoader(Configurator.class, classLoader);
sortByPriority(configuratorList);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ch.qos.logback.classic.util;

import ch.qos.logback.core.util.EnvUtil;
import org.junit.Test;

import static org.junit.Assert.*;

public class EnvUtilTest {


// 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.ourVersion();
assertNotNull(versionStr);
assertTrue(versionStr.startsWith("1.3"));
}




}
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,12 @@ public class CoreConstants {
* Replaced by {@link CoreConstants#UNBOUNDED_HISTORY} with the same identical value.
* @deprecated
* @see UNBOUNDED_HISTORY
* @see #UNBOUNDED_HISTORY
*/
public static final int UNBOUND_HISTORY = UNBOUNDED_HISTORY;

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

public static final String LOGBACK_CLASSIC_VERSION_MESSAGE = "This is logback-classic version ";
}
18 changes: 18 additions & 0 deletions logback-core/src/main/java/ch/qos/logback/core/util/EnvUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
package ch.qos.logback.core.util;

import ch.qos.logback.core.CoreConstants;

/**
* @author Ceki G&uuml;lc&uuml;
*/
Expand All @@ -21,6 +23,22 @@ public class EnvUtil {
private EnvUtil() {
}

/**
* <p>Returns the current version of logback, or null if data is not
* available.
* </p>
*
* @since 1.3.0
* @return current version or null if missing version data
*/
static public String ourVersion() {
Package pkg = EnvUtil.class.getPackage();
if(pkg == null) {
return null;
}
return pkg.getImplementationVersion();
}

static public int getJDKVersion(String javaVersionStr) {
int version = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
*/
package ch.qos.logback.core.util;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import ch.qos.logback.core.CoreConstants;
import org.junit.After;
import org.junit.Test;
import org.mockito.Mock;

import static org.junit.Assert.*;
import static org.junit.Assert.assertNotEquals;

/**
* @author Patrick Reinhart
*/
Expand Down

0 comments on commit 475281b

Please sign in to comment.