finished logging and improved logging in FileIO #17
This commit is contained in:
		
							parent
							
								
									1ff37de1a3
								
							
						
					
					
						commit
						035b096889
					
				| 
						 | 
					@ -6,8 +6,18 @@ package ch.zhaw.projekt2.turnierverwaltung;
 | 
				
			||||||
import ch.zhaw.projekt2.turnierverwaltung.main.MainWindow;
 | 
					import ch.zhaw.projekt2.turnierverwaltung.main.MainWindow;
 | 
				
			||||||
import javafx.application.Application;
 | 
					import javafx.application.Application;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.io.IOException;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class App {
 | 
					public class App {
 | 
				
			||||||
    public static void main(String[] args) {
 | 
					    public static void main(String[] args) {
 | 
				
			||||||
 | 
					        try {
 | 
				
			||||||
 | 
					            new LogConfiguration(System.getProperty("user.dir") + System.getProperty("file.separator") + "tournierverwaltung_angrynerds",
 | 
				
			||||||
 | 
					                    "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");
 | 
				
			||||||
 | 
					        } catch (IOException e) {
 | 
				
			||||||
 | 
					            throw new RuntimeException(e);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        Application.launch(MainWindow.class,args);
 | 
					        Application.launch(MainWindow.class,args);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -23,9 +23,6 @@ public class FileIO {
 | 
				
			||||||
     * @param saveLocation the directory in which the save Files will be saved
 | 
					     * @param saveLocation the directory in which the save Files will be saved
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public FileIO(String saveLocation) {
 | 
					    public FileIO(String saveLocation) {
 | 
				
			||||||
 | 
					 | 
				
			||||||
        System.out.println(FileIO.class.getCanonicalName());
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        this.mainDir = new File(saveLocation);
 | 
					        this.mainDir = new File(saveLocation);
 | 
				
			||||||
        if (!mainDir.exists()) {
 | 
					        if (!mainDir.exists()) {
 | 
				
			||||||
            logger.fine("Creating main directory in given path" + saveLocation);
 | 
					            logger.fine("Creating main directory in given path" + saveLocation);
 | 
				
			||||||
| 
						 | 
					@ -43,6 +40,7 @@ public class FileIO {
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Returns a list with all save Files that are located inside the save folder.
 | 
					     * Returns a list with all save Files that are located inside the save folder.
 | 
				
			||||||
     *
 | 
					     *
 | 
				
			||||||
| 
						 | 
					@ -51,18 +49,26 @@ public class FileIO {
 | 
				
			||||||
    public ObservableList<TournamentFile> getList() {
 | 
					    public ObservableList<TournamentFile> getList() {
 | 
				
			||||||
        logger.fine("Creating a List out of all Files in the save directory and returning it");
 | 
					        logger.fine("Creating a List out of all Files in the save directory and returning it");
 | 
				
			||||||
        ObservableList<TournamentFile> tournamentFiles = FXCollections.observableArrayList();
 | 
					        ObservableList<TournamentFile> tournamentFiles = FXCollections.observableArrayList();
 | 
				
			||||||
        for(File tournament : saves.listFiles()){
 | 
					        for (File tournament : saves.listFiles()) {
 | 
				
			||||||
            tournamentFiles.add(new TournamentFile(tournament.toURI()));
 | 
					            tournamentFiles.add(new TournamentFile(tournament.toURI()));
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        return tournamentFiles;
 | 
					        return tournamentFiles;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public boolean tournamentExists(String name){
 | 
					    /**
 | 
				
			||||||
        for(TournamentFile file : getList()) {
 | 
					     * Method to check if a tournament with the existing name already exists.
 | 
				
			||||||
            if(file.toString().toLowerCase().equals(name.toLowerCase())){
 | 
					     * @param name that is being checked
 | 
				
			||||||
 | 
					     * @return true if the name exists already false if the name is unique
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public boolean tournamentExists(String name) {
 | 
				
			||||||
 | 
					        logger.finer("checking for duplicate name in tournament List");
 | 
				
			||||||
 | 
					        for (TournamentFile file : getList()) {
 | 
				
			||||||
 | 
					            if (file.toString().toLowerCase().equals(name.toLowerCase())) {
 | 
				
			||||||
 | 
					                logger.fine(name + " is an already existing name in the list");
 | 
				
			||||||
                return true;
 | 
					                return true;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					        logger.fine(name + " is an unique name in the list");
 | 
				
			||||||
        return false;
 | 
					        return false;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -90,13 +96,13 @@ public class FileIO {
 | 
				
			||||||
            logger.finer("Starting to read tournament File");
 | 
					            logger.finer("Starting to read tournament File");
 | 
				
			||||||
            tournament = (Tournament) in.readObject();
 | 
					            tournament = (Tournament) in.readObject();
 | 
				
			||||||
        } catch (FileNotFoundException e) {
 | 
					        } catch (FileNotFoundException e) {
 | 
				
			||||||
            logger.severe("Could not find tournament File");
 | 
					            logger.severe("Could not find tournament File" + e);
 | 
				
			||||||
            throw e;
 | 
					            throw e;
 | 
				
			||||||
        } catch (IOException e) {
 | 
					        } catch (IOException e) {
 | 
				
			||||||
            logger.severe("Failed to read File" + tournamentFile.getName());
 | 
					            logger.severe("Failed to read File" + tournamentFile.getName());
 | 
				
			||||||
            throw new IOException("Error while reading File", e);
 | 
					            throw new IOException("Error while reading File", e);
 | 
				
			||||||
        } catch (ClassNotFoundException e) {
 | 
					        } catch (ClassNotFoundException e) {
 | 
				
			||||||
            logger.severe("No definition for the class with the specified name could be found");
 | 
					            logger.severe("No definition for the class with the specified name could be found" + e);
 | 
				
			||||||
            throw new ClassNotFoundException("No definition for the class with the specified name could be found", e);
 | 
					            throw new ClassNotFoundException("No definition for the class with the specified name could be found", e);
 | 
				
			||||||
        } finally {
 | 
					        } finally {
 | 
				
			||||||
            if (in != null) {
 | 
					            if (in != null) {
 | 
				
			||||||
| 
						 | 
					@ -104,7 +110,7 @@ public class FileIO {
 | 
				
			||||||
                    logger.finer("Trying to close input stream");
 | 
					                    logger.finer("Trying to close input stream");
 | 
				
			||||||
                    in.close();
 | 
					                    in.close();
 | 
				
			||||||
                } catch (IOException e) {
 | 
					                } catch (IOException e) {
 | 
				
			||||||
                    logger.severe("Failed to close input stream");
 | 
					                    logger.severe("Failed to close input stream" + e);
 | 
				
			||||||
                    throw new IOException("Error while closing input stream", e);
 | 
					                    throw new IOException("Error while closing input stream", e);
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
| 
						 | 
					@ -136,17 +142,17 @@ public class FileIO {
 | 
				
			||||||
            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) {
 | 
					        } catch (FileNotFoundException e) {
 | 
				
			||||||
            logger.severe("Could not find tournament File");
 | 
					            logger.severe("Could not find tournament File" + e);
 | 
				
			||||||
            throw e;
 | 
					            throw e;
 | 
				
			||||||
        } catch (IOException e) {
 | 
					        } catch (IOException e) {
 | 
				
			||||||
            logger.severe("Failed to write File" + tournament.getName());
 | 
					            logger.severe("Failed to write File " + tournament.getName() + e);
 | 
				
			||||||
            throw new IOException("Error while writing File", e);
 | 
					            throw new IOException("Error while writing File", e);
 | 
				
			||||||
        } finally {
 | 
					        } finally {
 | 
				
			||||||
            if (out != null) {
 | 
					            if (out != null) {
 | 
				
			||||||
                try {
 | 
					                try {
 | 
				
			||||||
                    out.close();
 | 
					                    out.close();
 | 
				
			||||||
                } catch (IOException e) {
 | 
					                } catch (IOException e) {
 | 
				
			||||||
                    logger.severe("Failed to close output stream");
 | 
					                    logger.severe("Failed to close output stream" + e);
 | 
				
			||||||
                    throw new IOException("Error while closing output stream", e);
 | 
					                    throw new IOException("Error while closing output stream", e);
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
| 
						 | 
					@ -177,13 +183,24 @@ public class FileIO {
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * TournamentFile Class is in use to add missing functionality that is
 | 
					     * TournamentFile Class is in use to add missing functionality that is
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public class TournamentFile extends File{
 | 
					    public class TournamentFile extends File {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /**
 | 
				
			||||||
 | 
					         * Only job the constructor got is to initialize it via its superclass. See java.io.File Documentation for more info.
 | 
				
			||||||
 | 
					         *
 | 
				
			||||||
 | 
					         * @param uri abstract pathname needed for its superclass to intialize the file accordingly.
 | 
				
			||||||
 | 
					         */
 | 
				
			||||||
        public TournamentFile(URI uri) {
 | 
					        public TournamentFile(URI uri) {
 | 
				
			||||||
            super(uri);
 | 
					            super(uri);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public String toString(){
 | 
					        /**
 | 
				
			||||||
 | 
					         * Method overrides toString to return the names of the tournaments without having .txt endings.
 | 
				
			||||||
 | 
					         *
 | 
				
			||||||
 | 
					         * @return String without a txt ending
 | 
				
			||||||
 | 
					         */
 | 
				
			||||||
 | 
					        @Override
 | 
				
			||||||
 | 
					        public String toString() {
 | 
				
			||||||
            return getName().split("\\.")[0];
 | 
					            return getName().split("\\.")[0];
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,34 +1,41 @@
 | 
				
			||||||
package ch.zhaw.projekt2.turnierverwaltung;
 | 
					package ch.zhaw.projekt2.turnierverwaltung;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.io.File;
 | 
				
			||||||
import java.io.IOException;
 | 
					import java.io.IOException;
 | 
				
			||||||
import java.io.InputStream;
 | 
					import java.io.InputStream;
 | 
				
			||||||
import java.util.logging.*;
 | 
					import java.util.logging.*;
 | 
				
			||||||
import java.time.LocalDateTime;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class LogConfiguration {
 | 
					public class LogConfiguration {
 | 
				
			||||||
    private static final Logger logger = Logger.getLogger(LogConfiguration.class.getCanonicalName());
 | 
					    private static final Logger logger = Logger.getLogger(LogConfiguration.class.getCanonicalName());
 | 
				
			||||||
 | 
					    private final File mainDir;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public LogConfiguration(String saveLocation, String logFileLocation) throws IOException {
 | 
				
			||||||
 | 
					        logger.fine("Starts setting up a main directory in which a folder with the log files will be placed, if not already exists");
 | 
				
			||||||
 | 
					        this.mainDir = new File(saveLocation);
 | 
				
			||||||
 | 
					        if (!mainDir.exists()) {
 | 
				
			||||||
 | 
					            logger.fine("Creating main directory for log ordner in given path" + saveLocation);
 | 
				
			||||||
 | 
					            mainDir.mkdir();
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					            logger.finer("main directory for log folder already exists");
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        File saves = new File(mainDir, "log_files");
 | 
				
			||||||
 | 
					        if (!saves.exists()) {
 | 
				
			||||||
 | 
					            saves.mkdir();
 | 
				
			||||||
 | 
					            logger.fine("Creating log save directory");
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					            logger.finer("log save directory already exists");
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public LogConfiguration() throws IOException {
 | 
					 | 
				
			||||||
        String propertiesPath = "ch" + System.getProperty("file.separator") + "zhaw" + System.getProperty("file.separator") + "projekt2" + System.getProperty("file.separator") +
 | 
					        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";
 | 
					                "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");
 | 
					        logger.fine("Getting and reading logconfig file from " + propertiesPath);
 | 
				
			||||||
 | 
					        InputStream logConfig = this.getClass().getClassLoader().getResourceAsStream(propertiesPath);
 | 
				
			||||||
        LogManager.getLogManager().readConfiguration(logConfig);
 | 
					        LogManager.getLogManager().readConfiguration(logConfig);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        Logger.getLogger(LogConfiguration.class.getPackageName());
 | 
					        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");
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -15,7 +15,7 @@ import java.awt.*;
 | 
				
			||||||
import java.io.IOException;
 | 
					import java.io.IOException;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class MainWindow extends Application {
 | 
					public class MainWindow extends Application {
 | 
				
			||||||
    private FileIO fileIO = new FileIO(System.getProperty("user.dir") + "/tournierverwaltung_angrynerds");
 | 
					    private FileIO fileIO = new FileIO(System.getProperty("user.dir") + System.getProperty("file.separator") + "tournierverwaltung_angrynerds");
 | 
				
			||||||
    private TournamentDecorator tournamentDecorator = new TournamentDecorator(fileIO);
 | 
					    private TournamentDecorator tournamentDecorator = new TournamentDecorator(fileIO);
 | 
				
			||||||
    private Factory factory = new Factory(fileIO, tournamentDecorator); //TODO make it private!
 | 
					    private Factory factory = new Factory(fileIO, tournamentDecorator); //TODO make it private!
 | 
				
			||||||
    private FactoryDecorator factoryDecorator;
 | 
					    private FactoryDecorator factoryDecorator;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,34 +1,32 @@
 | 
				
			||||||
## configure handlers
 | 
					## configures handlers
 | 
				
			||||||
java.util.logging.ConsoleHandler.level = ALL
 | 
					java.util.logging.ConsoleHandler.level = ALL
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## File handler configuration
 | 
					## File handler configuration
 | 
				
			||||||
## see https://docs.oracle.com/en/java/javase/11/docs/api/java.logging/java/util/logging/FileHandler.html
 | 
					## see https://docs.oracle.com/en/java/javase/11/docs/api/java.logging/java/util/logging/FileHandler.html
 | 
				
			||||||
java.util.logging.FileHandler.level = ALL
 | 
					java.util.logging.FileHandler.level = ALL
 | 
				
			||||||
# %g = generation number, %u = unique number to resolve conflicts
 | 
					# %g = generation number, %u = unique number to resolve conflicts
 | 
				
			||||||
java.util.logging.FileHandler.pattern = log-%g-%u.log
 | 
					java.util.logging.FileHandler.pattern = tournierverwaltung_angrynerds/log_files/log-%g-%u.log
 | 
				
			||||||
# use SimpleFormatter instead of default XMLFormatter
 | 
					# use SimpleFormatter instead of default XMLFormatter
 | 
				
			||||||
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
 | 
					java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
 | 
				
			||||||
java.util.logging.FileHandler.encoding = UTF-8
 | 
					java.util.logging.FileHandler.encoding = UTF-8
 | 
				
			||||||
# max log file size in byte before switching to next generation (=1kB); 0=unlimited
 | 
					# max log file size in byte before switching to next generation (=1kB); 0=unlimited
 | 
				
			||||||
java.util.logging.FileHandler.limit = 1024
 | 
					java.util.logging.FileHandler.limit = 2048
 | 
				
			||||||
# max number of generations (%g) before overwriting (5 -> 0..4)
 | 
					# max number of generations (%g) before overwriting (5 -> 0..4)
 | 
				
			||||||
java.util.logging.FileHandler.count=10
 | 
					java.util.logging.FileHandler.count=10
 | 
				
			||||||
java.util.logging.FileHandler.append=true
 | 
					java.util.logging.FileHandler.append=true
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## configure Formatter
 | 
					## configures Formatter
 | 
				
			||||||
java.util.logging.SimpleFormatter.format = [%1$tc] %4$s: %5$s {%2$s}%6$s%n
 | 
					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)
 | 
					## configures default log level (for all loggers, if not overwritten below)
 | 
				
			||||||
.level = INFO
 | 
					.level = INFO
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## configure root logger ""
 | 
					## configure root logger ""
 | 
				
			||||||
handlers = java.util.logging.ConsoleHandler, java.util.logging.FileHandler
 | 
					handlers = java.util.logging.ConsoleHandler, java.util.logging.FileHandler
 | 
				
			||||||
level = FINE
 | 
					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
 | 
					#ch.zhaw.prog2.turnierverwaltung.useParentHandlers = false
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# logger level for individual classes
 | 
					# logger level for individual classes
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue