Skip to content

Commit

Permalink
Enhance logging with lower-level statements
Browse files Browse the repository at this point in the history
This commit introduces lower-level logging statements, specifically at
CONFIG, FINE, and FINER levels, to demonstrate the utilization of
various logging levels effectively. Some of the existing logs have also
been adjusted to utilize lower-levels, providing more detailed insights
for debugging and troubleshooting purposes.

The intention of this enhancement is to bolster the application's
logging capabilities, enabling better comprehension of its internal
behavior and facilitating smoother debugging processes.
  • Loading branch information
Eclipse-Dominator committed Jul 28, 2023
1 parent 99c991d commit 8c4a065
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 6 deletions.
8 changes: 5 additions & 3 deletions src/main/java/seedu/address/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public void init() throws Exception {
* or an empty address book will be used instead if errors occur when reading {@code storage}'s address book.
*/
private Model initModelManager(Storage storage, ReadOnlyUserPrefs userPrefs) {
logger.config("Using data file : " + storage.getAddressBookFilePath());

Optional<ReadOnlyAddressBook> addressBookOptional;
ReadOnlyAddressBook initialData;
try {
Expand Down Expand Up @@ -108,11 +110,11 @@ protected Config initConfig(Path configFilePath) {
configFilePathUsed = Config.DEFAULT_CONFIG_FILE;

if (configFilePath != null) {
logger.info("Custom Config file specified " + configFilePath);
logger.fine("Custom Config file specified " + configFilePath);
configFilePathUsed = configFilePath;
}

logger.info("Using config file : " + configFilePathUsed);
logger.config("Using config file : " + configFilePathUsed);

try {
Optional<Config> configOptional = ConfigUtil.readConfig(configFilePathUsed);
Expand Down Expand Up @@ -142,7 +144,7 @@ protected Config initConfig(Path configFilePath) {
*/
protected UserPrefs initPrefs(UserPrefsStorage storage) {
Path prefsFilePath = storage.getUserPrefsFilePath();
logger.info("Using preference file : " + prefsFilePath);
logger.config("Using preference file : " + prefsFilePath);

UserPrefs initializedPrefs;
try {
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/seedu/address/commons/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.logging.Logger;

import seedu.address.commons.core.LogsCenter;

/**
* Writes and reads files
*/
public class FileUtil {

private static final String CHARSET = "UTF-8";
private static final Logger logger = LogsCenter.getLogger(FileUtil.class);

public static boolean isFileExists(Path file) {
return Files.exists(file) && Files.isRegularFile(file);
Expand Down Expand Up @@ -48,6 +52,7 @@ public static void createFile(Path file) throws IOException {
if (Files.exists(file)) {
return;
}
logger.fine("Creating file: " + file);

createParentDirsOfFile(file);

Expand All @@ -61,6 +66,7 @@ public static void createParentDirsOfFile(Path file) throws IOException {
Path parentDir = file.getParent();

if (parentDir != null) {
logger.fine("Creating parent directories of file: " + parentDir);
Files.createDirectories(parentDir);
}
}
Expand All @@ -77,6 +83,7 @@ public static String readFromFile(Path file) throws IOException {
* Will create the file if it does not exist yet.
*/
public static void writeToFile(Path file, String content) throws IOException {
logger.finer(String.format("Writing to file %s with %s", file, content));
Files.write(file, content.getBytes(CHARSET));
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/commons/util/JsonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static <T> Optional<T> readJsonFile(
if (!Files.exists(filePath)) {
return Optional.empty();
}
logger.info("JSON file " + filePath + " found.");
logger.fine("JSON file " + filePath + " found.");

T jsonFile;

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/seedu/address/logic/LogicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public CommandResult execute(String commandText) throws CommandException, ParseE

CommandResult commandResult;
Command command = addressBookParser.parseCommand(commandText);

logger.fine("Created Command to be executed: " + command.toString());
commandResult = command.execute(model);

try {
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/seedu/address/logic/parser/AddressBookParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.Messages.MESSAGE_UNKNOWN_COMMAND;

import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import seedu.address.commons.core.LogsCenter;
import seedu.address.logic.commands.AddCommand;
import seedu.address.logic.commands.ClearCommand;
import seedu.address.logic.commands.Command;
Expand All @@ -26,6 +28,7 @@ public class AddressBookParser {
* Used for initial separation of command word and args.
*/
private static final Pattern BASIC_COMMAND_FORMAT = Pattern.compile("(?<commandWord>\\S+)(?<arguments>.*)");
private static final Logger logger = LogsCenter.getLogger(AddressBookParser.class);

/**
* Parses user input into command for execution.
Expand All @@ -42,33 +45,45 @@ public Command parseCommand(String userInput) throws ParseException {

final String commandWord = matcher.group("commandWord");
final String arguments = matcher.group("arguments");
logger.fine("Command word: " + commandWord);
logger.fine("Arguments: " + arguments);

switch (commandWord) {

case AddCommand.COMMAND_WORD:
logger.fine("Add command detected");
return new AddCommandParser().parse(arguments);

case EditCommand.COMMAND_WORD:
logger.fine("Edit command detected");
return new EditCommandParser().parse(arguments);

case DeleteCommand.COMMAND_WORD:
logger.fine("Delete command detected");
return new DeleteCommandParser().parse(arguments);

case ClearCommand.COMMAND_WORD:
logger.fine("Clear command detected");
return new ClearCommand();

case FindCommand.COMMAND_WORD:
logger.fine("Find command detected");
return new FindCommandParser().parse(arguments);

case ListCommand.COMMAND_WORD:
logger.fine("List command detected");
return new ListCommand();

case ExitCommand.COMMAND_WORD:
logger.fine("Exit command detected");
return new ExitCommand();

case HelpCommand.COMMAND_WORD:
logger.fine("Help command detected");
return new HelpCommand();

default:
logger.fine("Unknown command detected");
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package seedu.address.logic.parser.exceptions;

import java.util.logging.Logger;

import seedu.address.commons.core.LogsCenter;
import seedu.address.commons.exceptions.IllegalValueException;

/**
* Represents a parse error encountered by a parser.
*/
public class ParseException extends IllegalValueException {
private static Logger logger = LogsCenter.getLogger(ParseException.class);

public ParseException(String message) {
super(message);
logger.fine("ParseException thrown: " + message);
}

public ParseException(String message, Throwable cause) {
super(message, cause);
logger.fine("ParseException thrown: " + message);
}
}
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class ModelManager implements Model {
public ModelManager(ReadOnlyAddressBook addressBook, ReadOnlyUserPrefs userPrefs) {
requireAllNonNull(addressBook, userPrefs);

logger.fine("Initializing with address book: " + addressBook + " and user prefs " + userPrefs);
logger.finer("Initializing with address book: " + addressBook + " and user prefs " + userPrefs);

this.addressBook = new AddressBook(addressBook);
this.userPrefs = new UserPrefs(userPrefs);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/seedu/address/storage/StorageManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ public Path getUserPrefsFilePath() {

@Override
public Optional<UserPrefs> readUserPrefs() throws DataLoadingException {
logger.fine("Attempting to read from preference file: " + getUserPrefsFilePath());
return userPrefsStorage.readUserPrefs();
}

@Override
public void saveUserPrefs(ReadOnlyUserPrefs userPrefs) throws IOException {
logger.fine("Attempting to write to preference file: " + getUserPrefsFilePath());
userPrefsStorage.saveUserPrefs(userPrefs);
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/seedu/address/ui/HelpWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public boolean isShowing() {
* Hides the help window.
*/
public void hide() {
logger.fine("Hiding help page");
getRoot().hide();
}

Expand All @@ -94,6 +95,7 @@ public void focus() {
*/
@FXML
private void copyUrl() {
logger.fine("Copying user guide URL to clipboard");
final Clipboard clipboard = Clipboard.getSystemClipboard();
final ClipboardContent url = new ClipboardContent();
url.putString(USERGUIDE_URL);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/seedu/address/ui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ void show() {
*/
@FXML
private void handleExit() {
logger.fine("Exiting application");
GuiSettings guiSettings = new GuiSettings(primaryStage.getWidth(), primaryStage.getHeight(),
(int) primaryStage.getX(), (int) primaryStage.getY());
logic.setGuiSettings(guiSettings);
Expand All @@ -175,6 +176,7 @@ public PersonListPanel getPersonListPanel() {
private CommandResult executeCommand(String commandText) throws CommandException, ParseException {
try {
CommandResult commandResult = logic.execute(commandText);
logger.fine("Received CommandResult: " + commandResult);
logger.info("Result: " + commandResult.getFeedbackToUser());
resultDisplay.setFeedbackToUser(commandResult.getFeedbackToUser());

Expand Down
1 change: 1 addition & 0 deletions src/main/java/seedu/address/ui/PersonListPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class PersonListPanel extends UiPart<Region> {
*/
public PersonListPanel(ObservableList<Person> personList) {
super(FXML);
logger.finer("Created person list panel with " + personList.toString());
personListView.setItems(personList);
personListView.setCellFactory(listView -> new PersonListViewCell());
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/ui/UiManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public UiManager(Logic logic) {

@Override
public void start(Stage primaryStage) {
logger.info("Starting UI...");
logger.fine("Starting UI...");

//Set the application icon.
primaryStage.getIcons().add(getImage(ICON_APPLICATION));
Expand Down

0 comments on commit 8c4a065

Please sign in to comment.