From 65354e89cc8b0787bedb51ab91341b69efbfa0b1 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Tue, 4 Jan 2022 02:27:14 +0100 Subject: [PATCH] Improved log4j2 configuration and fixed build process. --- build-appjar.xml | 19 ++----- resources/scripts/statoolinfos.sh | 21 +++++-- snapshot | 3 + .../statoolinfos/StatoolInfosLauncher.java | 57 ++++++++++++++----- 4 files changed, 68 insertions(+), 32 deletions(-) create mode 100755 snapshot diff --git a/build-appjar.xml b/build-appjar.xml index d19f06d..9a33972 100644 --- a/build-appjar.xml +++ b/build-appjar.xml @@ -11,8 +11,7 @@ - - + @@ -95,26 +94,18 @@ - - - - - - - - - + + - - - + + diff --git a/resources/scripts/statoolinfos.sh b/resources/scripts/statoolinfos.sh index 78690bf..03e9d0d 100755 --- a/resources/scripts/statoolinfos.sh +++ b/resources/scripts/statoolinfos.sh @@ -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 diff --git a/snapshot b/snapshot new file mode 100755 index 0000000..44bbfc6 --- /dev/null +++ b/snapshot @@ -0,0 +1,3 @@ +#!/bin/bash + +./build.sh -snapshot diff --git a/src/fr/devinsy/statoolinfos/StatoolInfosLauncher.java b/src/fr/devinsy/statoolinfos/StatoolInfosLauncher.java index b603689..d07b413 100644 --- a/src/fr/devinsy/statoolinfos/StatoolInfosLauncher.java +++ b/src/fr/devinsy/statoolinfos/StatoolInfosLauncher.java @@ -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 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.