diff --git a/.gitignore b/.gitignore index be56b64..f0fdad5 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ build .gradle .idea .pdf -.db \ No newline at end of file +.db +logs/ diff --git a/README.md b/README.md index 26e735f..c91454c 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ SQLite storage and PDF ledger reports. `src/main/java/openconsignment/di`) - SQLite (via `sqlite-jdbc`) for storage - JasperReports for PDF report generation +- SLF4J with Log4j 2 for asynchronous rolling-file logging - Gradle (Kotlin DSL) with the Shadow plugin for a single runnable JAR - JUnit 5, Mockito, and TestFX for testing; Spotless (`palantir-java-format`) for formatting; JaCoCo for coverage @@ -107,6 +108,34 @@ Keep the JAR and launch script together when moving the application. - The report organization name is configured in `src/main/java/openconsignment/common/Constants.java`. +## Logging + +The application writes INFO and higher messages to stderr and to +`logs/openconsignment.log` in its working directory. Application logging uses +SLF4J directly with Log4j 2 as its backend, including exception stack traces. +Use SLF4J's `LoggerFactory` and parameterized messages in application code. + +The file is rolled at startup, daily, or when it reaches 10 MB. Archives are +compressed; during rollover, archives older than 30 days or beyond 100 MB total +are deleted. The active file is separate from that archive limit. File writes use +a bounded asynchronous queue; if it fills, callers wait rather than dropping +messages. The queue is drained on normal JVM shutdown. An abrupt termination +may lose queued messages. Log4j reports file setup problems to stderr, where +console logging remains available. + +The working directory is the repository root when using `./gradlew run` and +`build/libs` when using the supplied launch scripts. To use a different +directory or file size, pass JVM properties, for example: + +```shell +java -Dopenconsignment.logDir=/path/to/logs -Dopenconsignment.logSize="20 MB" ... +``` + +Configure these options in the launch script when running the packaged JAR. +Use a separate log directory for each simultaneous application instance; +the rolling files are not coordinated across processes. Log files can contain +exception details, so restrict access and review them before sharing. + ## Testing ```shell diff --git a/build.gradle.kts b/build.gradle.kts index e5b3c9e..7a513d0 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -17,7 +17,10 @@ dependencies { implementation(libs.controlsfx) implementation(libs.jasperreports) implementation(libs.sqlite) - implementation(libs.olap4j) + implementation(libs.olap4j) + implementation(libs.slf4j.api) + runtimeOnly(libs.log4j.slf4j2.impl) + runtimeOnly(libs.log4j.core) // implementation(libs.itext) implementation(libs.dagger) @@ -50,9 +53,11 @@ javafx { modules = listOf("javafx.controls", "javafx.fxml", "javafx.graphics", "javafx.base") } -tasks.test { - useJUnitPlatform() - finalizedBy(tasks.jacocoTestReport) +tasks.test { + useJUnitPlatform() + finalizedBy(tasks.jacocoTestReport) + systemProperty("logging.test.classpath", sourceSets.test.get().runtimeClasspath.asPath) + providers.gradleProperty("loggingTestJar").orNull?.let { systemProperty("logging.test.jar", file(it).absolutePath) } // Keep UI tests independent of desktop focus and native mouse permissions. // Use -PheadlessTests=false when debugging tests with visible windows. @@ -106,9 +111,10 @@ tasks.named("run") { ) } -tasks.named("shadowJar") { - archiveClassifier.set("") -} +tasks.named("shadowJar") { + archiveClassifier.set("") + mergeServiceFiles() +} tasks.named("distZip") { enabled = false } tasks.named("distTar") { enabled = false } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 606cceb..1c567f9 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -13,7 +13,9 @@ monocle = "17.0.10" hamcrest = "2.2" #junit = "5.10.1" junitbom = "5.10.2" -mockito = "5.14.2" +mockito = "5.14.2" +slf4j = "2.0.17" +log4j = "2.24.3" spotless = "7.0.2" shadow = "7.1.2" @@ -23,7 +25,10 @@ javafxPlugin = "0.1.0" controlsfx = { module = "org.controlsfx:controlsfx", version.ref = "controlsfx" } jasperreports = { module = "net.sf.jasperreports:jasperreports", version.ref = "jasperreports" } sqlite = { module = "org.xerial:sqlite-jdbc", version.ref = "sqlite" } -olap4j = { module = "org.olap4j:olap4j", version.ref = "olap4j" } +olap4j = { module = "org.olap4j:olap4j", version.ref = "olap4j" } +slf4j-api = { module = "org.slf4j:slf4j-api", version.ref = "slf4j" } +log4j-slf4j2-impl = { module = "org.apache.logging.log4j:log4j-slf4j2-impl", version.ref = "log4j" } +log4j-core = { module = "org.apache.logging.log4j:log4j-core", version.ref = "log4j" } #itext = { module = "com.lowagie:itext", version.ref = "itext" } javafx-controls = { module = "org.openjfx:javafx-controls", version.ref = "javafx" } @@ -55,4 +60,4 @@ mockito-junit-jupiter = { module = "org.mockito:mockito-junit-jupiter", version. javafx = { id = "org.openjfx.javafxplugin", version.ref = "javafxPlugin" } spotless = { id = "com.diffplug.spotless", version.ref = "spotless" } #shadow = { id = "com.gradleup.shadow", version.ref = "shadow" } -shadow = { id = "com.github.johnrengelman.shadow", version.ref = "shadow" } \ No newline at end of file +shadow = { id = "com.github.johnrengelman.shadow", version.ref = "shadow" } diff --git a/src/main/java/openconsignment/Launcher.java b/src/main/java/openconsignment/Launcher.java index 5521b85..b2fdf73 100644 --- a/src/main/java/openconsignment/Launcher.java +++ b/src/main/java/openconsignment/Launcher.java @@ -2,8 +2,6 @@ import java.io.InputStream; import java.net.URL; -import java.util.logging.Level; -import java.util.logging.Logger; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; @@ -13,8 +11,11 @@ import openconsignment.common.Utils; import openconsignment.di.AppComponent; import openconsignment.di.DaggerAppComponent; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class Launcher extends Application { + private static final Logger logger = LoggerFactory.getLogger(Launcher.class); public static void main(String[] args) { launch(args); @@ -41,9 +42,10 @@ public void start(Stage stage) { stage.getIcons().add(new Image(resourceAsStream)); } stage.setTitle("OpenConsignment"); + logger.info("OpenConsignment started at {}", java.time.LocalDateTime.now()); } catch (Exception ex) { Utils.showAlert(ex.toString()); - Logger.getLogger(Launcher.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); } } } diff --git a/src/main/java/openconsignment/common/Utils.java b/src/main/java/openconsignment/common/Utils.java index eb89baf..a1f3a42 100644 --- a/src/main/java/openconsignment/common/Utils.java +++ b/src/main/java/openconsignment/common/Utils.java @@ -11,41 +11,23 @@ import java.util.List; import java.util.Map; import java.util.concurrent.CompletableFuture; -import java.util.logging.Level; -import java.util.logging.Logger; import javafx.scene.control.Alert; import javafx.scene.control.Alert.AlertType; import net.sf.jasperreports.engine.*; import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class Utils { - private static final Logger logger = Logger.getLogger(Utils.class.getName()); + private static final Logger logger = LoggerFactory.getLogger(Utils.class); public static void showAlert(String message) { - Alert alert = new Alert(AlertType.ERROR); - alert.setContentText(message); - alert.show(); - } - - private static AlertType getAlertTypeFromCustomValue(int choice) { - switch (choice) { - case 0: - return AlertType.ERROR; - case 1: - return AlertType.INFORMATION; - case 2: - return AlertType.WARNING; - case 3: - return AlertType.CONFIRMATION; - default: - showAlert("Programming Error. Select 1->error 2->info 3->warn 4->confirm"); - } - return AlertType.NONE; + showAlert(message, AlertType.ERROR); } - public static void showAlert(String message, int choice) { - Alert alert = new Alert(getAlertTypeFromCustomValue(choice), message); + public static void showAlert(String message, AlertType alertType) { + Alert alert = new Alert(alertType, message); alert.show(); } @@ -64,7 +46,7 @@ public static void launchPdf(String filename) throws IOException { try { Desktop.getDesktop().open(pdfFile); } catch (IOException e) { - logger.log(Level.WARNING, "Error opening file: " + pdfFile.getAbsolutePath(), e); + logger.warn("Error opening file: {}", pdfFile.getAbsolutePath(), e); } }); } @@ -78,7 +60,7 @@ public static void launchBrowser(String url) throws IOException { try { Desktop.getDesktop().browse(new URI(url)); } catch (IOException | URISyntaxException e) { - logger.log(Level.WARNING, "Error opening browser link " + url, e); + logger.warn("Error opening browser link {}", url, e); } }); } @@ -95,7 +77,7 @@ public static String formatDate(String dateString) { LocalDate.parse(dateString, outputFormatter); return dateString; } catch (DateTimeParseException ex2) { - logger.warning(ex2.toString()); + logger.warn(ex2.toString()); throw ex2; } } @@ -118,7 +100,7 @@ public static void generatePdf(String jrxmlFileName, Map payload JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, payload, dataSource); JasperExportManager.exportReportToPdfFile(jasperPrint, Constants.REPORT_FILE_NAME); } catch (JRException ex) { - Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); throw ex; } } diff --git a/src/main/java/openconsignment/common/ViewLoader.java b/src/main/java/openconsignment/common/ViewLoader.java index afa0f8a..1ecc4a0 100644 --- a/src/main/java/openconsignment/common/ViewLoader.java +++ b/src/main/java/openconsignment/common/ViewLoader.java @@ -2,18 +2,17 @@ import java.io.IOException; import java.util.Objects; -import java.util.logging.Level; -import java.util.logging.Logger; import javafx.fxml.FXMLLoader; import javafx.scene.Group; import javafx.scene.Parent; import javafx.scene.layout.BorderPane; import javafx.util.Callback; import javax.inject.Inject; -import openconsignment.controller.MainController; -import openconsignment.controller.entry.EntryController; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class ViewLoader { + private static final Logger logger = LoggerFactory.getLogger(ViewLoader.class); private final Callback, Object> controllerFactory; @@ -30,7 +29,7 @@ public void loadRoot(String resourcePath, Group root) { root.getChildren().setAll(parent); } catch (IOException ex) { Utils.showAlert(ex.toString()); - Logger.getLogger(EntryController.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); } } @@ -42,7 +41,7 @@ public void loadCenter(String resourcePath, BorderPane center) { center.setCenter(parent); } catch (IOException ex) { Utils.showAlert(ex.toString()); - Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); } } } diff --git a/src/main/java/openconsignment/controller/WelcomeScreenController.java b/src/main/java/openconsignment/controller/WelcomeScreenController.java index 3b5e9a6..aaf45cd 100644 --- a/src/main/java/openconsignment/controller/WelcomeScreenController.java +++ b/src/main/java/openconsignment/controller/WelcomeScreenController.java @@ -3,8 +3,6 @@ import java.io.IOException; import java.net.URL; import java.util.ResourceBundle; -import java.util.logging.Level; -import java.util.logging.Logger; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; @@ -13,8 +11,11 @@ import javax.inject.Inject; import openconsignment.common.Constants; import openconsignment.common.Utils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class WelcomeScreenController implements Initializable { + private static final Logger logger = LoggerFactory.getLogger(WelcomeScreenController.class); @FXML private Text organization_name; @@ -31,7 +32,7 @@ private void handleOpenGithub(ActionEvent event) { Utils.launchBrowser(url); } catch (IOException ex) { Utils.showAlert(ex.toString()); - Logger.getLogger(WelcomeScreenController.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); } } diff --git a/src/main/java/openconsignment/controller/entry/BillEntryController.java b/src/main/java/openconsignment/controller/entry/BillEntryController.java index 656a386..48f06a7 100644 --- a/src/main/java/openconsignment/controller/entry/BillEntryController.java +++ b/src/main/java/openconsignment/controller/entry/BillEntryController.java @@ -7,14 +7,13 @@ import java.util.ArrayList; import java.util.List; import java.util.ResourceBundle; -import java.util.logging.Level; -import java.util.logging.Logger; import java.util.stream.Collectors; import javafx.collections.FXCollections; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.*; +import javafx.scene.control.Alert.AlertType; import javafx.scene.control.cell.PropertyValueFactory; import javax.inject.Inject; import openconsignment.common.Utils; @@ -26,9 +25,12 @@ import openconsignment.service.SupplierService; import openconsignment.service.TransportService; import org.controlsfx.control.textfield.TextFields; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; @SuppressWarnings("unused") public class BillEntryController implements Initializable { + private static final Logger logger = LoggerFactory.getLogger(BillEntryController.class); @FXML private TextField supplier_field; @@ -114,7 +116,7 @@ public void initialize(URL url, ResourceBundle rb) { } catch (Exception ex) { Utils.showAlert(ex.toString()); - Logger.getLogger(BillEntryController.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); } } @@ -123,7 +125,8 @@ private void addToList(ActionEvent event) { if (bill_no_field.getText().isEmpty() || lr_field.getText().isEmpty() || pm_field.getText().isEmpty()) { - Utils.showAlert("Please ensure that the Bill No., LR No. and PM fields are filled up properly.", 2); + Utils.showAlert( + "Please ensure that the Bill No., LR No. and PM fields are filled up properly.", AlertType.WARNING); return; } lrList.add(new LR(bill_no_field.getText(), lr_field.getText(), pm_field.getText())); @@ -163,10 +166,10 @@ private void save(ActionEvent event) { try { billService.saveBill(billEntity); clearAllFields(); - Utils.showAlert("Saved Successfully", 1); + Utils.showAlert("Saved Successfully", AlertType.INFORMATION); } catch (Exception ex) { Utils.showAlert(ex.toString()); - Logger.getLogger(BillEntryController.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); } } @@ -188,13 +191,14 @@ private void clearAllFields() { @FXML private void replaceInList(ActionEvent event) { if (lrTable.getSelectionModel().getSelectedIndex() == -1) { - Utils.showAlert("Please select an item from the LR Table", 2); + Utils.showAlert("Please select an item from the LR Table", AlertType.WARNING); return; } if (bill_no_field.getText().isEmpty() || lr_field.getText().isEmpty() || pm_field.getText().isEmpty()) { - Utils.showAlert("Please ensure that the Bill No., LR No. and PM fields are filled up properly.", 2); + Utils.showAlert( + "Please ensure that the Bill No., LR No. and PM fields are filled up properly.", AlertType.WARNING); return; } lrList.set( @@ -208,7 +212,7 @@ private void replaceInList(ActionEvent event) { @FXML private void deleteFromList(ActionEvent event) { if (lrTable.getSelectionModel().getSelectedIndex() == -1) { - Utils.showAlert("Please select an item from the LR Table", 2); + Utils.showAlert("Please select an item from the LR Table", AlertType.WARNING); return; } lrList.remove(lrTable.getSelectionModel().getSelectedIndex()); @@ -221,7 +225,7 @@ private void fetchBill(ActionEvent event) { BillEntity billEntity = billService.getBill(bill_no_field.getText()); if (null == billEntity) { - Utils.showAlert("Record not found. Please try again later.", 2); + Utils.showAlert("Record not found. Please try again later.", AlertType.WARNING); return; } @@ -241,7 +245,7 @@ private void fetchBill(ActionEvent event) { } catch (SQLException ex) { Utils.showAlert(ex.toString()); - Logger.getLogger(BillEntryController.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); } } @@ -270,10 +274,11 @@ private void deleteEntry(ActionEvent event) { bill_amount_field.setText(""); lrTable.setItems(FXCollections.observableArrayList()); - Utils.showAlert(bill_no_field.getText().toUpperCase() + " Entry was successfully deleted.", 1); + Utils.showAlert( + bill_no_field.getText().toUpperCase() + " Entry was successfully deleted.", AlertType.INFORMATION); } catch (SQLException ex) { Utils.showAlert(ex.toString()); - Logger.getLogger(BillEntryController.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); } } } diff --git a/src/main/java/openconsignment/controller/entry/CollectionEntryController.java b/src/main/java/openconsignment/controller/entry/CollectionEntryController.java index 89f6eed..4636023 100644 --- a/src/main/java/openconsignment/controller/entry/CollectionEntryController.java +++ b/src/main/java/openconsignment/controller/entry/CollectionEntryController.java @@ -9,13 +9,12 @@ import java.util.ArrayList; import java.util.List; import java.util.ResourceBundle; -import java.util.logging.Level; -import java.util.logging.Logger; import javafx.collections.FXCollections; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.*; +import javafx.scene.control.Alert.AlertType; import javafx.scene.control.cell.PropertyValueFactory; import javax.inject.Inject; import net.sf.jasperreports.engine.JRException; @@ -28,6 +27,8 @@ import openconsignment.service.BillService; import openconsignment.service.BuyerService; import openconsignment.service.CollectionService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class CollectionEntryController implements Initializable { @@ -36,7 +37,7 @@ public class CollectionEntryController implements Initializable { private List billNoComboList; private int previouslyDue; - private static final Logger logger = Logger.getLogger(CollectionEntryController.class.getName()); + private static final Logger logger = LoggerFactory.getLogger(CollectionEntryController.class); @FXML private TextField dd_no_field; @@ -151,7 +152,7 @@ private void fillBuyerNameCombo() { buyer_name.setItems(FXCollections.observableArrayList(buyerNameComboList)); } catch (SQLException ex) { Utils.showAlert(ex.toString()); - logger.log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); } } @@ -165,7 +166,7 @@ private void fillBillNoCombo() { bill_no_combo.setItems(FXCollections.observableArrayList(billNoComboList)); } catch (SQLException ex) { Utils.showAlert(ex.toString()); - logger.log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); } } @@ -183,7 +184,7 @@ private void fetchData() { updateCollectionDue(); } catch (SQLException ex) { Utils.showAlert(ex.toString()); - logger.log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); } } @@ -198,7 +199,7 @@ private void addCollection(ActionEvent ignoredEvent) { || amount_collected_field.getText().isEmpty() || dd_no_field.getText().isEmpty() || dd_date_field.getValue().toString().isEmpty()) { - Utils.showAlert("Please check whether the fields are properly filled or not.", 2); + Utils.showAlert("Please check whether the fields are properly filled or not.", AlertType.WARNING); return; } @@ -226,7 +227,7 @@ private void updateCollectionDue() { try { amountCollected = Integer.parseInt(amount_collected_field.getText()); } catch (Exception ex) { - logger.log(Level.WARNING, "cannot be converted to integer"); + logger.warn("cannot be converted to integer"); } collection_due_field.setText(Integer.toString(previouslyDue - amountCollected)); } @@ -254,7 +255,7 @@ private void clearRepeatingFields() { @FXML private void replaceCollection(ActionEvent ignoredEvent) { if (collection_tableview.getSelectionModel().getSelectedIndex() == -1) { - Utils.showAlert("Please select an item from the Collection Table", 2); + Utils.showAlert("Please select an item from the Collection Table", AlertType.WARNING); return; } if (bill_no_combo.getValue().isEmpty() @@ -266,7 +267,7 @@ private void replaceCollection(ActionEvent ignoredEvent) { || amount_collected_field.getText().isEmpty() || dd_no_field.getText().isEmpty() || dd_date_field.getValue().toString().isEmpty()) { - Utils.showAlert("Please check whether the fields are properly filled or not.", 2); + Utils.showAlert("Please check whether the fields are properly filled or not.", AlertType.WARNING); return; } collectionItemList.set( @@ -289,7 +290,7 @@ private void replaceCollection(ActionEvent ignoredEvent) { @FXML private void deleteCollection(ActionEvent ignoredEvent) { if (collection_tableview.getSelectionModel().getSelectedIndex() == -1) { - Utils.showAlert("Please select an item from the LR Table", 2); + Utils.showAlert("Please select an item from the LR Table", AlertType.WARNING); return; } collectionItemList.remove(collection_tableview.getSelectionModel().getSelectedIndex()); @@ -316,13 +317,13 @@ private void clearAllFields() { private void fetchCollectionVoucher() { String voucherNo = voucher_no_field.getText().trim(); if (voucherNo.isEmpty()) { - Utils.showAlert("Please enter a voucher number.", 2); + Utils.showAlert("Please enter a voucher number.", AlertType.WARNING); return; } try { CollectionEntity voucher = collectionService.getCollection(voucherNo); if (voucher == null) { - Utils.showAlert("Collection voucher not found", 2); + Utils.showAlert("Collection voucher not found", AlertType.WARNING); return; } List items = new ArrayList<>(); @@ -350,7 +351,7 @@ private void fetchCollectionVoucher() { total_amount_field.setText(voucher.getTotalAmount()); } catch (SQLException ex) { Utils.showAlert(ex.toString()); - logger.log(Level.SEVERE, "Failed to fetch collection voucher", ex); + logger.error("Failed to fetch collection voucher", ex); } } @@ -358,20 +359,20 @@ private void fetchCollectionVoucher() { private void printCollectionVoucher() { String voucherNo = voucher_no_field.getText().trim(); if (voucherNo.isEmpty()) { - Utils.showAlert("Please enter a voucher number.", 2); + Utils.showAlert("Please enter a voucher number.", AlertType.WARNING); return; } try { if (collectionService.getCollection(voucherNo) == null) { - Utils.showAlert("Collection voucher not found", 2); + Utils.showAlert("Collection voucher not found", AlertType.WARNING); return; } collectionService.generatePdf(voucherNo); Utils.launchPdf(Constants.REPORT_FILE_NAME); - Utils.showAlert("Report Successfully Generated", 1); + Utils.showAlert("Report Successfully Generated", AlertType.INFORMATION); } catch (IOException | JRException | SQLException ex) { Utils.showAlert(ex.toString()); - logger.log(Level.SEVERE, "Failed to print collection voucher", ex); + logger.error("Failed to print collection voucher", ex); } } @@ -379,12 +380,12 @@ private void printCollectionVoucher() { private void deleteCollectionVoucher() { String voucherNo = voucher_no_field.getText().trim(); if (voucherNo.isEmpty()) { - Utils.showAlert("Please enter a voucher number.", 2); + Utils.showAlert("Please enter a voucher number.", AlertType.WARNING); return; } try { if (collectionService.getCollection(voucherNo) == null) { - Utils.showAlert("Collection voucher not found", 2); + Utils.showAlert("Collection voucher not found", AlertType.WARNING); return; } Alert alert = new Alert( @@ -398,10 +399,10 @@ private void deleteCollectionVoucher() { collectionService.deleteCollection(voucherNo); clearAllFields(); updateLastVoucher(); - Utils.showAlert(voucherNo + " was successfully deleted.", 1); + Utils.showAlert(voucherNo + " was successfully deleted.", AlertType.INFORMATION); } catch (SQLException ex) { Utils.showAlert(ex.toString()); - logger.log(Level.SEVERE, "Failed to delete collection voucher", ex); + logger.error("Failed to delete collection voucher", ex); } } @@ -421,7 +422,7 @@ private void saveData(ActionEvent ignoredEvent) { if (voucher_no_field.getText().isEmpty() || voucher_date_field.getValue() == null || voucher_date_field.getValue().toString().isEmpty()) { - Utils.showAlert("Check whether the Voucher No. and the Voucher Date is properly filled", 2); + Utils.showAlert("Check whether the Voucher No. and the Voucher Date is properly filled", AlertType.WARNING); return; } @@ -448,10 +449,10 @@ private void saveData(ActionEvent ignoredEvent) { .build(); try { collectionService.saveCollection(collectionEntity); - Utils.showAlert("Saved Successfully", 1); + Utils.showAlert("Saved Successfully", AlertType.INFORMATION); } catch (SQLException ex) { Utils.showAlert(ex.toString()); - logger.log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); return; } @@ -467,7 +468,7 @@ private void updateLastVoucher() { voucher_no_field.setText(upcomingVoucherNo); } catch (SQLException ex) { Utils.showAlert(ex.toString()); - logger.log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); } } } diff --git a/src/main/java/openconsignment/controller/entry/PaymentEntryController.java b/src/main/java/openconsignment/controller/entry/PaymentEntryController.java index 5101e33..c2fed71 100644 --- a/src/main/java/openconsignment/controller/entry/PaymentEntryController.java +++ b/src/main/java/openconsignment/controller/entry/PaymentEntryController.java @@ -7,14 +7,13 @@ import java.sql.SQLException; import java.time.format.DateTimeFormatter; import java.util.*; -import java.util.logging.Level; -import java.util.logging.Logger; import java.util.stream.Collectors; import javafx.collections.FXCollections; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.*; +import javafx.scene.control.Alert.AlertType; import javafx.scene.control.cell.PropertyValueFactory; import javafx.scene.text.Text; import javax.inject.Inject; @@ -28,6 +27,8 @@ import openconsignment.service.BillService; import openconsignment.service.PaymentService; import openconsignment.service.SupplierService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; @SuppressWarnings("unused") public class PaymentEntryController implements Initializable { @@ -37,7 +38,7 @@ public class PaymentEntryController implements Initializable { private List billNoComboList; private int previouslyDue; - private static final Logger logger = Logger.getLogger(PaymentEntryController.class.getName()); + private static final Logger logger = LoggerFactory.getLogger(PaymentEntryController.class); @FXML private TextField dd_no_field; @@ -176,7 +177,7 @@ private void addPayment(ActionEvent ignoredEvent) { || amount_paid_field.getText().isEmpty() || dd_no_field.getText().isEmpty() || dd_date_field.getValue().toString().isEmpty()) { - Utils.showAlert("Please check whether the fields are properly filled or not.", 2); + Utils.showAlert("Please check whether the fields are properly filled or not.", AlertType.WARNING); return; } @@ -203,7 +204,7 @@ private void addPayment(ActionEvent ignoredEvent) { private void replacePayment(ActionEvent ignoredEvent) { if (payment_tableview.getSelectionModel().getSelectedIndex() == -1) { - Utils.showAlert("Please select an item from the Payment Table", 2); + Utils.showAlert("Please select an item from the Payment Table", AlertType.WARNING); return; } @@ -217,7 +218,7 @@ private void replacePayment(ActionEvent ignoredEvent) { || amount_paid_field.getText().isEmpty() || dd_no_field.getText().isEmpty() || dd_date_field.getValue().toString().isEmpty()) { - Utils.showAlert("Please check whether the fields are properly filled or not.", 2); + Utils.showAlert("Please check whether the fields are properly filled or not.", AlertType.WARNING); return; } @@ -245,7 +246,7 @@ private void replacePayment(ActionEvent ignoredEvent) { @FXML private void deletePayment(ActionEvent ignoredEvent) { if (payment_tableview.getSelectionModel().getSelectedIndex() == -1) { - Utils.showAlert("Please select an item from the Payment Table", 2); + Utils.showAlert("Please select an item from the Payment Table", AlertType.WARNING); return; } paymentItems.remove(payment_tableview.getSelectionModel().getSelectedIndex()); @@ -301,7 +302,7 @@ private void saveData(ActionEvent ignoredEvent) { if (voucher_no_field.getText().isEmpty() || voucher_date_field.getValue() == null || voucher_date_field.getValue().toString().isEmpty()) { - Utils.showAlert("Check whether the Voucher No. and the Voucher Date is properly filled", 2); + Utils.showAlert("Check whether the Voucher No. and the Voucher Date is properly filled", AlertType.WARNING); return; } @@ -330,11 +331,11 @@ private void saveData(ActionEvent ignoredEvent) { try { paymentService.savePayment(paymentEntity); - Utils.showAlert("Saved Successfully", 1); + Utils.showAlert("Saved Successfully", AlertType.INFORMATION); clearAllFields(); } catch (SQLException ex) { Utils.showAlert(ex.toString()); - Logger.getLogger(PaymentEntryController.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); } clearAllFields(); @@ -351,7 +352,7 @@ private void updateLastVoucher() { voucher_no_field.setText(upcomingVoucherNo); } catch (SQLException ex) { Utils.showAlert(ex.toString()); - logger.log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); } } @@ -361,7 +362,7 @@ private void fillSupplierCombo() { supplier_name_combo.setItems(FXCollections.observableArrayList(supplierNameComboList)); } catch (SQLException ex) { Utils.showAlert(ex.toString()); - logger.log(Level.SEVERE, "Failed to populate the supplier combo", ex); + logger.error("Failed to populate the supplier combo", ex); } } @@ -372,7 +373,7 @@ private void fillBillNoCombo(ActionEvent ignoredEvent) { bill_no_combo.setItems(FXCollections.observableArrayList(billNoComboList)); } catch (SQLException ex) { Utils.showAlert(ex.toString()); - logger.log(Level.SEVERE, "Failed to populate bill number combo", ex); + logger.error("Failed to populate bill number combo", ex); } } @@ -390,7 +391,7 @@ private void fetchBillNos(ActionEvent ignoredEvent) { updateDueAmount(); } catch (SQLException ex) { Utils.showAlert(ex.toString()); - logger.log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); } } @@ -400,7 +401,7 @@ private void updateDueAmount() { try { amountPaid = Integer.parseInt(amount_paid_field.getText()); } catch (Exception ex) { - logger.log(Level.WARNING, "cannot be converted to integer"); + logger.warn("cannot be converted to integer"); } due_amount_field.setText(Integer.toString(previouslyDue - amountPaid)); } @@ -441,7 +442,7 @@ public void fetchPaymentVoucher() { // delete_entry_btn.setDisable(false); } catch (SQLException ex) { Utils.showAlert(ex.toString()); - Logger.getLogger(PaymentEntryController.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); } } @@ -449,10 +450,10 @@ public void printPaymentVoucher() { try { paymentService.generatePdf(voucher_no_field.getText()); Utils.launchPdf(Constants.REPORT_FILE_NAME); - Utils.showAlert("Report Successfully Generated", 1); + Utils.showAlert("Report Successfully Generated", AlertType.INFORMATION); } catch (IOException | JRException | SQLException ex) { Utils.showAlert(ex.toString()); - Logger.getLogger(PaymentEntryController.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); } } @@ -472,10 +473,12 @@ public void deletePaymentVoucher() { try { paymentService.deletePayment(voucher_no_field.getText()); - Utils.showAlert(voucher_no_field.getText().toUpperCase() + " Entry was successfully deleted.", 1); + Utils.showAlert( + voucher_no_field.getText().toUpperCase() + " Entry was successfully deleted.", + AlertType.INFORMATION); } catch (SQLException ex) { Utils.showAlert(ex.toString()); - Logger.getLogger(PaymentEntryController.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); } } } diff --git a/src/main/java/openconsignment/controller/history/BillHistoryController.java b/src/main/java/openconsignment/controller/history/BillHistoryController.java index 2856295..6b8dbd3 100644 --- a/src/main/java/openconsignment/controller/history/BillHistoryController.java +++ b/src/main/java/openconsignment/controller/history/BillHistoryController.java @@ -4,8 +4,6 @@ import java.sql.SQLException; import java.util.List; import java.util.ResourceBundle; -import java.util.logging.Level; -import java.util.logging.Logger; import javafx.collections.FXCollections; import javafx.fxml.FXML; import javafx.fxml.Initializable; @@ -17,8 +15,11 @@ import openconsignment.common.Utils; import openconsignment.entity.BillEntity; import openconsignment.service.BillService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class BillHistoryController implements Initializable { + private static final Logger logger = LoggerFactory.getLogger(BillHistoryController.class); @FXML private Group root; @@ -69,7 +70,7 @@ public void initialize(URL url, ResourceBundle rb) { tableView.setItems(FXCollections.observableArrayList(billList)); } catch (SQLException ex) { Utils.showAlert(ex.toString()); - Logger.getLogger(BillHistoryController.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); } } } diff --git a/src/main/java/openconsignment/controller/history/CollectionHistoryController.java b/src/main/java/openconsignment/controller/history/CollectionHistoryController.java index f5f3eb1..a52e65c 100644 --- a/src/main/java/openconsignment/controller/history/CollectionHistoryController.java +++ b/src/main/java/openconsignment/controller/history/CollectionHistoryController.java @@ -4,8 +4,6 @@ import java.sql.SQLException; import java.util.List; import java.util.ResourceBundle; -import java.util.logging.Level; -import java.util.logging.Logger; import javafx.collections.FXCollections; import javafx.fxml.FXML; import javafx.fxml.Initializable; @@ -17,8 +15,11 @@ import openconsignment.common.Utils; import openconsignment.entity.CollectionEntity; import openconsignment.service.CollectionService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class CollectionHistoryController implements Initializable { + private static final Logger logger = LoggerFactory.getLogger(CollectionHistoryController.class); @FXML private Group root; @@ -57,7 +58,7 @@ public void initialize(URL url, ResourceBundle rb) { tableView.setItems(FXCollections.observableArrayList(collectionEntityList)); } catch (SQLException ex) { Utils.showAlert(ex.toString()); - Logger.getLogger(CollectionHistoryController.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); } } } diff --git a/src/main/java/openconsignment/controller/history/PaymentHistoryController.java b/src/main/java/openconsignment/controller/history/PaymentHistoryController.java index 4121ef5..333aa6a 100644 --- a/src/main/java/openconsignment/controller/history/PaymentHistoryController.java +++ b/src/main/java/openconsignment/controller/history/PaymentHistoryController.java @@ -4,8 +4,6 @@ import java.sql.SQLException; import java.util.List; import java.util.ResourceBundle; -import java.util.logging.Level; -import java.util.logging.Logger; import javafx.collections.FXCollections; import javafx.fxml.FXML; import javafx.fxml.Initializable; @@ -17,8 +15,11 @@ import openconsignment.common.Utils; import openconsignment.entity.PaymentEntity; import openconsignment.service.PaymentService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class PaymentHistoryController implements Initializable { + private static final Logger logger = LoggerFactory.getLogger(PaymentHistoryController.class); @FXML private Group root; @@ -57,7 +58,7 @@ public void initialize(URL url, ResourceBundle rb) { tableView.setItems(FXCollections.observableArrayList(paymentEntityList)); } catch (SQLException ex) { Utils.showAlert(ex.toString()); - Logger.getLogger(PaymentHistoryController.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); } } } diff --git a/src/main/java/openconsignment/controller/master/BuyerController.java b/src/main/java/openconsignment/controller/master/BuyerController.java index 4b6ec9d..0c54ff0 100644 --- a/src/main/java/openconsignment/controller/master/BuyerController.java +++ b/src/main/java/openconsignment/controller/master/BuyerController.java @@ -4,19 +4,21 @@ import java.sql.SQLException; import java.util.List; import java.util.ResourceBundle; -import java.util.logging.Level; -import java.util.logging.Logger; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; +import javafx.scene.control.Alert.AlertType; import javafx.scene.control.Button; import javafx.scene.control.ListView; import javafx.scene.control.TextField; import javax.inject.Inject; import openconsignment.common.Utils; import openconsignment.service.BuyerService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class BuyerController implements Initializable { + private static final Logger logger = LoggerFactory.getLogger(BuyerController.class); @FXML private Button add_btn; @@ -62,7 +64,7 @@ private void addMaster(ActionEvent event) { refreshList(); } catch (SQLException ex) { Utils.showAlert(ex.toString()); - Logger.getLogger(BuyerController.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); } } @@ -77,7 +79,7 @@ private void deleteMaster(ActionEvent event) { refreshList(); } catch (SQLException ex) { Utils.showAlert(ex.toString()); - Logger.getLogger(BuyerController.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); } } @@ -88,7 +90,7 @@ private void refreshList() { listView.getItems().setAll(buyerList); } catch (SQLException ex) { Utils.showAlert(ex.toString()); - Logger.getLogger(BuyerController.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); } } @@ -100,11 +102,11 @@ private void renameMaster(ActionEvent event) { } try { buyerService.renameBuyer(listView.getSelectionModel().getSelectedItem(), rename_field.getText()); - Utils.showAlert("Success", 1); + Utils.showAlert("Success", AlertType.INFORMATION); refreshList(); } catch (SQLException ex) { Utils.showAlert(ex.toString()); - Logger.getLogger(SupplierController.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); } } } diff --git a/src/main/java/openconsignment/controller/master/SupplierController.java b/src/main/java/openconsignment/controller/master/SupplierController.java index 83b84fa..e9be20a 100644 --- a/src/main/java/openconsignment/controller/master/SupplierController.java +++ b/src/main/java/openconsignment/controller/master/SupplierController.java @@ -4,19 +4,21 @@ import java.sql.SQLException; import java.util.List; import java.util.ResourceBundle; -import java.util.logging.Level; -import java.util.logging.Logger; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; +import javafx.scene.control.Alert.AlertType; import javafx.scene.control.Button; import javafx.scene.control.ListView; import javafx.scene.control.TextField; import javax.inject.Inject; import openconsignment.common.Utils; import openconsignment.service.SupplierService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class SupplierController implements Initializable { + private static final Logger logger = LoggerFactory.getLogger(SupplierController.class); @FXML private Button add_btn; @@ -62,7 +64,7 @@ private void addMaster(ActionEvent event) { refreshList(); } catch (SQLException ex) { Utils.showAlert(ex.toString()); - Logger.getLogger(SupplierController.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); } } @@ -76,11 +78,11 @@ private void renameMaster(ActionEvent event) { try { supplierService.renameSupplier(listView.getSelectionModel().getSelectedItem(), rename_field.getText()); - Utils.showAlert("Success", 1); + Utils.showAlert("Success", AlertType.INFORMATION); refreshList(); } catch (SQLException ex) { Utils.showAlert(ex.toString()); - Logger.getLogger(SupplierController.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); } } @@ -95,7 +97,7 @@ private void deleteMaster(ActionEvent event) { refreshList(); } catch (SQLException ex) { Utils.showAlert(ex.toString()); - Logger.getLogger(SupplierController.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); } } @@ -106,7 +108,7 @@ private void refreshList() { listView.getItems().setAll(supplierList); } catch (SQLException ex) { Utils.showAlert(ex.toString()); - Logger.getLogger(SupplierController.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); } } } diff --git a/src/main/java/openconsignment/controller/master/TransportController.java b/src/main/java/openconsignment/controller/master/TransportController.java index 23a9d7e..67dbd46 100644 --- a/src/main/java/openconsignment/controller/master/TransportController.java +++ b/src/main/java/openconsignment/controller/master/TransportController.java @@ -4,19 +4,21 @@ import java.sql.SQLException; import java.util.List; import java.util.ResourceBundle; -import java.util.logging.Level; -import java.util.logging.Logger; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; +import javafx.scene.control.Alert.AlertType; import javafx.scene.control.Button; import javafx.scene.control.ListView; import javafx.scene.control.TextField; import javax.inject.Inject; import openconsignment.common.Utils; import openconsignment.service.TransportService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class TransportController implements Initializable { + private static final Logger logger = LoggerFactory.getLogger(TransportController.class); @FXML private Button add_btn; @@ -62,7 +64,7 @@ private void addMaster(ActionEvent event) { refreshList(); } catch (SQLException ex) { Utils.showAlert(ex.toString()); - Logger.getLogger(TransportController.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); } } @@ -77,7 +79,7 @@ private void deleteMaster(ActionEvent event) { refreshList(); } catch (SQLException ex) { Utils.showAlert(ex.toString()); - Logger.getLogger(TransportController.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); } } @@ -88,7 +90,7 @@ private void refreshList() { listView.getItems().setAll(transportList); } catch (SQLException ex) { Utils.showAlert(ex.toString()); - Logger.getLogger(TransportController.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); } } @@ -100,11 +102,11 @@ private void renameMaster(ActionEvent event) { } try { transportService.renameTransport(listView.getSelectionModel().getSelectedItem(), rename_field.getText()); - Utils.showAlert("Success", 1); + Utils.showAlert("Success", AlertType.INFORMATION); refreshList(); } catch (SQLException ex) { Utils.showAlert(ex.toString()); - Logger.getLogger(TransportController.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); } } } diff --git a/src/main/java/openconsignment/controller/report/BuyerLedgerController.java b/src/main/java/openconsignment/controller/report/BuyerLedgerController.java index 0427e1d..80872f4 100644 --- a/src/main/java/openconsignment/controller/report/BuyerLedgerController.java +++ b/src/main/java/openconsignment/controller/report/BuyerLedgerController.java @@ -5,12 +5,11 @@ import java.sql.SQLException; import java.util.List; import java.util.ResourceBundle; -import java.util.logging.Level; -import java.util.logging.Logger; import javafx.collections.FXCollections; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; +import javafx.scene.control.Alert.AlertType; import javafx.scene.control.Button; import javafx.scene.control.ComboBox; import javafx.scene.control.RadioButton; @@ -20,8 +19,11 @@ import openconsignment.common.Constants; import openconsignment.common.Utils; import openconsignment.service.BuyerService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class BuyerLedgerController implements Initializable { + private static final Logger logger = LoggerFactory.getLogger(BuyerLedgerController.class); @FXML private RadioButton buyer_ledger_radio; @@ -55,7 +57,7 @@ public void initialize(URL url, ResourceBundle rb) { buyer_name_combo.setItems(FXCollections.observableArrayList(buyerNames)); } catch (SQLException ex) { Utils.showAlert(ex.toString()); - Logger.getLogger(BuyerLedgerController.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); } } @@ -65,10 +67,10 @@ private void generatePDF(ActionEvent event) { buyerService.generatePdf( buyer_name_combo.getSelectionModel().getSelectedItem(), agewise_outstanding_radio.isSelected()); Utils.launchPdf(Constants.REPORT_FILE_NAME); - Utils.showAlert("Report Successfully Generated", 1); + Utils.showAlert("Report Successfully Generated", AlertType.INFORMATION); } catch (SQLException | IOException | JRException ex) { Utils.showAlert(ex.toString()); - Logger.getLogger(BuyerLedgerController.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); } } @@ -76,10 +78,10 @@ private void generatePDF(ActionEvent event) { private void launchPdf(ActionEvent event) { try { Utils.launchPdf(Constants.REPORT_FILE_NAME); - Utils.showAlert("PDF opened successfully.", 1); + Utils.showAlert("PDF opened successfully.", AlertType.INFORMATION); } catch (IOException ex) { Utils.showAlert(ex.toString()); - Logger.getLogger(BuyerLedgerController.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); } } } diff --git a/src/main/java/openconsignment/controller/report/SupplierLedgerController.java b/src/main/java/openconsignment/controller/report/SupplierLedgerController.java index 73e5cef..340d15c 100644 --- a/src/main/java/openconsignment/controller/report/SupplierLedgerController.java +++ b/src/main/java/openconsignment/controller/report/SupplierLedgerController.java @@ -5,12 +5,11 @@ import java.sql.SQLException; import java.util.List; import java.util.ResourceBundle; -import java.util.logging.Level; -import java.util.logging.Logger; import javafx.collections.FXCollections; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; +import javafx.scene.control.Alert.AlertType; import javafx.scene.control.Button; import javafx.scene.control.ComboBox; import javafx.scene.control.RadioButton; @@ -20,8 +19,11 @@ import openconsignment.common.Constants; import openconsignment.common.Utils; import openconsignment.service.SupplierService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class SupplierLedgerController implements Initializable { + private static final Logger logger = LoggerFactory.getLogger(SupplierLedgerController.class); @FXML private ComboBox supplier_name_combo; @@ -55,7 +57,7 @@ public void initialize(URL url, ResourceBundle rb) { supplier_name_combo.setItems(FXCollections.observableArrayList(supplierList)); } catch (SQLException ex) { Utils.showAlert(ex.toString()); - Logger.getLogger(SupplierLedgerController.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); } } @@ -65,10 +67,10 @@ private void generatePDF(ActionEvent event) { supplierService.generatePdf( supplier_name_combo.getSelectionModel().getSelectedItem(), agewise_outstanding_radio.isSelected()); Utils.launchPdf(Constants.REPORT_FILE_NAME); - Utils.showAlert("Report Successfully Generated", 1); + Utils.showAlert("Report Successfully Generated", AlertType.INFORMATION); } catch (IOException | SQLException | JRException ex) { Utils.showAlert(ex.toString()); - Logger.getLogger(SupplierLedgerController.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); } } @@ -76,10 +78,10 @@ private void generatePDF(ActionEvent event) { private void launchPdf(ActionEvent event) { try { Utils.launchPdf(Constants.REPORT_FILE_NAME); - Utils.showAlert("PDF opened successfully.", 1); + Utils.showAlert("PDF opened successfully.", AlertType.INFORMATION); } catch (IOException ex) { Utils.showAlert(ex.toString()); - Logger.getLogger(SupplierLedgerController.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); } } } diff --git a/src/main/java/openconsignment/controller/report/TransportLedgerController.java b/src/main/java/openconsignment/controller/report/TransportLedgerController.java index 1616056..a26fc7c 100644 --- a/src/main/java/openconsignment/controller/report/TransportLedgerController.java +++ b/src/main/java/openconsignment/controller/report/TransportLedgerController.java @@ -4,12 +4,11 @@ import java.net.URL; import java.sql.SQLException; import java.util.*; -import java.util.logging.Level; -import java.util.logging.Logger; import javafx.collections.FXCollections; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; +import javafx.scene.control.Alert.AlertType; import javafx.scene.control.Button; import javafx.scene.control.CheckBox; import javafx.scene.control.ComboBox; @@ -19,8 +18,11 @@ import openconsignment.common.Constants; import openconsignment.common.Utils; import openconsignment.service.TransportService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class TransportLedgerController implements Initializable { + private static final Logger logger = LoggerFactory.getLogger(TransportLedgerController.class); @FXML private CheckBox all_time_checkbox; @@ -54,7 +56,7 @@ public void initialize(URL url, ResourceBundle rb) { transport_name_combo.setItems(FXCollections.observableArrayList(transportList)); } catch (SQLException ex) { Utils.showAlert(ex.toString()); - Logger.getLogger(TransportLedgerController.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); } } @@ -73,10 +75,10 @@ private void generatePDF(ActionEvent event) { transportService.generatePdf( transport_name_combo.getSelectionModel().getSelectedItem(), fromDate, toDate); Utils.launchPdf(Constants.REPORT_FILE_NAME); - Utils.showAlert("Report Successfully Generated", 1); + Utils.showAlert("Report Successfully Generated", AlertType.INFORMATION); } catch (JRException | SQLException | IOException ex) { Utils.showAlert(ex.toString()); - Logger.getLogger(TransportLedgerController.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); } } @@ -84,10 +86,10 @@ private void generatePDF(ActionEvent event) { private void launchPdf(ActionEvent event) { try { Utils.launchPdf(Constants.REPORT_FILE_NAME); - Utils.showAlert("PDF opened successfully.", 1); + Utils.showAlert("PDF opened successfully.", AlertType.INFORMATION); } catch (IOException ex) { Utils.showAlert(ex.toString()); - Logger.getLogger(TransportLedgerController.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); } } } diff --git a/src/main/java/openconsignment/di/DatabaseModule.java b/src/main/java/openconsignment/di/DatabaseModule.java index 7083c0c..c6ae947 100644 --- a/src/main/java/openconsignment/di/DatabaseModule.java +++ b/src/main/java/openconsignment/di/DatabaseModule.java @@ -9,15 +9,16 @@ import java.sql.SQLException; import java.sql.Statement; import java.util.Objects; -import java.util.logging.Level; -import java.util.logging.Logger; import javax.inject.Singleton; import openconsignment.common.Constants; import openconsignment.common.Utils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; @Module @SuppressWarnings("unused") public class DatabaseModule { + private static final Logger logger = LoggerFactory.getLogger(DatabaseModule.class); @Provides @Singleton @@ -29,7 +30,7 @@ static Connection provideConnection() { return conn; } catch (ClassNotFoundException | SQLException ex) { Utils.showAlert(ex.toString()); - Logger.getLogger(DatabaseModule.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); throw new RuntimeException(ex); } } @@ -55,7 +56,7 @@ private static void initDatabase(Connection conn) { conn.setAutoCommit(true); } } catch (Exception ex) { - Logger.getLogger(DatabaseModule.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); } } diff --git a/src/main/java/openconsignment/repository/BillRepository.java b/src/main/java/openconsignment/repository/BillRepository.java index 915a8d8..a1a5971 100644 --- a/src/main/java/openconsignment/repository/BillRepository.java +++ b/src/main/java/openconsignment/repository/BillRepository.java @@ -6,15 +6,16 @@ import java.sql.SQLException; import java.util.ArrayList; import java.util.List; -import java.util.logging.Level; -import java.util.logging.Logger; import javax.inject.Inject; import openconsignment.common.SqlLoader; import openconsignment.common.SqlQuery; import openconsignment.entity.BillEntity; import openconsignment.entity.LREntity; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class BillRepository { + private static final Logger logger = LoggerFactory.getLogger(BillRepository.class); private final Connection conn; @@ -56,7 +57,7 @@ public void save(BillEntity billEntity) throws SQLException { conn.commit(); } catch (SQLException ex) { conn.rollback(); - Logger.getLogger(BillRepository.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); throw ex; } } @@ -98,7 +99,7 @@ public BillEntity getBill(String billNo) throws SQLException { billEntity.setLrEntities(lrpmList); return billEntity; } catch (SQLException ex) { - Logger.getLogger(BillRepository.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); throw ex; } } @@ -119,7 +120,7 @@ public void deleteBill(String billNo) throws SQLException { conn.commit(); } catch (SQLException ex) { conn.rollback(); - Logger.getLogger(BillRepository.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); throw ex; } } @@ -144,7 +145,7 @@ public List getBills() throws SQLException { } return res; } catch (SQLException ex) { - Logger.getLogger(BillRepository.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); throw ex; } } diff --git a/src/main/java/openconsignment/repository/BuyerRepository.java b/src/main/java/openconsignment/repository/BuyerRepository.java index 417eccf..8e4d23f 100644 --- a/src/main/java/openconsignment/repository/BuyerRepository.java +++ b/src/main/java/openconsignment/repository/BuyerRepository.java @@ -6,13 +6,14 @@ import java.sql.SQLException; import java.util.ArrayList; import java.util.List; -import java.util.logging.Level; -import java.util.logging.Logger; import javax.inject.Inject; import openconsignment.common.SqlLoader; import openconsignment.common.SqlQuery; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class BuyerRepository { + private static final Logger logger = LoggerFactory.getLogger(BuyerRepository.class); private final Connection conn; @@ -28,7 +29,7 @@ public void saveBuyer(String buyerName) throws SQLException { ps.setString(1, buyerName); ps.execute(); } catch (SQLException ex) { - Logger.getLogger(BuyerRepository.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); throw ex; } } @@ -40,7 +41,7 @@ public void deleteBuyer(String buyerName) throws SQLException { ps.setString(1, buyerName); ps.execute(); } catch (SQLException ex) { - Logger.getLogger(BuyerRepository.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); throw ex; } } @@ -53,7 +54,7 @@ public void renameBuyer(String oldName, String newName) throws SQLException { ps.setString(2, oldName); ps.execute(); } catch (SQLException ex) { - Logger.getLogger(BuyerRepository.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); throw ex; } } @@ -68,7 +69,7 @@ public List getAllBuyers() throws SQLException { } return buyerList; } catch (SQLException ex) { - Logger.getLogger(BuyerRepository.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); throw ex; } } diff --git a/src/main/java/openconsignment/repository/CollectionRepository.java b/src/main/java/openconsignment/repository/CollectionRepository.java index 82a2991..41daf5c 100644 --- a/src/main/java/openconsignment/repository/CollectionRepository.java +++ b/src/main/java/openconsignment/repository/CollectionRepository.java @@ -6,8 +6,6 @@ import java.sql.SQLException; import java.util.ArrayList; import java.util.List; -import java.util.logging.Level; -import java.util.logging.Logger; import javax.inject.Inject; import openconsignment.common.SqlLoader; import openconsignment.common.SqlQuery; @@ -16,8 +14,11 @@ import openconsignment.entity.CollectionEntity; import openconsignment.entity.CollectionItemEntity; import openconsignment.model.BuyerLedgerRow; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class CollectionRepository { + private static final Logger logger = LoggerFactory.getLogger(CollectionRepository.class); private final Connection conn; @@ -44,7 +45,7 @@ public List fetchPendingBillsForBuyer(String buyerName) throws SQLEx } return res; } catch (SQLException ex) { - Logger.getLogger(CollectionRepository.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); throw ex; } } @@ -62,7 +63,7 @@ public int fetchPendingAmountForBillNo(String billNo) throws SQLException { return resultSet.getInt("pending_amount"); } catch (SQLException ex) { - Logger.getLogger(CollectionRepository.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); throw ex; } } @@ -104,7 +105,7 @@ public void saveCollection(CollectionEntity collectionEntity) throws SQLExceptio } catch (SQLException ex) { conn.rollback(); - Logger.getLogger(CollectionRepository.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); throw ex; } } @@ -116,7 +117,7 @@ public String getLastVoucher() throws SQLException { ResultSet resultSet = preparedStatement.executeQuery(); return resultSet.getString("Max(`Voucher No.`)"); } catch (SQLException ex) { - Logger.getLogger(CollectionRepository.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); throw ex; } } @@ -160,7 +161,7 @@ public CollectionEntity getCollection(String voucherNo) throws SQLException { .items(collectionItemList) .build(); } catch (SQLException ex) { - Logger.getLogger(CollectionRepository.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); throw ex; } } @@ -183,7 +184,7 @@ public void deleteCollection(String voucherNo) throws SQLException { conn.commit(); } catch (SQLException ex) { Utils.showAlert(ex.toString()); - Logger.getLogger(CollectionRepository.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); conn.rollback(); } } @@ -205,7 +206,7 @@ public List getCollections() throws SQLException { } return res; } catch (SQLException ex) { - Logger.getLogger(CollectionRepository.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); throw ex; } } @@ -234,7 +235,7 @@ public List getCollectionDetailsForBuyer(String buyerName) throw } return res; } catch (SQLException ex) { - Logger.getLogger(CollectionRepository.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); throw ex; } } diff --git a/src/main/java/openconsignment/repository/PaymentRepository.java b/src/main/java/openconsignment/repository/PaymentRepository.java index eada8c5..89c1b11 100644 --- a/src/main/java/openconsignment/repository/PaymentRepository.java +++ b/src/main/java/openconsignment/repository/PaymentRepository.java @@ -6,8 +6,6 @@ import java.sql.SQLException; import java.util.ArrayList; import java.util.List; -import java.util.logging.Level; -import java.util.logging.Logger; import javax.inject.Inject; import openconsignment.common.SqlLoader; import openconsignment.common.SqlQuery; @@ -16,8 +14,11 @@ import openconsignment.entity.PaymentEntity; import openconsignment.entity.PaymentItemEntity; import openconsignment.model.SupplierLedgerRow; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class PaymentRepository { + private static final Logger logger = LoggerFactory.getLogger(PaymentRepository.class); private final Connection conn; @@ -61,7 +62,7 @@ public void savePayment(PaymentEntity paymentEntity) throws SQLException { conn.commit(); } catch (SQLException ex) { conn.rollback(); - Logger.getLogger(PaymentRepository.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); } } @@ -83,7 +84,7 @@ public List fetchPendingBillsForSupplier(String supplierName) throws } return res; } catch (SQLException ex) { - Logger.getLogger(PaymentRepository.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); throw ex; } } @@ -101,7 +102,7 @@ public int fetchPendingAmountForBillNo(String billNo) throws SQLException { return resultSet.getInt("pending_amount"); } catch (SQLException ex) { - Logger.getLogger(PaymentRepository.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); throw ex; } } @@ -113,7 +114,7 @@ public String getLastVoucher() throws SQLException { ResultSet resultSet = preparedStatement.executeQuery(); return resultSet.getString("Max(`Voucher No.`)"); } catch (SQLException ex) { - Logger.getLogger(PaymentRepository.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); throw ex; } } @@ -162,7 +163,7 @@ public PaymentEntity getPayment(String voucherNo) throws SQLException { } catch (SQLException ex) { Utils.showAlert(ex.toString()); - Logger.getLogger(PaymentRepository.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); throw ex; } } @@ -185,7 +186,7 @@ public void deletePayment(String voucherNo) throws SQLException { conn.commit(); } catch (SQLException ex) { Utils.showAlert(ex.toString()); - Logger.getLogger(PaymentRepository.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); conn.rollback(); } } @@ -207,7 +208,7 @@ public List getPayments() throws SQLException { } return res; } catch (SQLException ex) { - Logger.getLogger(PaymentRepository.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); throw ex; } } @@ -236,7 +237,7 @@ public List getPaymentDetailsForSupplier(String supplierName) } return res; } catch (SQLException ex) { - Logger.getLogger(PaymentRepository.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); throw ex; } } diff --git a/src/main/java/openconsignment/repository/SupplierRepository.java b/src/main/java/openconsignment/repository/SupplierRepository.java index ed4831a..9534dbd 100644 --- a/src/main/java/openconsignment/repository/SupplierRepository.java +++ b/src/main/java/openconsignment/repository/SupplierRepository.java @@ -6,13 +6,14 @@ import java.sql.SQLException; import java.util.ArrayList; import java.util.List; -import java.util.logging.Level; -import java.util.logging.Logger; import javax.inject.Inject; import openconsignment.common.SqlLoader; import openconsignment.common.SqlQuery; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class SupplierRepository { + private static final Logger logger = LoggerFactory.getLogger(SupplierRepository.class); private final Connection conn; @@ -28,7 +29,7 @@ public void saveSupplier(String supplierName) throws SQLException { ps.setString(1, supplierName); ps.execute(); } catch (SQLException ex) { - Logger.getLogger(SupplierRepository.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); throw ex; } } @@ -40,7 +41,7 @@ public void deleteSupplier(String supplierName) throws SQLException { ps.setString(1, supplierName); ps.execute(); } catch (SQLException ex) { - Logger.getLogger(SupplierRepository.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); throw ex; } } @@ -53,7 +54,7 @@ public void renameSupplier(String oldName, String newName) throws SQLException { ps.setString(2, oldName); ps.execute(); } catch (SQLException ex) { - Logger.getLogger(SupplierRepository.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); throw ex; } } @@ -68,7 +69,7 @@ public List getAllSuppliers() throws SQLException { } return supplierList; } catch (SQLException ex) { - Logger.getLogger(SupplierRepository.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); throw ex; } } diff --git a/src/main/java/openconsignment/repository/TransportRepository.java b/src/main/java/openconsignment/repository/TransportRepository.java index 752b1bf..b2679b1 100644 --- a/src/main/java/openconsignment/repository/TransportRepository.java +++ b/src/main/java/openconsignment/repository/TransportRepository.java @@ -6,14 +6,15 @@ import java.sql.SQLException; import java.util.ArrayList; import java.util.List; -import java.util.logging.Level; -import java.util.logging.Logger; import javax.inject.Inject; import openconsignment.common.SqlLoader; import openconsignment.common.SqlQuery; import openconsignment.model.TransportLedgerRow; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class TransportRepository { + private static final Logger logger = LoggerFactory.getLogger(TransportRepository.class); private final Connection conn; @@ -22,14 +23,14 @@ public TransportRepository(Connection conn) { this.conn = conn; } - public void saveTransport(String tranportName) throws SQLException { + public void saveTransport(String transportName) throws SQLException { try { String sql = SqlLoader.load(SqlQuery.TRANSPORT_SAVE); PreparedStatement ps = conn.prepareStatement(sql); - ps.setString(1, tranportName); + ps.setString(1, transportName); ps.execute(); } catch (SQLException ex) { - Logger.getLogger(TransportRepository.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); throw ex; } } @@ -41,7 +42,7 @@ public void deleteTransport(String transportName) throws SQLException { ps.setString(1, transportName); ps.execute(); } catch (SQLException ex) { - Logger.getLogger(TransportRepository.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); throw ex; } } @@ -54,7 +55,7 @@ public void renameTransport(String oldName, String newName) throws SQLException ps.setString(2, oldName); ps.execute(); } catch (SQLException ex) { - Logger.getLogger(TransportRepository.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); throw ex; } } @@ -69,7 +70,7 @@ public List getAllTransports() throws SQLException { } return transportList; } catch (SQLException ex) { - Logger.getLogger(TransportRepository.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); throw ex; } } @@ -104,7 +105,7 @@ public List getTransportReportFor(String transportName, Stri } return result; } catch (SQLException ex) { - Logger.getLogger(TransportRepository.class.getName()).log(Level.SEVERE, ex.toString(), ex); + logger.error(ex.toString(), ex); throw ex; } } diff --git a/src/main/resources/log4j2.xml b/src/main/resources/log4j2.xml new file mode 100644 index 0000000..e0109ec --- /dev/null +++ b/src/main/resources/log4j2.xml @@ -0,0 +1,42 @@ + + + + ${sys:openconsignment.logDir:-logs} + ${sys:openconsignment.logSize:-10 MB} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/java/openconsignment/common/LoggingIntegrationTest.java b/src/test/java/openconsignment/common/LoggingIntegrationTest.java new file mode 100644 index 0000000..7f37bd8 --- /dev/null +++ b/src/test/java/openconsignment/common/LoggingIntegrationTest.java @@ -0,0 +1,110 @@ +package openconsignment.common; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.attribute.FileTime; +import java.time.Instant; +import java.time.temporal.ChronoUnit; +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.zip.GZIPInputStream; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +class LoggingIntegrationTest { + @TempDir + Path tempDir; + + @Test + void flushesSlf4jMessagesAcrossCompressedRollovers() throws Exception { + runProbe(); + + Path logDir = tempDir.resolve("logs"); + List files; + try (var stream = Files.list(logDir)) { + files = stream.toList(); + } + assertTrue(files.stream().anyMatch(path -> path.toString().endsWith(".gz")), "Expected compressed archives"); + StringBuilder logs = new StringBuilder(); + for (Path file : files) { + if (file.toString().endsWith(".gz")) { + try (InputStream input = new GZIPInputStream(Files.newInputStream(file))) { + logs.append(new String(input.readAllBytes(), StandardCharsets.UTF_8)); + } + } else if (file.getFileName().toString().equals("openconsignment.log")) { + logs.append(Files.readString(file)); + } + } + String content = logs.toString(); + assertTrue(content.contains("slf4j-marker 42")); + assertTrue(content.contains("error-marker")); + assertTrue(content.contains("IllegalStateException: probe-exception")); + for (int i = 0; i < 80; i++) { + assertTrue(content.contains("rotation-marker-" + i + " "), "Missing message " + i); + } + } + + @Test + void rolloverDeletesOnlyExpiredMatchingArchives() throws Exception { + Path logDir = Files.createDirectory(tempDir.resolve("logs")); + Path expired = Files.writeString(logDir.resolve("openconsignment-2000-01-01-1.log.gz"), "old"); + Path unrelated = Files.writeString(logDir.resolve("other.log.gz"), "keep"); + FileTime old = FileTime.from(Instant.now().minus(31, ChronoUnit.DAYS)); + Files.setLastModifiedTime(expired, old); + Files.setLastModifiedTime(unrelated, old); + + runProbe(); + + assertTrue(Files.notExists(expired), "Expired archive should be removed"); + assertTrue(Files.exists(unrelated), "Unrelated files should be preserved"); + } + + @Test + void continuesLoggingToConsoleWhenFileCannotBeCreated() throws Exception { + Path notADirectory = Files.writeString(tempDir.resolve("not-a-directory"), "file"); + String output = runProbe(notADirectory.resolve("logs")); + assertTrue(output.contains("error-marker")); + assertTrue(output.contains("slf4j-marker 42")); + assertTrue(output.contains("ERROR"), "Expected a file initialization error on stderr"); + } + + private void runProbe() throws Exception { + runProbe(tempDir.resolve("logs")); + } + + private String runProbe(Path logDir) throws Exception { + String classpath = System.getProperty("logging.test.classpath"); + String jar = System.getProperty("logging.test.jar"); + if (jar != null) { + Path testClasses = Path.of(LoggingProbe.class + .getProtectionDomain() + .getCodeSource() + .getLocation() + .toURI()); + classpath = testClasses + System.getProperty("path.separator") + jar; + } + Path output = tempDir.resolve("process-output.txt"); + Process process = new ProcessBuilder( + Path.of(System.getProperty("java.home"), "bin", "java").toString(), + "-Dopenconsignment.logDir=" + logDir, + "-Dopenconsignment.logSize=1 KB", + "-cp", + classpath, + LoggingProbe.class.getName()) + .redirectErrorStream(true) + .redirectOutput(output.toFile()) + .start(); + boolean finished = process.waitFor(30, TimeUnit.SECONDS); + if (!finished) { + process.destroyForcibly(); + } + assertTrue(finished, "Logging process timed out"); + assertEquals(0, process.exitValue(), Files.readString(output)); + return Files.readString(output); + } +} diff --git a/src/test/java/openconsignment/common/LoggingProbe.java b/src/test/java/openconsignment/common/LoggingProbe.java new file mode 100644 index 0000000..1ae2307 --- /dev/null +++ b/src/test/java/openconsignment/common/LoggingProbe.java @@ -0,0 +1,16 @@ +package openconsignment.common; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** A separate JVM exercises the same startup, configuration and shutdown as the application. */ +public class LoggingProbe { + public static void main(String[] args) { + Logger logger = LoggerFactory.getLogger(LoggingProbe.class); + logger.info("slf4j-marker {}", 42); + logger.error("error-marker", new IllegalStateException("probe-exception")); + for (int i = 0; i < 80; i++) { + logger.info("rotation-marker-{} {}", i, "x".repeat(200)); + } + } +}