1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package Utilities;
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
32
33
34
35
36
37 public class LogToFile {
38
39 private static final Logger logger = Logger.getLogger("MYLOG");
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 public static void log(Exception ex, Severity level, String msg) {
66
67 FileHandler fh = null;
68 try {
69
70 fh = new FileHandler("log.txt", 10000, 1, true);
71
72
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
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
126
127
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 }