Skip to content

Commit

Permalink
Merge pull request #280 from tatayu/master
Browse files Browse the repository at this point in the history
Add Logging Support
  • Loading branch information
Weng-Kexin authored Nov 11, 2019
2 parents 1712cde + b24257f commit d50cbb6
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 7 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ application {
dependencies {
testCompile("org.junit.jupiter:junit-jupiter-api:5.4.2")
testImplementation 'org.junit.jupiter:junit-jupiter:5.5.0'

}

shadowJar {
Expand Down
25 changes: 25 additions & 0 deletions data/dollaLogger.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@


Nov 11, 2019 9:22:05 PM dolla.Dolla main
INFO: Running dolla..........
Nov 11, 2019 9:22:05 PM dolla.storage.StorageRead load
INFO: Save file successfully loaded
Nov 11, 2019 9:22:23 PM dolla.parser.Parser verifyAddCommand
SEVERE: Invalid entry format.
java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
at dolla.parser.Parser.verifyAddCommand(Parser.java:160)
at dolla.parser.EntryParser.parseInput(EntryParser.java:30)
at dolla.parser.MainParser.handleInput(MainParser.java:50)
at dolla.Dolla.run(Dolla.java:45)
at dolla.Dolla.main(Dolla.java:54)

Nov 11, 2019 9:24:14 PM dolla.Dolla main
INFO: ***********************DOLLA RUNNING***********************
Nov 11, 2019 9:24:14 PM dolla.storage.StorageRead load
INFO: Save file successfully loaded.
Nov 11, 2019 9:24:22 PM dolla.Dolla run
INFO: ***********************DOLLA TERMINATED***********************
Nov 11, 2019 9:29:05 PM dolla.Dolla main
INFO: ***********************DOLLA RUNNING***********************
Nov 11, 2019 9:29:05 PM dolla.storage.StorageRead load
INFO: Save file successfully loaded.
Empty file added data/dollaLogger.log.lck
Empty file.
6 changes: 5 additions & 1 deletion src/main/java/dolla/Dolla.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* Dolla is a chat-bot styled expense manager.
*/
public class Dolla implements ModeStringList, ParserStringList {
public class Dolla implements ModeStringList, ParserStringList {

private DollaData dollaData = new DollaData();
private boolean isExit = false;
Expand All @@ -28,6 +28,8 @@ private void verifyIsExit(String string) {
}

private void run() throws DollaException {
LogsCentreUtil.logSetter();
LogsCentreUtil.setLogger.info("***********************DOLLA RUNNING***********************");
Reminder reminder = new Reminder(MODE_DEBT);
reminder.showReminder(dollaData);
Scanner input = new Scanner(System.in); // TODO: Add to Ui or MainParser instead?
Expand All @@ -38,8 +40,10 @@ private void run() throws DollaException {
Command c = MainParser.handleInput(mode, inputLine);
c.execute(dollaData);
}
LogsCentreUtil.setLogger.info("************************DOLLA TERMINATED************************");
}


public static void main(String[] args) throws DollaException {
new Dolla().run();
}
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/dolla/LogsCentreUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package dolla;

import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;


public class LogsCentreUtil {

public static final Logger setLogger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);

/**
* SEVERE > WARNING > INFO > CONFIG > FINE > FINER > FINEST
* This method will setup the log to be stored in data.
*/
public static void logSetter() {
LogManager.getLogManager().reset();
setLogger.setLevel(Level.ALL);
try {
FileHandler fh = new FileHandler("./data/dollaLogger.log",true);
fh.setLevel(Level.INFO);
fh.setFormatter(new SimpleFormatter());
setLogger.addHandler(fh);
} catch (IOException e) {
setLogger.log(Level.SEVERE,"File logger not working", e);
}
}

}
38 changes: 33 additions & 5 deletions src/main/java/dolla/parser/Parser.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dolla.parser;

import dolla.LogsCentreUtil;
import dolla.ModeStringList;
import dolla.Time;
import dolla.model.RecordList;
Expand All @@ -22,6 +23,7 @@
import java.time.LocalDate;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.logging.Level;

import static dolla.parser.LimitParser.verifyLimitType;
import static dolla.parser.LimitParser.verifyLimitDuration;
Expand Down Expand Up @@ -85,6 +87,7 @@ protected void extractDescTime() throws Exception {
date = Time.readDate(dateString.trim());
} catch (DateTimeParseException e) {
Ui.printDateFormatError();
LogsCentreUtil.setLogger.log(Level.SEVERE, "Invalid date.", e);
throw new DollaException(INVALID_DATE_EXCEPTION);
}
}
Expand Down Expand Up @@ -116,6 +119,7 @@ public static double stringToDouble(String str) throws Exception {
}
} catch (NumberFormatException e) {
Ui.printInvalidNumberError(str);
LogsCentreUtil.setLogger.log(Level.SEVERE, "Invalid amount", e);
throw new NumberFormatException(INVALID_AMOUNT_EXCEPTION);
}
return newDouble;
Expand All @@ -141,6 +145,7 @@ private static String verifyAddType(String s) throws Exception {
return s;
} else {
EntryUi.printInvalidEntryType();
LogsCentreUtil.setLogger.log(Level.SEVERE, "Invalid entry type.");
throw new DollaException(DollaException.invalidType());
}
}
Expand All @@ -157,8 +162,10 @@ protected boolean verifyAddCommand() {
extractDescTime();
} catch (IndexOutOfBoundsException e) {
EntryUi.printInvalidEntryFormatError();
LogsCentreUtil.setLogger.log(Level.SEVERE, "Invalid entry format.", e);
return false;
} catch (Exception e) {
LogsCentreUtil.setLogger.log(Level.SEVERE, "Error occurs.", e);
return false; // If error occurs, stop the method!
}
return true;
Expand All @@ -177,6 +184,7 @@ public boolean verifyDebtCommand() {
DebtUi.printInvalidNameMessage();
return false;
} catch (Exception ignored) {
LogsCentreUtil.setLogger.log(Level.SEVERE, "Error occurs");
//do nothing
}
amount = stringToDouble(inputArray[2]);
Expand All @@ -186,6 +194,7 @@ public boolean verifyDebtCommand() {
return checkDate(dateString[1]);
} catch (Exception e) {
DebtUi.printInvalidDebtFormatError();
LogsCentreUtil.setLogger.log(Level.SEVERE, "Invalid debt format", e);
return false;
}
}
Expand All @@ -201,6 +210,7 @@ private boolean checkDate(String dateString) {
date = Time.readDate(dateString);
} catch (DateTimeParseException e) {
Ui.printDateFormatError();
LogsCentreUtil.setLogger.log(Level.SEVERE, "Date format error", e);
return false;
}
return true;
Expand Down Expand Up @@ -274,6 +284,7 @@ protected boolean verifyRemove() {
return false;
}
} catch (NumberFormatException e) {
LogsCentreUtil.setLogger.log(Level.SEVERE, "Invalid remove message.", e);
RemoveUi.printInvalidRemoveMessage();
return false;
}
Expand All @@ -292,6 +303,7 @@ protected boolean verifyRemoveForDebtMode() {
return false;
}
} catch (NumberFormatException e) {
LogsCentreUtil.setLogger.log(Level.SEVERE, "Invalid number.", e);
return false;
}
return true;
Expand Down Expand Up @@ -335,6 +347,7 @@ protected boolean verifyRemoveBill(RecordList recordList) {
return true;
}
} catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
LogsCentreUtil.setLogger.log(Level.SEVERE, "Invalid number.", e);
return false;
}
} else {
Expand All @@ -360,6 +373,7 @@ protected boolean verifyShortcut() {
}
} catch (NumberFormatException e) {
ShortcutUi.printInvalidShortcutMessage();
LogsCentreUtil.setLogger.log(Level.SEVERE, "Invalid shortcut", e);
return false;
}
return true;
Expand All @@ -378,6 +392,7 @@ protected boolean verifyFullModifyCommand() {
Integer.parseInt(inputArray[1]);
} catch (Exception e) {
ModifyUi.printInvalidFullModifyFormatError();
LogsCentreUtil.setLogger.log(Level.SEVERE, "Invalid full modify format.", e);
return false;
}
return true;
Expand All @@ -401,7 +416,8 @@ protected boolean verifyPartialModifyCommand() {
try {
modifyRecordNum = Integer.parseInt(inputArray[1]);
} catch (Exception e) {
ModifyUi.printInvalidFullModifyFormatError();
ModifyUi.printInvalidPartialModifyFormatError();
LogsCentreUtil.setLogger.log(Level.SEVERE, "Invalid partial modify format", e);
return false;
}

Expand Down Expand Up @@ -448,11 +464,14 @@ private boolean findComponents() {

} catch (ArrayIndexOutOfBoundsException e) {
ModifyUi.printMissingComponentInfoError(currStr);
LogsCentreUtil.setLogger.log(Level.SEVERE, "Missing component information error", e);
return false;
} catch (DateTimeParseException e) {
LogsCentreUtil.setLogger.log(Level.SEVERE, "Date format error", e);
Ui.printDateFormatError();
return false;
} catch (Exception e) {
LogsCentreUtil.setLogger.log(Level.SEVERE, "Error occurs", e);
return false;
}

Expand Down Expand Up @@ -517,10 +536,10 @@ private void verifyLimitComponents(String currStr, String nextStr) throws Except
}

