Skip to content

Commit

Permalink
Merge pull request nus-cs2113-AY2324S2#54 from IanFH/logging-file-han…
Browse files Browse the repository at this point in the history
…dler

Successfully set up file handler. Florizz.xml file will appear if for…
  • Loading branch information
JeffinsonDarmawan authored Mar 22, 2024
2 parents 8483e62 + 1bb95fa commit ef7de18
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 4 deletions.
1 change: 1 addition & 0 deletions FlorizzLogger.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1::florizz.core.Florizz::main::Fri Mar 22 18:09:06 SGT 2024::Entered isRunning while loop in Florizz.java
Empty file added FlorizzLogger.xml.lck
Empty file.
7 changes: 4 additions & 3 deletions src/main/java/florizz/core/Florizz.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package florizz.core;

import florizz.command.Command;
import florizz.logging.MyFormatter;
import florizz.objects.Bouquet;

import java.io.IOException;
Expand Down Expand Up @@ -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);

Expand 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();
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/florizz/core/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/florizz/logging/MyFilter.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
24 changes: 24 additions & 0 deletions src/main/java/florizz/logging/MyFormatter.java
Original file line number Diff line number Diff line change
@@ -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";
}

}

0 comments on commit ef7de18

Please sign in to comment.