fabbe680

Untitled

Jun 8th, 2021
479
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. package com.dt180g.laboration_3;
  2.  
  3. import java.io.IOException;
  4. import java.util.logging.*;
  5. import java.util.logging.FileHandler;
  6. import java.util.logging.Handler;
  7. import java.util.logging.Level;
  8. import java.util.logging.Logger;
  9.  
  10. /**
  11. * Singleton class which contains a logger for logging
  12. * information about the game.
  13. */
  14.  
  15. public class HanoiLogger implements Constants {
  16. private static Logger logger;
  17.  
  18. /**
  19. * Constructor made private.
  20. */
  21. private HanoiLogger() {}
  22.  
  23. /**
  24. * Function to retrieve the logger.
  25. * Calls method to initialize the logger conforming to lazy initialization of a singleton class.
  26. * @return The logger instance.
  27. */
  28. public static Logger getLogger() {
  29. if(logger == null){
  30. initializeLogger();
  31. }
  32. return logger;
  33. }
  34.  
  35. /**
  36. * Closes all the handlers of the logger.
  37. */
  38. public static void closeLogger() {
  39. for (Handler h : logger.getHandlers()) {
  40. h.close();
  41. }
  42. }
  43.  
  44. /**
  45. * Initializer method for the logger.
  46. */
  47. private static void initializeLogger() {
  48. try {
  49. logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
  50. FileHandler handler = new FileHandler(LOG_FILE_PATH);
  51. SimpleFormatter formatter = new SimpleFormatter() {
  52. @Override
  53. public synchronized String format(LogRecord logRecord) {
  54. return String.format("%s%n", logRecord.getMessage());
  55. }
  56. };
  57. handler.setFormatter(formatter);
  58. logger.addHandler(handler);
  59. } catch (IOException ex) {
  60. logger.log(Level.SEVERE, ex.getMessage(), ex);
  61. } catch (SecurityException ex) {
  62. logger.log(Level.SEVERE, ex.getMessage(), ex);
  63. }
  64. }
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment