From 3270256ca0826e2da73052c3b01f8f2b4c602a16 Mon Sep 17 00:00:00 2001 From: IanFH <101397272+IanFH@users.noreply.github.com> Date: Fri, 22 Mar 2024 17:58:40 +0800 Subject: [PATCH 1/2] Successfully set up file handler. Florizz.xml file will appear if for Florizz main class logger --- FlorizzLogger.xml | 8 ++++++ src/main/java/florizz/core/Florizz.java | 9 ++++--- src/main/java/florizz/core/Parser.java | 5 ++-- src/main/java/florizz/logging/MyFilter.java | 25 +++++++++++++++++++ .../java/florizz/logging/MyFormatter.java | 24 ++++++++++++++++++ 5 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 FlorizzLogger.xml create mode 100644 src/main/java/florizz/logging/MyFilter.java create mode 100644 src/main/java/florizz/logging/MyFormatter.java diff --git a/FlorizzLogger.xml b/FlorizzLogger.xml new file mode 100644 index 0000000000..f5985853b7 --- /dev/null +++ b/FlorizzLogger.xml @@ -0,0 +1,8 @@ +1::florizz.core.Florizz::main::Fri Mar 22 17:57:02 SGT 2024::Entered isRunning while loop in Florizz.java +1::Parser::parse::Fri Mar 22 17:57:07 SGT 2024::ENTRY +1::florizz.core.Parser::parse::Fri Mar 22 17:57:07 SGT 2024::commandHandler handled command successfully +1::Parser::parse::Fri Mar 22 17:57:07 SGT 2024::RETURN +1::florizz.core.Florizz::main::Fri Mar 22 17:57:07 SGT 2024::Entered isRunning while loop in Florizz.java +1::Parser::parse::Fri Mar 22 17:57:08 SGT 2024::ENTRY +1::florizz.core.Parser::parse::Fri Mar 22 17:57:08 SGT 2024::commandHandler handled command successfully +1::Parser::parse::Fri Mar 22 17:57:08 SGT 2024::RETURN diff --git a/src/main/java/florizz/core/Florizz.java b/src/main/java/florizz/core/Florizz.java index 096620fcb2..b1f686a8ef 100644 --- a/src/main/java/florizz/core/Florizz.java +++ b/src/main/java/florizz/core/Florizz.java @@ -1,8 +1,11 @@ package florizz.core; import florizz.command.Command; +import florizz.logging.MyFilter; +import florizz.logging.MyFormatter; import florizz.objects.Bouquet; +import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.logging.ConsoleHandler; @@ -32,6 +35,7 @@ public static void main(String[] args) { assert tempBouquetList !=null : "tempBouquetList doesn't exist"; // Set up logger + LogManager.getLogManager().reset(); logger.setLevel(Level.ALL); @@ -42,11 +46,10 @@ public static void main(String[] args) { // Set up file handler try { - FileHandler fh = new FileHandler(); + FileHandler fh = new FileHandler("./FlorizzLogger.xml", 2000, 1); + fh.setFormatter(new MyFormatter()); fh.setLevel(Level.ALL); logger.addHandler(fh); - //System.out.println("FileHandler opened"); - //System.out.println(Florizz.class.getClassLoader().getResource("logging.properties")); } catch (IOException e) { ui.printIOError(); } diff --git a/src/main/java/florizz/core/Parser.java b/src/main/java/florizz/core/Parser.java index 1d5e4169a7..b9777e2dbb 100644 --- a/src/main/java/florizz/core/Parser.java +++ b/src/main/java/florizz/core/Parser.java @@ -13,10 +13,11 @@ import florizz.command.HelpCommand; import florizz.objects.Bouquet; +import java.util.logging.Level; import java.util.logging.Logger; public class Parser { - private static Logger logger = Logger.getLogger(Parser.class.getName()); + private static Logger logger = Logger.getLogger(Florizz.class.getName()); // prefixes to parse input private static final String QUANTITY = "/q"; private static final String ADD_FLOWER_PREFIX = "/to"; @@ -30,7 +31,7 @@ public static Command parse (String input) throws FlorizzException{ logger.entering("Parser", "parse"); String[] decodedInput = commandHandler(input); - //logger.log(Level.INFO, "commandHandler handled command successfully"); + logger.log(Level.INFO, "commandHandler handled command successfully"); Command command; switch (decodedInput[0]){ case ("mybouquets"): diff --git a/src/main/java/florizz/logging/MyFilter.java b/src/main/java/florizz/logging/MyFilter.java new file mode 100644 index 0000000000..c33d3d8311 --- /dev/null +++ b/src/main/java/florizz/logging/MyFilter.java @@ -0,0 +1,25 @@ +package florizz.logging; + +import java.util.logging.Filter; +import java.util.logging.Level; +import java.util.logging.LogRecord; + +public class MyFilter implements Filter { + + /** + * Determine what level should a handler take + * Code taken from "https://www.digitalocean.com/community/tutorials/logger-in-java-logging-example" + * + * @param log a LogRecord + * @return + */ + @Override + public boolean isLoggable(LogRecord log) { + //don't log CONFIG logs in file + if (log.getLevel() == Level.CONFIG) { + return false; + } + return true; + } + +} diff --git a/src/main/java/florizz/logging/MyFormatter.java b/src/main/java/florizz/logging/MyFormatter.java new file mode 100644 index 0000000000..7bd1f16456 --- /dev/null +++ b/src/main/java/florizz/logging/MyFormatter.java @@ -0,0 +1,24 @@ +package florizz.logging; + +import java.util.Date; +import java.util.logging.Formatter; +import java.util.logging.LogRecord; + +public class MyFormatter extends Formatter { + + /** + * Better format logging output + * Code taken from "https://www.digitalocean.com/community/tutorials/logger-in-java-logging-example" + * + * @param record the log record to be formatted. + * @return + */ + @Override + public String format(LogRecord record) { + return record.getThreadID()+"::"+record.getSourceClassName()+"::" + +record.getSourceMethodName()+"::" + +new Date(record.getMillis())+"::" + +record.getMessage()+"\n"; + } + +} From 5672f2ab1e64b6500d75234b3fd76f72620f9b96 Mon Sep 17 00:00:00 2001 From: IanFH <101397272+IanFH@users.noreply.github.com> Date: Fri, 22 Mar 2024 18:10:02 +0800 Subject: [PATCH 2/2] fixed checkStyle issue --- FlorizzLogger.xml | 9 +-------- FlorizzLogger.xml.lck | 0 src/main/java/florizz/core/Florizz.java | 2 -- 3 files changed, 1 insertion(+), 10 deletions(-) create mode 100644 FlorizzLogger.xml.lck diff --git a/FlorizzLogger.xml b/FlorizzLogger.xml index f5985853b7..50397a0816 100644 --- a/FlorizzLogger.xml +++ b/FlorizzLogger.xml @@ -1,8 +1 @@ -1::florizz.core.Florizz::main::Fri Mar 22 17:57:02 SGT 2024::Entered isRunning while loop in Florizz.java -1::Parser::parse::Fri Mar 22 17:57:07 SGT 2024::ENTRY -1::florizz.core.Parser::parse::Fri Mar 22 17:57:07 SGT 2024::commandHandler handled command successfully -1::Parser::parse::Fri Mar 22 17:57:07 SGT 2024::RETURN -1::florizz.core.Florizz::main::Fri Mar 22 17:57:07 SGT 2024::Entered isRunning while loop in Florizz.java -1::Parser::parse::Fri Mar 22 17:57:08 SGT 2024::ENTRY -1::florizz.core.Parser::parse::Fri Mar 22 17:57:08 SGT 2024::commandHandler handled command successfully -1::Parser::parse::Fri Mar 22 17:57:08 SGT 2024::RETURN +1::florizz.core.Florizz::main::Fri Mar 22 18:09:06 SGT 2024::Entered isRunning while loop in Florizz.java diff --git a/FlorizzLogger.xml.lck b/FlorizzLogger.xml.lck new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/main/java/florizz/core/Florizz.java b/src/main/java/florizz/core/Florizz.java index b1f686a8ef..fcee4e1e0e 100644 --- a/src/main/java/florizz/core/Florizz.java +++ b/src/main/java/florizz/core/Florizz.java @@ -1,11 +1,9 @@ package florizz.core; import florizz.command.Command; -import florizz.logging.MyFilter; import florizz.logging.MyFormatter; import florizz.objects.Bouquet; -import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.logging.ConsoleHandler;