Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.dt180g.laboration_3;
- import java.io.IOException;
- import java.util.logging.*;
- import java.util.logging.FileHandler;
- import java.util.logging.Handler;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- * Singleton class which contains a logger for logging
- * information about the game.
- */
- public class HanoiLogger implements Constants {
- private static Logger logger;
- /**
- * Constructor made private.
- */
- private HanoiLogger() {}
- /**
- * Function to retrieve the logger.
- * Calls method to initialize the logger conforming to lazy initialization of a singleton class.
- * @return The logger instance.
- */
- public static Logger getLogger() {
- if(logger == null){
- initializeLogger();
- }
- return logger;
- }
- /**
- * Closes all the handlers of the logger.
- */
- public static void closeLogger() {
- for (Handler h : logger.getHandlers()) {
- h.close();
- }
- }
- /**
- * Initializer method for the logger.
- */
- private static void initializeLogger() {
- try {
- logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
- FileHandler handler = new FileHandler(LOG_FILE_PATH);
- SimpleFormatter formatter = new SimpleFormatter() {
- @Override
- public synchronized String format(LogRecord logRecord) {
- return String.format("%s%n", logRecord.getMessage());
- }
- };
- handler.setFormatter(formatter);
- logger.addHandler(handler);
- } catch (IOException ex) {
- logger.log(Level.SEVERE, ex.getMessage(), ex);
- } catch (SecurityException ex) {
- logger.log(Level.SEVERE, ex.getMessage(), ex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment