Skip to content

Commit

Permalink
Added log4j
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc committed Feb 2, 2021
1 parent 408a812 commit a3ad4d4
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 40 deletions.
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@
<artifactId>javafx-fxml</artifactId>
<version>11</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.14.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.14.0</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
Expand Down
1 change: 1 addition & 0 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
requires commons.csv;
requires javafx.controls;
requires javafx.fxml;
requires org.apache.logging.log4j;

opens org.log.presentation to javafx.fxml;
exports org.log.presentation;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.log.application.service;

import org.log.domain.ports.logfile.LogFileExporter;
import org.log.domain.ports.logfile.LogFileFilter;
import org.log.domain.ports.logfile.LogFileOpener;
import org.log.domain.ports.logfile.LogFileExporter;

import java.io.File;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.log.application.usecases;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.log.domain.ports.logfile.LogFileExporter;

import java.io.File;
Expand All @@ -8,6 +10,7 @@
import java.util.List;

public class LogFileExporterImpl implements LogFileExporter {
private static final Logger logger = LogManager.getLogger(LogFileExporterImpl.class);

@Override
public boolean exportLogToFile(File file, List<String> logFileList) {
Expand All @@ -21,7 +24,7 @@ public boolean exportLogToFile(File file, List<String> logFileList) {
writer.close();
return true;
} catch (IOException ex) {
System.out.println("Could not save log to file: " + ex);
logger.error("LogFileExporterImpl.exportLogToFile:: Could not save log to file: " + ex);
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.log.application.usecases;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.log.domain.ports.logfile.LogFileFilter;

import java.util.List;
Expand All @@ -9,16 +11,17 @@
import java.util.stream.Collectors;

public class LogFileFilterImpl implements LogFileFilter {
private Logger logger = LogManager.getLogger(LogFileFilterImpl.class);

@Override
public List<String> filterListBy(List<String> originalList, List<String> wordsToInclude, List<String> wordsToExclude) {
LogFilterPredicate includeFilterPredicate = new LogFilterPredicate(wordsToInclude, true);
LogFilterPredicate excludeFilterPredicate = new LogFilterPredicate(wordsToExclude, false);

System.out.println("Words to include:");
logger.debug("Words to include:");
wordsToInclude.forEach(System.out::println);

System.out.println("Words to exclude:");
logger.debug("Words to exclude:");
wordsToExclude.forEach(System.out::println);

if (wordsToInclude.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.log.application.usecases;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.log.domain.ports.logfile.LogFileOpener;

import java.io.File;
Expand All @@ -9,6 +11,7 @@
import java.util.Scanner;

public class LogFileOpenerImpl implements LogFileOpener {
private static final Logger logger = LogManager.getLogger(LogFileOpenerImpl.class);

@Override
public List<String> openFile(String fileName) {
Expand All @@ -23,7 +26,7 @@ public List<String> openFile(String fileName) {
}
logReader.close();
} catch (FileNotFoundException e) {
System.out.println("File not found: " + e);
logger.error("LogFileOpenerImpl.openFile:: File not found: " + e);
}
return logList;
}
Expand Down
21 changes: 12 additions & 9 deletions src/main/java/org/log/infrastructure/FilePersistor.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.csv.CSVRecord;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.log.domain.entities.Filter;
import org.log.domain.ports.filter.FilterRepository;

Expand All @@ -21,6 +23,7 @@

public class FilePersistor implements FilterRepository {
private static final String FILTERS_FILENAME = "filters.csv";
private Logger logger = LogManager.getLogger(FilePersistor.class);

@Override
public List<Filter> findAll() {
Expand All @@ -36,15 +39,15 @@ public List<Filter> findAll() {
//}
}
} catch (IOException e) {
System.out.println("Error fetching filters: " + e);
logger.error("FilePersistor.findAll:: Error fetching filters: " + e);
}

return filterList;
}

@Override
public boolean create(String filterName, String filterData) {
System.out.println("Create filter with name: " + filterName);
logger.debug("Create filter with name: " + filterName);

boolean doesFilterAlreadyExists = findAll().stream().anyMatch(filter -> filter.getFilterName().equalsIgnoreCase(filterName));

Expand All @@ -58,7 +61,7 @@ public boolean create(String filterName, String filterData) {
csvPrinter.flush();

} catch (IOException e) {
System.out.println("Error creating filter: " + e);
logger.error("FilePersistor.create:: Error creating filter: " + e);
return false;
}

Expand All @@ -74,17 +77,17 @@ public boolean update(String filterName, String filterData) {
for (CSVRecord csvRecord : csvParser) {
Filter filter;
if (csvRecord.get(0).equalsIgnoreCase(filterName)) {
System.out.println("Found filter, edit with: " + filterData);
logger.debug("Found filter, edit with: " + filterData);
filter = new Filter(csvRecord.get(0), filterData);
} else {
System.out.println("Filter not found yet...: " + csvRecord.get(0));
logger.debug("Filter not found yet...: " + csvRecord.get(0));
filter = new Filter(csvRecord.get(0), csvRecord.get(1));
}
filterList.add(filter);
}
return overrideFilterFile(filterList);
} catch (IOException e) {
System.out.println("Error updating filters: " + e);
logger.error("FilePersistor.update:: Error updating filter: " + e);
}
return false;
}
Expand All @@ -95,17 +98,17 @@ private boolean overrideFilterFile(List<Filter> filterList) {

filterList.forEach(filter -> {
try {
System.out.println("Adding to file: " + filter.getFilterName() + ": " + filter.getFilterData());
logger.debug("Adding to file: " + filter.getFilterName() + ": " + filter.getFilterData());
csvPrinter.printRecord(filter.getFilterName(), filter.getFilterData());
csvPrinter.flush();
} catch (IOException e) {
System.out.println("Error saving to filter file: " + e);
logger.error("FilePersistor.overrideFilterFile:: Error saving to filter file: " + e);
}
});

return true;
} catch (IOException e) {
System.out.println("Error overriding filter file: " + e);
logger.error("FilePersistor.overrideFilterFile:: Error overriding filter: " + e);
return false;
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/org/log/presentation/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;

public class App extends Application {

private Stage window;
private static final Logger logger = LogManager.getLogger(App.class);

@Override
public void start(Stage stage) throws IOException {
Expand All @@ -37,10 +40,10 @@ public void start(Stage stage) throws IOException {
private void closeProgram() {
boolean exit = ConfirmBox.display("Exit", "Are you sure you want to leave?");
if (exit) {
System.out.println("File is saved!");
logger.debug("File is saved!");
window.close();
} else {
System.out.println("Good decision");
logger.debug("Good decision");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.log.application.usecases.FilterEraser;
import org.log.application.usecases.FilterReader;
import org.log.application.usecases.FilterUpdater;
Expand All @@ -36,6 +38,8 @@ public class EditFilterController implements Initializable{
@FXML
public Button saveFilterButton;

private static final Logger logger = LogManager.getLogger(EditFilterController.class);

@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
populateFilters();
Expand All @@ -50,13 +54,13 @@ private void populateFilters() {
filterListView.getItems().clear();

filterList.forEach(filter -> {
System.out.println("Filter found -> " + filter.getFilterName());
logger.debug("Filter found -> " + filter.getFilterName());
filterListView.getItems().add(filter.getFilterName());
});

filterListView.setOnMouseClicked(mouseEvent -> {
String filterName = filterListView.getSelectionModel().getSelectedItem();
System.out.println("clicked on " + filterName);
logger.debug("clicked on " + filterName);

filterList.forEach(filter -> {
if (filter.getFilterName().equalsIgnoreCase(filterName)) {
Expand Down Expand Up @@ -85,10 +89,10 @@ public void handleOnSaveFilterClick(ActionEvent actionEvent) {

boolean isUpdated = filterUpdater.update(filterName, filterData);
if (isUpdated) {
System.out.println("Updated!");
logger.debug("Updated!");
populateFilters();
saveFilterButton.setDisable(true);
} else System.out.println("Not updated!");
} else logger.debug("Not updated!");
filterListView.getSelectionModel().select(filterListIndex);
}

Expand All @@ -111,7 +115,7 @@ public void handleOnCreateFilterClick(ActionEvent actionEvent) {
stage.setOnHidden(e -> populateFilters());
stage.show();
} catch (IOException e) {
System.out.println("Could not load new filter stage: " + e.getMessage());
logger.error("EditFilterController.handleOnCreateFilterClick:: Could not load new filter stage: " + e.getMessage());
e.printStackTrace();
}
}
Expand All @@ -122,9 +126,9 @@ public void handleOnDeleteFilterClick(ActionEvent actionEvent) {

boolean isDeleted = filterEraser.delete(filterName);
if (isDeleted) {
System.out.println("Deleted!");
logger.debug("Deleted!");
populateFilters();
filterContentText.setText("");
} else System.out.println("Not deleted!");
} else logger.debug("Not deleted!");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.log.application.service.LogFileInteractor;
import org.log.application.usecases.LogFileExporterImpl;
import org.log.application.usecases.LogFileFilterImpl;
Expand All @@ -38,6 +40,7 @@ public class MainController implements Initializable {
@FXML
public VBox mainVerticalBox;

private static final Logger logger = LogManager.getLogger(MainController.class);
private LogFileInteractor logFileInteractor;
private Tab currentTab;

Expand All @@ -51,18 +54,18 @@ public void initialize(URL url, ResourceBundle resourceBundle) {
}

public void handleOpenFileMenuClick() {
System.out.println("Click on open file!");
logger.debug("Click on open file!");
FileChooser fileChooser = new FileChooser();
List<File> logFileList = fileChooser.showOpenMultipleDialog(null);

logFileList.forEach(file -> {
if (file != null) {
System.out.println("Opening log file: " + file.getAbsolutePath());
logger.debug("Opening log file: " + file.getAbsolutePath());
Stage stage = (Stage) menuBar.getScene().getWindow();
stage.setTitle(file.getName());
openLogFile(file);
} else {
System.out.println("Could not open the file!");
logger.error("MainController.handleOpenFileMenuClick:: Could not open the file because it's null");
}
});
}
Expand All @@ -77,7 +80,7 @@ private void openLogFile(File logFile) {
tabController.setData(logFileInteractor.loadLogFile(logFile.getPath()));
logTabPane.getSelectionModel().select(newTab);
} catch (IOException e) {
System.out.println("Could not load file to filter: " + logFile);
logger.error("MainController.openLogFile:: Could not load file to filter: " + logFile);
e.printStackTrace();
}
}
Expand Down Expand Up @@ -114,7 +117,7 @@ public void handleOnEditFiltersClick(ActionEvent actionEvent) {
//stage.setOnHidden(e -> loadFilterMenu());
stage.show();
} catch (IOException e) {
System.out.println("Could not load edit stage: " + e);
logger.error("MainController.handleOnEditFiltersClick:: Could not load edit stage: " + e);
}
}

Expand Down
Loading

0 comments on commit a3ad4d4

Please sign in to comment.