/**
* Checks if the first word after 'add' is either 'income' or 'expense'.
* @param s String to be analysed.
* @return Either 'expense' or 'income' if either are passed in.
* @throws Exception ???
* * Checks if the first word after 'add' is either 'income' or 'expense'.
* * @param s String to be analysed.
* * @return Either 'expense' or 'income' if either are passed in.
* * @throws Exception ???
*/
private static String verifyDebtType(String s) throws Exception {
if (s.equals(TYPE_OWE) || s.equals(TYPE_BORROW)) {
Expand Down Expand Up @@ -644,6 +663,7 @@ protected Boolean verifyPaidNumberAndName() {
return true;
}
} catch (Exception e) {
LogsCentreUtil.setLogger.log(Level.SEVERE, "Error occurs.", e);
return false;
}
}
Expand Down Expand Up @@ -681,6 +701,7 @@ protected Boolean verifyDebtSearchCommand() {
}
} catch (IndexOutOfBoundsException | NullPointerException e) {
SearchUi.printInvalidSearchFormat();
LogsCentreUtil.setLogger.log(Level.SEVERE, "Invalid search format.", e);
return false;
}
}
Expand All @@ -698,6 +719,7 @@ protected Boolean verifyEntrySearchCommand() {
return false;
}
} catch (IndexOutOfBoundsException | NullPointerException e) {
LogsCentreUtil.setLogger.log(Level.SEVERE, "Invalid search format.", e);
SearchUi.printInvalidSearchFormat();
return false;
}
Expand All @@ -717,6 +739,7 @@ protected Boolean verifyLimitSearchCommand() {
}
} catch (IndexOutOfBoundsException | NullPointerException e) {
SearchUi.printInvalidSearchFormat();
LogsCentreUtil.setLogger.log(Level.SEVERE, "Invalid search format.", e);
return false;
}
}
Expand All @@ -735,6 +758,7 @@ protected Boolean verifyShortcutSearchCommand() {
}
} catch (IndexOutOfBoundsException | NullPointerException e) {
SearchUi.printInvalidSearchFormat();
LogsCentreUtil.setLogger.log(Level.SEVERE, "Invalid search format.", e);
return false;
}
}
Expand Down Expand Up @@ -776,6 +800,7 @@ protected Boolean verifyBillPeopleAndAmount() {
stringToDouble(inputArray[2]);
} catch (Exception e) {
DebtUi.printInvalidBillFormatError();
LogsCentreUtil.setLogger.log(Level.SEVERE, "Invalid bill format.", e);
return false;
}
return true;
Expand Down Expand Up @@ -805,8 +830,10 @@ protected Boolean verifyAddBillCommand(ArrayList<String> nameList) {
}
} catch (IndexOutOfBoundsException e) {
DebtUi.printWrongPeopleNumberMessage(Integer.parseInt(inputArray[1]));
LogsCentreUtil.setLogger.log(Level.SEVERE, "Wrong people number.", e);
return false;
} catch (Exception e) {
LogsCentreUtil.setLogger.log(Level.SEVERE, "Error occurs.", e);
return false;
}
return true;
Expand All @@ -819,6 +846,7 @@ protected Boolean verifySetCommand() {
amount = stringToDouble(inputArray[2]);
duration = verifyLimitDuration(inputArray[3]);
} catch (Exception e) {
LogsCentreUtil.setLogger.log(Level.SEVERE, "Error occurs", e);
return false;
}
return true;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/dolla/storage/Storage.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package dolla.storage;

