-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleLogger.java
119 lines (94 loc) · 3.01 KB
/
SimpleLogger.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package fishtracking;
import java.io.PrintStream;
/**
* When all logging for an application will be written to one file,
* SimpleLogger provides an easier interface for logging than maintaining logs through LogManager.
*
* @author esocolofsky
*/
public class SimpleLogger {
public static final int DEBUG = 0;
public static final int INFO = 1;
public static final int WARN = 2;
public static final int ERROR = 3;
public static final int FATAL = 4;
public static final String[] LEVEL_NAMES = new String[] { "DEBUG", "INFO", "WARN", "ERROR", "FATAL" };
private static int logLevel = DEBUG;
private static LogWriter logWriter = null;
private static boolean appendDateStamp;
private static boolean localEcho = false;
public static void init (String logFolder, String logName, boolean appendDateStamp) {
LogManager.setLogFolder(logFolder);
logWriter = LogManager.addLogWriter(logName, logName +".txt", true);
// append date and log level stamps locally
logWriter.setTimestampDisplay(false);
SimpleLogger.appendDateStamp = appendDateStamp;
}
public static int logLevel () {
return logLevel;
}
public static void logLevel (int val) {
if (val < DEBUG || val > FATAL) { return; }
logLevel = val;
}
public static boolean localEcho () {
return localEcho;
}
public static void localEcho (boolean val) {
localEcho = val;
}
public static void log (String message, int level) {
if (logWriter == null) {
System.err.println("Initialize SimpleLogger with init() before logging.");
}
if (level < 0 || level > LEVEL_NAMES.length-1) {
System.err.println("Invalid value for log level; must be between 0 and "+ (LEVEL_NAMES.length-1) +", inclusive.");
}
if (level >= logLevel) {
// append date and log level stamps
String stamp = "";
if (appendDateStamp) {
stamp = "[ "+ LEVEL_NAMES[level] +" "+ LogWriter.getTimeStamp(" ") +" ]\t";
}
logWriter.writeToLog(stamp + message);
if (localEcho) {
PrintStream console = (level >= ERROR) ? System.err : System.out;
console.println(stamp + message);
}
}
}
public static void log (String message) {
log(message, 1);
}
public static void debug (String message) {
log(message, DEBUG);
}
public static void info (String message) {
log(message, INFO);
}
public static void warn (String message) {
log(message, WARN);
}
public static void error (String message) {
log(message, ERROR);
}
public static void fatal (String message) {
log(message, FATAL);
}
public static void writeExceptionToLog (Exception e) {
writeExceptionToLog(e, 5, "");
}
public static void writeExceptionToLog (Exception e, int depth) {
writeExceptionToLog(e, depth, "");
}
public static void writeExceptionToLog (Exception e, int depth, String header) {
if (header == null) {
header = "";
}
header = "[ "+ LEVEL_NAMES[ERROR] +" "+ LogWriter.getTimeStamp(" ") +" ]\t"+ header;
logWriter.writeExceptionToLog(e, depth, header);
}
public static void writeEmptyLineToLog () {
logWriter.writeToLog("");
}
}