Improved log4j2 configuration and fixed build process.

This commit is contained in:
Christian P. MOMON 2022-01-04 02:27:14 +01:00
parent efe5b00ae3
commit 65354e89cc
4 changed files with 68 additions and 32 deletions

View file

@ -11,8 +11,7 @@
<property name="test.classes" value="${build.dir}/test-classes" /> <property name="test.classes" value="${build.dir}/test-classes" />
<property name="debug" value="on" /> <property name="debug" value="on" />
<path id="project.libs"> <path id="project.libs">
<fileset dir="${basedir}/lib" includes="**/*.jar" excludes="*sources* *src*" /> <fileset dir="${basedir}/lib" includes="**/*.jar" excludes="*sources* **/*sources* *src* **/*src*" />
<!--fileset dir="${basedir}/lib/Logs" includes="**.jar" excludes="*sources* *src*" /-->
</path> </path>
<property name="classpath.absolute" value="${toString:project.libs}" /> <property name="classpath.absolute" value="${toString:project.libs}" />
@ -95,26 +94,18 @@
<entry key="product.revision.author" value="${user.name}"/> <entry key="product.revision.author" value="${user.name}"/>
</propertyfile> </propertyfile>
<!-- Build the jar file list. --> <!-- Build the jar file list. -->
<!-- Package jar --> <!-- Package jar -->
<property name="dist.jar" value="${dist.dir}/${product.name}.jar" /> <property name="dist.jar" value="${dist.dir}/${product.name}.jar" />
<jar destfile="${dist.jar}"> <jar destfile="${dist.jar}">
<restrict> <fileset dir="${build.classes}" />
<name regex=".*/*\.(class|gif|ico|jpg|ngp|png|pom|properties|svg@|xml|xsd)"/> <zipgroupfileset dir="${basedir}/lib" includes="*.jar **/*.jar" excludes="*sources* **/*sources* *src* **/*src*" />
<archives>
<zips>
<fileset dir="${basedir}/lib" includes="**/*.jar" excludes="*sources* *src*" />
</zips>
</archives>
</restrict>
<manifest> <manifest>
<attribute name="Built-By" value="${user.name} using ant" /> <attribute name="Built-By" value="${user.name} using ant" />
<attribute name="Built-Date" value="${dist.time}" /> <attribute name="Built-Date" value="${dist.time}" />
<attribute name="Main-Class" value = "fr.devinsy.statoolinfos.StatoolInfosLauncher"/> <attribute name="Main-Class" value = "fr.devinsy.statoolinfos.StatoolInfosLauncher"/>
</manifest> <attribute name="Multi-Release" value = "true"/>
<fileset dir="${build.classes}" /> </manifest>
<!--zipfileset dir="${basedir}/lib" includes="${toString:project.libs}" /-->
</jar> </jar>
<!-- Package sources --> <!-- Package sources -->

View file

@ -1,10 +1,21 @@
#!/bin/bash #!/bin/bash
# Java check. # Optional environment settings:
# export LOG4J_CONFIGURATION_FILE="toto.properties"
# export LOG4J_LEVEL=ERROR
# https://logging.apache.org/log4j/log4j-2.11.2/manual/configuration.html#System_Properties
# Java check.
javaCheck=`which java` javaCheck=`which java`
if [[ "$javaCheck" =~ ^/.* ]]; then if [[ "$javaCheck" =~ ^/.* ]]; then
#echo "Java requirement............... OK" #echo "Java requirement............... OK"
java -Djava.awt.headless=true -jar "$(dirname "$0")"/statoolinfos.jar $@
# Optional system properties:
# LOGFILE="-Dlog4j2.configurationFile=../../log4j2.properties"
# LOGLEVEL="-Dlog4j2.level=ERROR"
# https://logging.apache.org/log4j/log4j-2.11.2/manual/configuration.html#System_Properties
java -Djava.awt.headless=true $LOGFILE $LOGLEVEL -jar "$(dirname "$0")"/statoolinfos.jar $@
else else
echo "Java requirement............... MISSING" echo "Java requirement............... MISSING"
fi fi

3
snapshot Executable file
View file

@ -0,0 +1,3 @@
#!/bin/bash
./build.sh -snapshot

View file

@ -20,9 +20,15 @@ package fr.devinsy.statoolinfos;
import java.io.File; import java.io.File;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.Level; import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.appender.ConsoleAppender;
import org.apache.logging.log4j.core.config.Configurator; import org.apache.logging.log4j.core.config.Configurator;
import org.apache.logging.log4j.core.config.DefaultConfiguration; import org.apache.logging.log4j.core.config.builder.api.AppenderComponentBuilder;
import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder;
import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory;
import org.apache.logging.log4j.core.config.builder.api.RootLoggerComponentBuilder;
import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -33,8 +39,6 @@ import fr.devinsy.statoolinfos.cli.StatoolInfosCLI;
*/ */
public final class StatoolInfosLauncher public final class StatoolInfosLauncher
{ {
private static Logger logger = LoggerFactory.getLogger(StatoolInfosLauncher.class);
/** /**
* Instantiates a new statool infos launcher. * Instantiates a new statool infos launcher.
*/ */
@ -50,18 +54,45 @@ public final class StatoolInfosLauncher
*/ */
public static void main(final String[] args) public static void main(final String[] args)
{ {
String logFileEnv = System.getenv().get("LOG4J_CONFIGURATION_FILE");
String logFileProperty = System.getProperty("log4j2.configurationFile");
// Configure log. // Configure log.
File loggerConfig = new File("log4j.properties"); if ((StringUtils.isBlank(logFileEnv) && (StringUtils.isBlank(logFileProperty))))
if (loggerConfig.exists())
{ {
Configurator.initialize(null, loggerConfig.getAbsolutePath()); File loggerConfig = new File("log4j2.properties");
logger.info("Dedicated log configuration done."); if (loggerConfig.exists())
logger.info("Configuration file was found in [{}].", loggerConfig.getAbsoluteFile()); {
} // Try to initialize logs with a log file.
else Configurator.initialize(null, loggerConfig.getAbsolutePath());
{ Logger logger = LoggerFactory.getLogger(StatoolInfosLauncher.class);
Configurator.initialize(new DefaultConfiguration()); logger.info("Dedicated log configuration done.");
Configurator.setRootLevel(Level.INFO); logger.info("Configuration file was found in [{}].", loggerConfig.getAbsoluteFile());
}
else
{
// Build a custom default log configuration.
ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();
builder.setStatusLevel(Level.ERROR);
builder.setConfigurationName("CustomDefaultLogger");
AppenderComponentBuilder appenderBuilder = builder.newAppender("Console", "CONSOLE").addAttribute("target", ConsoleAppender.Target.SYSTEM_OUT);
appenderBuilder.add(builder.newLayout("PatternLayout").addAttribute("pattern", "%m%n"));
RootLoggerComponentBuilder rootLogger = builder.newRootLogger(Level.INFO);
rootLogger.add(builder.newAppenderRef("Console"));
builder.add(appenderBuilder);
builder.add(rootLogger);
Configurator.reconfigure(builder.build());
Logger logger = LoggerFactory.getLogger(StatoolInfosLauncher.class);
logger.debug("Custom default log configuration done.");
/*
Configurator.initialize(new DefaultConfiguration());
Configurator.setRootLevel(Level.INFO);
Logger logger = LoggerFactory.getLogger(StatoolInfosLauncher.class);
logger.debug("Default log configuration done.");
*/
}
} }
// Run. // Run.