import dolla.LogsCentreUtil;
import dolla.model.Record;
import dolla.ui.Ui;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;

//@@author yetong1895
/**
Expand All @@ -27,6 +29,7 @@ protected static double stringToDouble(String str) {
newDouble = Double.parseDouble(str);
} catch (NumberFormatException e) {
Ui.printInvalidNumberError(str);
LogsCentreUtil.setLogger.log(Level.SEVERE, "Invalid number.", e);
}
return newDouble;
}
Expand Down Expand Up @@ -82,6 +85,7 @@ public static void clearStorage() {
file.close();
} catch (IOException e) {
e.printStackTrace();
LogsCentreUtil.setLogger.log(Level.SEVERE, "Clear storage fail.", e);
}
}
}
6 changes: 5 additions & 1 deletion src/main/java/dolla/storage/StorageRead.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dolla.storage;

import dolla.LogsCentreUtil;
import dolla.Time;
import dolla.parser.MainParser;
import dolla.model.Debt;
Expand All @@ -20,6 +21,7 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;

//@@author yetong1895
public class StorageRead extends Storage {
Expand Down Expand Up @@ -94,11 +96,13 @@ public static void load() {
addToList(type, newRecord);
}
StorageWrite.save();

LogsCentreUtil.setLogger.log(Level.INFO,"Save file successfully loaded.");
} catch (FileNotFoundException e) {
StorageUi.printCreateFolderMessage();
LogsCentreUtil.setLogger.log(Level.SEVERE, "Create a new save file.", e);
} catch (IOException e) {
StorageUi.printErrorReadingSaveMessage();
LogsCentreUtil.setLogger.log(Level.SEVERE, "Error reading dolla.txt.", e);
MainParser.exit(); // TODO: Find out what is supposed to happen here
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/dolla/storage/StorageWrite.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package dolla.storage;

import dolla.LogsCentreUtil;
import dolla.model.Record;
import dolla.ui.StorageUi;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;

//@@author yetong1895
public class StorageWrite extends Storage {
Expand Down Expand Up @@ -75,6 +77,7 @@ protected static void save() {

} catch (IOException e) {
StorageUi.printErrorWritingSaveMessage();
LogsCentreUtil.setLogger.log(Level.SEVERE, "Error writing to Dolla.", e);
}
}
}

0 comments on commit d50cbb6

Please sign in to comment.