diff --git a/FlorizzLogger.xml b/FlorizzLogger.xml new file mode 100644 index 0000000000..50397a0816 --- /dev/null +++ b/FlorizzLogger.xml @@ -0,0 +1 @@ +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 096620fcb2..fcee4e1e0e 100644 --- a/src/main/java/florizz/core/Florizz.java +++ b/src/main/java/florizz/core/Florizz.java @@ -1,6 +1,7 @@ package florizz.core; import florizz.command.Command; +import florizz.logging.MyFormatter; import florizz.objects.Bouquet; import java.io.IOException; @@ -32,6 +33,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 +44,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 9db80a6766..077abe486e 100644 --- a/src/main/java/florizz/core/Parser.java +++ b/src/main/java/florizz/core/Parser.java @@ -13,11 +13,12 @@ import florizz.command.HelpCommand; import florizz.objects.Bouquet; +import java.util.logging.Level; import java.util.logging.Logger; import java.util.logging.Level; 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"; 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"; + } + +}