View Javadoc
1   /*
2    * Copyright (c) Patrick Magauran 2018.
3    *   Licensed under the AGPLv3. All conditions of said license apply.
4    *       This file is part of ABOS.
5    *
6    *       ABOS is free software: you can redistribute it and/or modify
7    *       it under the terms of the GNU Affero General Public License as published by
8    *       the Free Software Foundation, either version 3 of the License, or
9    *       (at your option) any later version.
10   *
11   *       ABOS is distributed in the hope that it will be useful,
12   *       but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   *       GNU Affero General Public License for more details.
15   *
16   *       You should have received a copy of the GNU Affero General Public License
17   *       along with ABOS.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package Utilities;//import javax.swing.*;
21  
22  import javafx.scene.control.Alert;
23  
24  import java.io.IOException;
25  import java.util.logging.FileHandler;
26  import java.util.logging.Level;
27  import java.util.logging.Logger;
28  import java.util.logging.SimpleFormatter;
29  
30  /**
31   * Utilities.LogToFile class
32   * This class is intended to be use with the default logging class of java
33   * It save the log in an XML file  and display a friendly message to the user
34   *
35   * @author Ibrabel <ibrabel@gmail.com>
36   */
37  public class LogToFile {
38  
39      private static final Logger logger = Logger.getLogger("MYLOG");
40      /* Severities
41        SEVERE
42        WARNING
43        INFO
44        CONFIG
45        FINE
46        FINER
47        FINEST
48       */
49  
50      /**
51       * log Method
52       * enable to log all exceptions to a file and display user message on demand
53       *
54       * @param ex    the exception
55       * @param level the severity level
56       *              SEVERE
57       *              WARNING
58       *              INFO
59       *              CONFIG
60       *              FINE
61       *              FINER
62       *              FINEST
63       * @param msg   the message to print
64       */
65      public static void log(Exception ex, Severity level, String msg) {
66  
67          FileHandler fh = null;
68          try {
69              // Create a file with append set to  true. Also limit the file to 10000 bytes
70              fh = new FileHandler("log.txt", 10000, 1, true);
71  
72              // Set the file format to a simple text file.
73              fh.setFormatter(new SimpleFormatter());
74              logger.addHandler(fh);
75  
76              switch (level) {
77                  case SEVERE:
78                      logger.log(Level.SEVERE, msg, ex);
79                      if (!msg.isEmpty()) {
80                          Alert alert = new Alert(Alert.AlertType.ERROR);
81                          alert.setTitle("Error");
82                          alert.setHeaderText("Error");
83                          alert.setContentText(msg);
84                          alert.setResizable(true);
85                          alert.showAndWait();
86  
87                      }
88                      break;
89                  case WARNING:
90                      logger.log(Level.WARNING, msg, ex);
91                      if (!msg.isEmpty()) {
92                          Alert alert = new Alert(Alert.AlertType.WARNING);
93                          alert.setTitle("Warning");
94                          alert.setHeaderText("Warning");
95                          alert.setContentText(msg);
96                          alert.setResizable(true);
97  
98                          alert.showAndWait();
99                      }
100                     break;
101                 case INFO:
102                     logger.log(Level.INFO, msg, ex);
103                     if (!msg.isEmpty()) {
104                         Alert alert = new Alert(Alert.AlertType.INFORMATION);
105                         alert.setTitle("Info");
106                         // alert.setHeaderText("");
107                         alert.setContentText(msg);
108                         alert.setResizable(true);
109 
110                         alert.showAndWait();
111                     }
112                     break;
113                 case CONFIG:
114                     logger.log(Level.CONFIG, msg, ex);
115                     break;
116                 case FINE:
117                     logger.log(Level.FINE, msg, ex);
118                     break;
119                 case FINER:
120                     logger.log(Level.FINER, msg, ex);
121                     break;
122                 case FINEST:
123                     logger.log(Level.FINEST, msg, ex);
124                     break;
125                 //default:
126                 //    logger.log(Level.CONFIG, msg, ex);
127                 //    break;
128             }
129         } catch (IOException | SecurityException ex1) {
130             logger.log(Level.SEVERE, null, ex1);
131         } finally {
132             if (fh != null) {
133                 fh.close();
134             }
135         }
136     }
137 }