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="debug" value="on" />
<path id="project.libs">
<fileset dir="${basedir}/lib" includes="**/*.jar" excludes="*sources* *src*" />
<!--fileset dir="${basedir}/lib/Logs" includes="**.jar" excludes="*sources* *src*" /-->
<fileset dir="${basedir}/lib" includes="**/*.jar" excludes="*sources* **/*sources* *src* **/*src*" />
</path>
<property name="classpath.absolute" value="${toString:project.libs}" />
@ -95,26 +94,18 @@
<entry key="product.revision.author" value="${user.name}"/>
</propertyfile>
<!-- Build the jar file list. -->
<!-- Package jar -->
<property name="dist.jar" value="${dist.dir}/${product.name}.jar" />
<jar destfile="${dist.jar}">
<restrict>
<name regex=".*/*\.(class|gif|ico|jpg|ngp|png|pom|properties|svg@|xml|xsd)"/>
<archives>
<zips>
<fileset dir="${basedir}/lib" includes="**/*.jar" excludes="*sources* *src*" />
</zips>
</archives>
</restrict>
<fileset dir="${build.classes}" />
<zipgroupfileset dir="${basedir}/lib" includes="*.jar **/*.jar" excludes="*sources* **/*sources* *src* **/*src*" />
<manifest>
<attribute name="Built-By" value="${user.name} using ant" />
<attribute name="Built-Date" value="${dist.time}" />
<attribute name="Main-Class" value = "fr.devinsy.statoolinfos.StatoolInfosLauncher"/>
</manifest>
<fileset dir="${build.classes}" />
<!--zipfileset dir="${basedir}/lib" includes="${toString:project.libs}" /-->
<attribute name="Multi-Release" value = "true"/>
</manifest>
</jar>
<!-- 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`
if [[ "$javaCheck" =~ ^/.* ]]; then
#echo "Java requirement............... OK"
java -Djava.awt.headless=true -jar "$(dirname "$0")"/statoolinfos.jar $@
#echo "Java requirement............... OK"
# 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
echo "Java requirement............... MISSING"
echo "Java requirement............... MISSING"
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 org.apache.commons.lang3.StringUtils;
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.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.LoggerFactory;
@ -33,8 +39,6 @@ import fr.devinsy.statoolinfos.cli.StatoolInfosCLI;
*/
public final class StatoolInfosLauncher
{
private static Logger logger = LoggerFactory.getLogger(StatoolInfosLauncher.class);
/**
* Instantiates a new statool infos launcher.
*/
@ -50,18 +54,45 @@ public final class StatoolInfosLauncher
*/
public static void main(final String[] args)
{
String logFileEnv = System.getenv().get("LOG4J_CONFIGURATION_FILE");
String logFileProperty = System.getProperty("log4j2.configurationFile");
// Configure log.
File loggerConfig = new File("log4j.properties");
if (loggerConfig.exists())
if ((StringUtils.isBlank(logFileEnv) && (StringUtils.isBlank(logFileProperty))))
{
Configurator.initialize(null, loggerConfig.getAbsolutePath());
logger.info("Dedicated log configuration done.");
logger.info("Configuration file was found in [{}].", loggerConfig.getAbsoluteFile());
}
else
{
Configurator.initialize(new DefaultConfiguration());
Configurator.setRootLevel(Level.INFO);
File loggerConfig = new File("log4j2.properties");
if (loggerConfig.exists())
{
// Try to initialize logs with a log file.
Configurator.initialize(null, loggerConfig.getAbsolutePath());
Logger logger = LoggerFactory.getLogger(StatoolInfosLauncher.class);
logger.info("Dedicated log configuration done.");
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.