From 1ff37de1a35dd7c2b9a06a2b12ce4b8f087cd444 Mon Sep 17 00:00:00 2001 From: Leonardo Brandenberger Date: Thu, 5 May 2022 23:03:48 +0200 Subject: [PATCH] Initial logging draft --- .../projekt2/turnierverwaltung/FileIO.java | 15 ++++---- .../turnierverwaltung/LogConfiguration.java | 34 ++++++++++++++++++ .../turnierverwaltung/logging/log.properties | 35 +++++++++++++++++++ 3 files changed, 78 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/LogConfiguration.java create mode 100644 app/src/main/resources/ch/zhaw/projekt2/turnierverwaltung/logging/log.properties diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FileIO.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FileIO.java index 05bc85e..8834c63 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FileIO.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FileIO.java @@ -6,9 +6,6 @@ import javafx.collections.ObservableList; import java.io.*; import java.net.URI; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; import java.util.logging.Logger; /** @@ -18,7 +15,7 @@ public class FileIO { private File mainDir; private File saves; - private static final Logger logger = Logger.getLogger(FileIO.class.getName()); + private static final Logger logger = Logger.getLogger(FileIO.class.getCanonicalName()); /** * Constructor initiates the Directory and creates a save folder if not already existing. @@ -26,6 +23,9 @@ public class FileIO { * @param saveLocation the directory in which the save Files will be saved */ public FileIO(String saveLocation) { + + System.out.println(FileIO.class.getCanonicalName()); + this.mainDir = new File(saveLocation); if (!mainDir.exists()) { logger.fine("Creating main directory in given path" + saveLocation); @@ -133,7 +133,7 @@ public class FileIO { newSave.createNewFile(); out = new ObjectOutputStream(new FileOutputStream(newSave)); out.writeObject(tournament); - System.out.println("Save File" + tournament.getName() + ".txt being saved to " + saves.getAbsolutePath()); + System.out.println("Save File " + tournament.getName() + ".txt being saved to " + saves.getAbsolutePath()); } catch (FileNotFoundException e) { logger.severe("Could not find tournament File"); @@ -173,7 +173,10 @@ public class FileIO { throw new FileNotFoundException("File deletion unsuccessful"); } } - + + /** + * TournamentFile Class is in use to add missing functionality that is + */ public class TournamentFile extends File{ public TournamentFile(URI uri) { diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/LogConfiguration.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/LogConfiguration.java new file mode 100644 index 0000000..7bdec52 --- /dev/null +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/LogConfiguration.java @@ -0,0 +1,34 @@ +package ch.zhaw.projekt2.turnierverwaltung; + +import java.io.IOException; +import java.io.InputStream; +import java.util.logging.*; +import java.time.LocalDateTime; + + +public class LogConfiguration { + private static final Logger logger = Logger.getLogger(LogConfiguration.class.getCanonicalName()); + + public LogConfiguration() throws IOException { + String propertiesPath = "ch" + System.getProperty("file.separator") + "zhaw" + System.getProperty("file.separator") + "projekt2" + System.getProperty("file.separator") + + "turnierverwaltung" + System.getProperty("file.separator") + "logging" + System.getProperty("file.separator") + "log.properties"; + + InputStream logConfig = this.getClass().getClassLoader().getResourceAsStream("ch/zhaw/projekt2/turnierverwaltung/logging/log.properties"); + LogManager.getLogManager().readConfiguration(logConfig); + + + Logger.getLogger(LogConfiguration.class.getPackageName()); + + //Logger mainlogger = Logger.getLogger(LogConfiguration.class.getPackageName()); + //Logger mainlogger = Logger.getLogger("main"); + + // programmatically add a file handler to the current logger + //FileHandler fileHandler = new FileHandler(System.getProperty("user.dir") + System.getProperty("file.separator") + "tournierverwaltung_angrynerds" + System.getProperty("file.separator") + "turnierverwaltung.log", true); + //fileHandler.setFormatter(new SimpleFormatter()); // use a default SimpleFormatter + //fileHandler.setLevel(Level.FINER); // only handle messages level >= FINER + //logger.addHandler(fileHandler); // add the handler to the specific logger + //logger.setUseParentHandlers(false); // if set to true (default) logging could happen twice + //mainlogger.addHandler(fileHandler); + //logger.fine("started logger"); + } +} diff --git a/app/src/main/resources/ch/zhaw/projekt2/turnierverwaltung/logging/log.properties b/app/src/main/resources/ch/zhaw/projekt2/turnierverwaltung/logging/log.properties new file mode 100644 index 0000000..b6420ad --- /dev/null +++ b/app/src/main/resources/ch/zhaw/projekt2/turnierverwaltung/logging/log.properties @@ -0,0 +1,35 @@ +## configure handlers +java.util.logging.ConsoleHandler.level = ALL + +## File handler configuration +## see https://docs.oracle.com/en/java/javase/11/docs/api/java.logging/java/util/logging/FileHandler.html +java.util.logging.FileHandler.level = ALL +# %g = generation number, %u = unique number to resolve conflicts +java.util.logging.FileHandler.pattern = log-%g-%u.log +# use SimpleFormatter instead of default XMLFormatter +java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter +java.util.logging.FileHandler.encoding = UTF-8 +# max log file size in byte before switching to next generation (=1kB); 0=unlimited +java.util.logging.FileHandler.limit = 1024 +# max number of generations (%g) before overwriting (5 -> 0..4) +java.util.logging.FileHandler.count=10 +java.util.logging.FileHandler.append=true + +## configure Formatter +java.util.logging.SimpleFormatter.format = [%1$tc] %4$s: %5$s {%2$s}%6$s%n + +## configure default log level (for all loggers, if not overwritten below) +.level = INFO + +## configure root logger "" +handlers = java.util.logging.ConsoleHandler, java.util.logging.FileHandler +level = FINE + +## Application specific logger configuration +# loggers starting with "ch.zhaw.prog2.io" -> write to console and file and do not forward to parent handlers +#ch.zhaw.prog2.turnierverwaltung.handlers = java.util.logging.FileHandler, java.util.logging.ConsoleHandler +#ch.zhaw.prog2.turnierverwaltung.level = FINE +#ch.zhaw.prog2.turnierverwaltung.useParentHandlers = false + +# logger level for individual classes +ch.zhaw.projekt2.turnierverwaltung.FileIO.level = FINEST