-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathLog.java
More file actions
225 lines (180 loc) · 6.75 KB
/
Log.java
File metadata and controls
225 lines (180 loc) · 6.75 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package com.esotericsoftware.minlog;
import java.io.PrintWriter;
import java.io.StringWriter;
/** A low overhead, lightweight logging system.
* @author Nathan Sweet <[email protected]> */
public class Log {
/** No logging at all. */
static public final int LEVEL_NONE = 6;
/** Critical errors. The application may no longer work correctly. */
static public final int LEVEL_ERROR = 5;
/** Important warnings. The application will continue to work correctly. */
static public final int LEVEL_WARN = 4;
/** Informative messages. Typically used for deployment. */
static public final int LEVEL_INFO = 3;
/** Debug messages. This level is useful during development. */
static public final int LEVEL_DEBUG = 2;
/** Trace messages. A lot of information is logged, so this level is usually only needed when debugging a problem. */
static public final int LEVEL_TRACE = 1;
/** The level of messages that will be logged. Compiling this and the booleans below as "final" will cause the compiler to
* remove all "if (Log.info) ..." type statements below the set level. */
static private int level = LEVEL_INFO;
/** True when the ERROR level will be logged. */
static public boolean ERROR = level <= LEVEL_ERROR;
/** True when the WARN level will be logged. */
static public boolean WARN = level <= LEVEL_WARN;
/** True when the INFO level will be logged. */
static public boolean INFO = level <= LEVEL_INFO;
/** True when the DEBUG level will be logged. */
static public boolean DEBUG = level <= LEVEL_DEBUG;
/** True when the TRACE level will be logged. */
static public boolean TRACE = level <= LEVEL_TRACE;
static public int getLevel () {
return level;
}
/** Sets the level to log. If a version of this class is being used that has a final log level, this has no affect. */
static public void set (int level) {
// Comment out method contents when compiling fixed level JARs.
Log.level = level;
ERROR = level <= LEVEL_ERROR;
WARN = level <= LEVEL_WARN;
INFO = level <= LEVEL_INFO;
DEBUG = level <= LEVEL_DEBUG;
TRACE = level <= LEVEL_TRACE;
}
static public void NONE () {
set(LEVEL_NONE);
}
static public void ERROR () {
set(LEVEL_ERROR);
}
static public void WARN () {
set(LEVEL_WARN);
}
static public void INFO () {
set(LEVEL_INFO);
}
static public void DEBUG () {
set(LEVEL_DEBUG);
}
static public void TRACE () {
set(LEVEL_TRACE);
}
/** Sets the logger that will write the log messages. */
static public void setLogger (Logger logger) {
Log.logger = logger;
}
static private Logger logger = new Logger();
static public void error (String message, Throwable ex) {
if (ERROR) logger.log(LEVEL_ERROR, null, message, ex);
}
static public void error (String category, String message, Throwable ex) {
if (ERROR) logger.log(LEVEL_ERROR, category, message, ex);
}
static public void error (String message) {
if (ERROR) logger.log(LEVEL_ERROR, null, message, null);
}
static public void error (String category, String message) {
if (ERROR) logger.log(LEVEL_ERROR, category, message, null);
}
static public void warn (String message, Throwable ex) {
if (WARN) logger.log(LEVEL_WARN, null, message, ex);
}
static public void warn (String category, String message, Throwable ex) {
if (WARN) logger.log(LEVEL_WARN, category, message, ex);
}
static public void warn (String message) {
if (WARN) logger.log(LEVEL_WARN, null, message, null);
}
static public void warn (String category, String message) {
if (WARN) logger.log(LEVEL_WARN, category, message, null);
}
static public void info (String message, Throwable ex) {
if (INFO) logger.log(LEVEL_INFO, null, message, ex);
}
static public void info (String category, String message, Throwable ex) {
if (INFO) logger.log(LEVEL_INFO, category, message, ex);
}
static public void info (String message) {
if (INFO) logger.log(LEVEL_INFO, null, message, null);
}
static public void info (String category, String message) {
if (INFO) logger.log(LEVEL_INFO, category, message, null);
}
static public void debug (String message, Throwable ex) {
if (DEBUG) logger.log(LEVEL_DEBUG, null, message, ex);
}
static public void debug (String category, String message, Throwable ex) {
if (DEBUG) logger.log(LEVEL_DEBUG, category, message, ex);
}
static public void debug (String message) {
if (DEBUG) logger.log(LEVEL_DEBUG, null, message, null);
}
static public void debug (String category, String message) {
if (DEBUG) logger.log(LEVEL_DEBUG, category, message, null);
}
static public void trace (String message, Throwable ex) {
if (TRACE) logger.log(LEVEL_TRACE, null, message, ex);
}
static public void trace (String category, String message, Throwable ex) {
if (TRACE) logger.log(LEVEL_TRACE, category, message, ex);
}
static public void trace (String message) {
if (TRACE) logger.log(LEVEL_TRACE, null, message, null);
}
static public void trace (String category, String message) {
if (TRACE) logger.log(LEVEL_TRACE, category, message, null);
}
private Log () {
}
/** Performs the actual logging. Default implementation logs to System.out. Extended and use {@link Log#logger} set to handle
* logging differently. */
static public class Logger {
private final long firstLogTime = System.currentTimeMillis();
public void log (int level, String category, String message, Throwable ex) {
StringBuilder builder = new StringBuilder(256);
long time = System.currentTimeMillis() - firstLogTime;
long minutes = time / (1000 * 60);
long seconds = time / (1000) % 60;
if (minutes <= 9) builder.append('0');
builder.append(minutes);
builder.append(':');
if (seconds <= 9) builder.append('0');
builder.append(seconds);
switch (level) {
case LEVEL_ERROR:
builder.append(" ERROR: ");
break;
case LEVEL_WARN:
builder.append(" WARN: ");
break;
case LEVEL_INFO:
builder.append(" INFO: ");
break;
case LEVEL_DEBUG:
builder.append(" DEBUG: ");
break;
case LEVEL_TRACE:
builder.append(" TRACE: ");
break;
}
if (category != null) {
builder.append('[');
builder.append(category);
builder.append("] ");
}
builder.append(message);
if (ex != null) {
StringWriter writer = new StringWriter(256);
ex.printStackTrace(new PrintWriter(writer));
builder.append('\n');
builder.append(writer.toString().trim());
}
print(builder.toString());
}
/** Prints the message to System.out. Called by the default implementation of {@link #log(int, String, String, Throwable)}. */
protected void print (String message) {
System.out.println(message);
}
}
}