From 3f8094993eb00bd639fcead7943e5cddf271753b Mon Sep 17 00:00:00 2001 From: Louis Calas Date: Thu, 3 Oct 2019 21:40:08 +0200 Subject: [PATCH] Log system v1 (to add to all classes) --- NGCC/resources/log4j2.properties | 50 ++++++++++++++++++++++++++++++++ NGCC/src/Exec.java | 4 +-- NGCC/src/commands/Read.java | 17 +++++++++++ NGCC/tests/log4j/TestLog4j.java | 25 +++++++++++++++- 4 files changed, 93 insertions(+), 3 deletions(-) create mode 100644 NGCC/resources/log4j2.properties diff --git a/NGCC/resources/log4j2.properties b/NGCC/resources/log4j2.properties new file mode 100644 index 0000000..f796a68 --- /dev/null +++ b/NGCC/resources/log4j2.properties @@ -0,0 +1,50 @@ +name=PropertiesConfig +property.filename = logs +appenders = console, file + + +# Création des appenders (interfaces) pour les sorties fichier et console +appender.console.type = Console +appender.console.name = STDOUT +appender.console.layout.type = PatternLayout +appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n + +appender.file.type = File +appender.file.name = LOGFILE +appender.file.fileName=${filename}/ngcclogs.log +appender.file.layout.type=PatternLayout +appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n + + +#Création des loggers + +loggers = console, f, d + +logger.f.name = fatalLogger +logger.f.level = fatal +logger.f.additivity = false +logger.f.appenderRefs = console +logger.f.appenderRef.console.ref = STDOUT +logger.f.appenderRef.file.ref = LOGFILE + +logger.d.name = debugLogger +logger.d.level = debug +logger.d.additivity = false +logger.d.appenderRefs = console +logger.d.appenderRef.console.ref = STDOUT +logger.d.appenderRef.file.ref = LOGFILE + + +logger.console.name = log4j +logger.console.level = fatal +logger.console.additivity = false +logger.console.appenderRefs = console +logger.console.appenderRef.console.ref = STDOUT +logger.console.appenderRef.file.ref = LOGFILE + + +# Root Logger (par défaut) +rootLogger.level = debug +rootLogger.appenderRefs = stdout, file +rootLogger.appenderRef.stdout.ref = STDOUT +rootLogger.appenderRef.file.ref = LOGFILE \ No newline at end of file diff --git a/NGCC/src/Exec.java b/NGCC/src/Exec.java index 4b51d5d..b1aac91 100644 --- a/NGCC/src/Exec.java +++ b/NGCC/src/Exec.java @@ -41,7 +41,7 @@ public class Exec implements Callable { public static void main(String[] args) throws InterruptedException { // Système de subcommand semblable à celui de git permettant d'appeler les classes relatives - String[] t = {"-r","-d","pdf"}; + //String[] t = {"-r","-v1","-d","pdf"}; CommandLine cmd = new CommandLine (new Exec()) .addSubcommand("-r", new Read(System.out)) // nom commande, objet commande .addSubcommand("-b", new Build(System.out)) @@ -51,7 +51,7 @@ public class Exec implements Callable { .addSubcommand("-e", new Evaluate(System.out)) .addSubcommand("help", new HelpCommand()); // Aide générée automatiquement par l'API - cmd.execute(t); + cmd.execute(args); //t } diff --git a/NGCC/src/commands/Read.java b/NGCC/src/commands/Read.java index 460d5ce..a5abf29 100644 --- a/NGCC/src/commands/Read.java +++ b/NGCC/src/commands/Read.java @@ -8,6 +8,9 @@ import progressbar.ProgressBar; import java.io.PrintStream; import java.util.concurrent.Callable; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + import config.Config; import csv.GenerateCSV; import ocr.GestionnaireCopies; @@ -31,6 +34,8 @@ import ocr.GestionnaireCopies; public class Read implements Callable { + private static Logger logger; + @Spec Model.CommandSpec spec; @@ -67,7 +72,17 @@ public class Read implements Callable { @Override public Void call() throws Exception { + if (vb_level >= 0 && vb_level <=2) { + logger = LogManager.getFormatterLogger("fatalLogger"); + } + else { + logger = LogManager.getFormatterLogger("debugLogger"); + } + + if(help) { + logger.fatal("help command for -r used"); + logger.debug("debug"); CommandLine.usage(this.spec, System.out); } else { @@ -93,6 +108,8 @@ public class Read implements Callable { //*************************************************** + logger.fatal("Read command executed"); + logger.debug("Reading begin"); System.out.println("\nRead mode activated ...\n"); // Debug des paramètres à exploiter plus tard System.out.println("Update : "+step +"\n"+ diff --git a/NGCC/tests/log4j/TestLog4j.java b/NGCC/tests/log4j/TestLog4j.java index 28581fc..d011163 100644 --- a/NGCC/tests/log4j/TestLog4j.java +++ b/NGCC/tests/log4j/TestLog4j.java @@ -10,7 +10,7 @@ import org.junit.jupiter.api.Test; class TestLog4j { - private static Logger logger = LogManager.getLogger(LogManager.getLogger("ngccLog")); + private static Logger logger = LogManager.getFormatterLogger("log4j"); public static void main(String[] args) { @@ -21,5 +21,28 @@ class TestLog4j { logger.fatal("msg d'erreur fatale"); } + + /* Idee verbosité + * + * Logger logger; + * + * if (v >= 0 && v <= 2) { + * logger = LogManager.getLogger(LogManager.getLogger("fatalLogger")); + * } + * else if (v >= 3 && v <= 4) { + * logger = LogManager.getLogger(LogManager.getLogger("errorLogger")); + * } + * else if (v >= 5 && v <= 6) { + * logger = LogManager.getLogger(LogManager.getLogger("warnLogger")); + * } + * else if (v >= 7 && v <= 8) { + * logger = LogManager.getLogger(LogManager.getLogger("infoLogger")); + * } + * else { + * logger = LogManager.getLogger(LogManager.getLogger("debugLogger")); + * } + * + * -> On met quand même logger.warn(...) et il apparaitra ou non selon le logger + */ }