1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.OutputStream;
|
---|
7 | import java.io.PrintWriter;
|
---|
8 | import java.io.StringWriter;
|
---|
9 | import java.text.MessageFormat;
|
---|
10 | import java.util.ArrayList;
|
---|
11 | import java.util.Arrays;
|
---|
12 | import java.util.List;
|
---|
13 | import java.util.function.Supplier;
|
---|
14 | import java.util.logging.ConsoleHandler;
|
---|
15 | import java.util.logging.Handler;
|
---|
16 | import java.util.logging.Level;
|
---|
17 | import java.util.logging.LogRecord;
|
---|
18 | import java.util.logging.Logger;
|
---|
19 |
|
---|
20 | import org.openstreetmap.josm.tools.bugreport.BugReport;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * This class contains utility methods to log errors and warnings.
|
---|
24 | * <p>
|
---|
25 | * There are multiple log levels supported.
|
---|
26 | * @author Michael Zangl
|
---|
27 | * @since 10899
|
---|
28 | */
|
---|
29 | public final class Logging {
|
---|
30 | /**
|
---|
31 | * The josm internal log level indicating a severe error in the application that usually leads to a crash.
|
---|
32 | */
|
---|
33 | public static final Level LEVEL_ERROR = Level.SEVERE;
|
---|
34 | /**
|
---|
35 | * The josm internal log level to use when something that may lead to a crash or wrong behaviour has happened.
|
---|
36 | */
|
---|
37 | public static final Level LEVEL_WARN = Level.WARNING;
|
---|
38 | /**
|
---|
39 | * The josm internal log level to use for important events that will be useful when debugging problems
|
---|
40 | */
|
---|
41 | public static final Level LEVEL_INFO = Level.INFO;
|
---|
42 | /**
|
---|
43 | * The josm internal log level to print debug output
|
---|
44 | */
|
---|
45 | public static final Level LEVEL_DEBUG = Level.FINE;
|
---|
46 | /**
|
---|
47 | * The finest log level josm supports. This lets josm print a lot of debug output.
|
---|
48 | */
|
---|
49 | public static final Level LEVEL_TRACE = Level.FINEST;
|
---|
50 | private static final Logger LOGGER = Logger.getAnonymousLogger();
|
---|
51 | private static final RememberWarningHandler WARNINGS = new RememberWarningHandler();
|
---|
52 |
|
---|
53 | static {
|
---|
54 | LOGGER.setLevel(Level.ALL);
|
---|
55 | LOGGER.setUseParentHandlers(false);
|
---|
56 |
|
---|
57 | // for a more concise logging output via java.util.logging.SimpleFormatter
|
---|
58 | Utils.updateSystemProperty("java.util.logging.SimpleFormatter.format", "%1$tF %1$tT.%1$tL %4$s: %5$s%6$s%n");
|
---|
59 |
|
---|
60 | ConsoleHandler stderr = new ConsoleHandler();
|
---|
61 | LOGGER.addHandler(stderr);
|
---|
62 | stderr.setLevel(LEVEL_WARN);
|
---|
63 |
|
---|
64 | ConsoleHandler stdout = new ConsoleHandler() {
|
---|
65 | @Override
|
---|
66 | protected synchronized void setOutputStream(OutputStream out) {
|
---|
67 | // overwrite output stream.
|
---|
68 | super.setOutputStream(System.out);
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Override
|
---|
72 | public synchronized void publish(LogRecord record) {
|
---|
73 | if (!stderr.isLoggable(record)) {
|
---|
74 | super.publish(record);
|
---|
75 | }
|
---|
76 | }
|
---|
77 | };
|
---|
78 | LOGGER.addHandler(stdout);
|
---|
79 | stdout.setLevel(Level.ALL);
|
---|
80 |
|
---|
81 | LOGGER.addHandler(WARNINGS);
|
---|
82 | }
|
---|
83 |
|
---|
84 | private Logging() {
|
---|
85 | // hide
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Set the global log level.
|
---|
90 | * @param level The log level to use
|
---|
91 | */
|
---|
92 | public static void setLogLevel(Level level) {
|
---|
93 | LOGGER.setLevel(level);
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Prints an error message if logging is on.
|
---|
98 | * @param message The message to print.
|
---|
99 | */
|
---|
100 | public static void error(String message) {
|
---|
101 | logPrivate(LEVEL_ERROR, message);
|
---|
102 | }
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Prints a formatted error message if logging is on. Calls {@link MessageFormat#format}
|
---|
106 | * function to format text.
|
---|
107 | * @param pattern The formatted message to print.
|
---|
108 | * @param args The objects to insert into format string.
|
---|
109 | */
|
---|
110 | public static void error(String pattern, Object... args) {
|
---|
111 | logPrivate(LEVEL_ERROR, pattern, args);
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Prints an error message for the given Throwable if logging is on.
|
---|
116 | * @param t The throwable object causing the error.
|
---|
117 | * @since 12620
|
---|
118 | */
|
---|
119 | public static void error(Throwable t) {
|
---|
120 | logWithStackTrace(Logging.LEVEL_ERROR, t);
|
---|
121 | }
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Prints a warning message if logging is on.
|
---|
125 | * @param message The message to print.
|
---|
126 | */
|
---|
127 | public static void warn(String message) {
|
---|
128 | logPrivate(LEVEL_WARN, message);
|
---|
129 | }
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Prints a formatted warning message if logging is on. Calls {@link MessageFormat#format}
|
---|
133 | * function to format text.
|
---|
134 | * @param pattern The formatted message to print.
|
---|
135 | * @param args The objects to insert into format string.
|
---|
136 | */
|
---|
137 | public static void warn(String pattern, Object... args) {
|
---|
138 | logPrivate(LEVEL_WARN, pattern, args);
|
---|
139 | }
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Prints a warning message for the given Throwable if logging is on.
|
---|
143 | * @param t The throwable object causing the error.
|
---|
144 | * @since 12620
|
---|
145 | */
|
---|
146 | public static void warn(Throwable t) {
|
---|
147 | logWithStackTrace(Logging.LEVEL_WARN, t);
|
---|
148 | }
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * Prints a info message if logging is on.
|
---|
152 | * @param message The message to print.
|
---|
153 | */
|
---|
154 | public static void info(String message) {
|
---|
155 | logPrivate(LEVEL_INFO, message);
|
---|
156 | }
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * Prints a formatted info message if logging is on. Calls {@link MessageFormat#format}
|
---|
160 | * function to format text.
|
---|
161 | * @param pattern The formatted message to print.
|
---|
162 | * @param args The objects to insert into format string.
|
---|
163 | */
|
---|
164 | public static void info(String pattern, Object... args) {
|
---|
165 | logPrivate(LEVEL_INFO, pattern, args);
|
---|
166 | }
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * Prints a info message for the given Throwable if logging is on.
|
---|
170 | * @param t The throwable object causing the error.
|
---|
171 | * @since 12620
|
---|
172 | */
|
---|
173 | public static void info(Throwable t) {
|
---|
174 | logWithStackTrace(Logging.LEVEL_INFO, t);
|
---|
175 | }
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Prints a debug message if logging is on.
|
---|
179 | * @param message The message to print.
|
---|
180 | */
|
---|
181 | public static void debug(String message) {
|
---|
182 | logPrivate(LEVEL_DEBUG, message);
|
---|
183 | }
|
---|
184 |
|
---|
185 | /**
|
---|
186 | * Prints a formatted debug message if logging is on. Calls {@link MessageFormat#format}
|
---|
187 | * function to format text.
|
---|
188 | * @param pattern The formatted message to print.
|
---|
189 | * @param args The objects to insert into format string.
|
---|
190 | */
|
---|
191 | public static void debug(String pattern, Object... args) {
|
---|
192 | logPrivate(LEVEL_DEBUG, pattern, args);
|
---|
193 | }
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * Prints a debug message for the given Throwable if logging is on.
|
---|
197 | * @param t The throwable object causing the error.
|
---|
198 | * @since 12620
|
---|
199 | */
|
---|
200 | public static void debug(Throwable t) {
|
---|
201 | log(Logging.LEVEL_DEBUG, t);
|
---|
202 | }
|
---|
203 |
|
---|
204 | /**
|
---|
205 | * Prints a trace message if logging is on.
|
---|
206 | * @param message The message to print.
|
---|
207 | */
|
---|
208 | public static void trace(String message) {
|
---|
209 | logPrivate(LEVEL_TRACE, message);
|
---|
210 | }
|
---|
211 |
|
---|
212 | /**
|
---|
213 | * Prints a formatted trace message if logging is on. Calls {@link MessageFormat#format}
|
---|
214 | * function to format text.
|
---|
215 | * @param pattern The formatted message to print.
|
---|
216 | * @param args The objects to insert into format string.
|
---|
217 | */
|
---|
218 | public static void trace(String pattern, Object... args) {
|
---|
219 | logPrivate(LEVEL_TRACE, pattern, args);
|
---|
220 | }
|
---|
221 |
|
---|
222 | /**
|
---|
223 | * Prints a trace message for the given Throwable if logging is on.
|
---|
224 | * @param t The throwable object causing the error.
|
---|
225 | * @since 12620
|
---|
226 | */
|
---|
227 | public static void trace(Throwable t) {
|
---|
228 | log(Logging.LEVEL_TRACE, t);
|
---|
229 | }
|
---|
230 |
|
---|
231 | /**
|
---|
232 | * Logs a throwable that happened. The stack trace is not added to the log.
|
---|
233 | * @param level The level.
|
---|
234 | * @param t The throwable that should be logged.
|
---|
235 | * @see #logWithStackTrace(Level, Throwable)
|
---|
236 | */
|
---|
237 | public static void log(Level level, Throwable t) {
|
---|
238 | logPrivate(level, () -> getErrorLog(null, t));
|
---|
239 | }
|
---|
240 |
|
---|
241 | /**
|
---|
242 | * Logs a throwable that happened. The stack trace is not added to the log.
|
---|
243 | * @param level The level.
|
---|
244 | * @param message An additional error message
|
---|
245 | * @param t The throwable that caused the message
|
---|
246 | * @see #logWithStackTrace(Level, String, Throwable)
|
---|
247 | */
|
---|
248 | public static void log(Level level, String message, Throwable t) {
|
---|
249 | logPrivate(level, () -> getErrorLog(message, t));
|
---|
250 | }
|
---|
251 |
|
---|
252 | /**
|
---|
253 | * Logs a throwable that happened. Adds the stack trace to the log.
|
---|
254 | * @param level The level.
|
---|
255 | * @param t The throwable that should be logged.
|
---|
256 | * @see #log(Level, Throwable)
|
---|
257 | */
|
---|
258 | public static void logWithStackTrace(Level level, Throwable t) {
|
---|
259 | logPrivate(level, () -> getErrorLogWithStack(null, t));
|
---|
260 | }
|
---|
261 |
|
---|
262 | /**
|
---|
263 | * Logs a throwable that happened. Adds the stack trace to the log.
|
---|
264 | * @param level The level.
|
---|
265 | * @param message An additional error message
|
---|
266 | * @param t The throwable that should be logged.
|
---|
267 | * @see #logWithStackTrace(Level, Throwable)
|
---|
268 | */
|
---|
269 | public static void logWithStackTrace(Level level, String message, Throwable t) {
|
---|
270 | logPrivate(level, () -> getErrorLogWithStack(message, t));
|
---|
271 | }
|
---|
272 |
|
---|
273 | private static void logPrivate(Level level, String pattern, Object... args) {
|
---|
274 | logPrivate(level, () -> MessageFormat.format(pattern, args));
|
---|
275 | }
|
---|
276 |
|
---|
277 | private static void logPrivate(Level level, String message) {
|
---|
278 | logPrivate(level, () -> message);
|
---|
279 | }
|
---|
280 |
|
---|
281 | private static void logPrivate(Level level, Supplier<String> supplier) {
|
---|
282 | // all log methods immediately call one of the logPrivate methods.
|
---|
283 | if (LOGGER.isLoggable(level)) {
|
---|
284 | StackTraceElement callingMethod = BugReport.getCallingMethod(1, Logging.class.getName(), name -> !"logPrivate".equals(name));
|
---|
285 | LOGGER.logp(level, callingMethod.getClassName(), callingMethod.getMethodName(), supplier);
|
---|
286 | }
|
---|
287 | }
|
---|
288 |
|
---|
289 | /**
|
---|
290 | * Tests if a given log level is enabled. This can be used to avoid constructing debug data if required.
|
---|
291 | *
|
---|
292 | * For formatting text, you should use the {@link #debug(String, Object...)} message
|
---|
293 | * @param level A level constant. You can e.g. use {@link Logging#LEVEL_ERROR}
|
---|
294 | * @return <code>true</code> if log level is enabled.
|
---|
295 | */
|
---|
296 | public static boolean isLoggingEnabled(Level level) {
|
---|
297 | return LOGGER.isLoggable(level);
|
---|
298 | }
|
---|
299 |
|
---|
300 | /**
|
---|
301 | * Determines if debug log level is enabled.
|
---|
302 | * Useful to avoid costly construction of debug messages when not enabled.
|
---|
303 | * @return {@code true} if log level is at least debug, {@code false} otherwise
|
---|
304 | * @since 12620
|
---|
305 | */
|
---|
306 | public static boolean isDebugEnabled() {
|
---|
307 | return isLoggingEnabled(Logging.LEVEL_DEBUG);
|
---|
308 | }
|
---|
309 |
|
---|
310 | /**
|
---|
311 | * Determines if trace log level is enabled.
|
---|
312 | * Useful to avoid costly construction of trace messages when not enabled.
|
---|
313 | * @return {@code true} if log level is at least trace, {@code false} otherwise
|
---|
314 | * @since 12620
|
---|
315 | */
|
---|
316 | public static boolean isTraceEnabled() {
|
---|
317 | return isLoggingEnabled(Logging.LEVEL_TRACE);
|
---|
318 | }
|
---|
319 |
|
---|
320 | private static String getErrorLog(String message, Throwable t) {
|
---|
321 | StringBuilder sb = new StringBuilder();
|
---|
322 | if (message != null) {
|
---|
323 | sb.append(message).append(": ");
|
---|
324 | }
|
---|
325 | sb.append(getErrorMessage(t));
|
---|
326 | return sb.toString();
|
---|
327 | }
|
---|
328 |
|
---|
329 | private static String getErrorLogWithStack(String message, Throwable t) {
|
---|
330 | StringWriter sb = new StringWriter();
|
---|
331 | sb.append(getErrorLog(message, t));
|
---|
332 | if (t != null) {
|
---|
333 | sb.append('\n');
|
---|
334 | t.printStackTrace(new PrintWriter(sb));
|
---|
335 | }
|
---|
336 | return sb.toString();
|
---|
337 | }
|
---|
338 |
|
---|
339 | /**
|
---|
340 | * Returns a human-readable message of error, also usable for developers.
|
---|
341 | * @param t The error
|
---|
342 | * @return The human-readable error message
|
---|
343 | */
|
---|
344 | public static String getErrorMessage(Throwable t) {
|
---|
345 | if (t == null) {
|
---|
346 | return "(no error)";
|
---|
347 | }
|
---|
348 | StringBuilder sb = new StringBuilder(t.getClass().getName());
|
---|
349 | String msg = t.getMessage();
|
---|
350 | if (msg != null) {
|
---|
351 | sb.append(": ").append(msg.trim());
|
---|
352 | }
|
---|
353 | Throwable cause = t.getCause();
|
---|
354 | if (cause != null && !cause.equals(t)) {
|
---|
355 | // this may cause infinite loops in the unlikely case that there is a loop in the causes.
|
---|
356 | sb.append(". ").append(tr("Cause: ")).append(getErrorMessage(cause));
|
---|
357 | }
|
---|
358 | return sb.toString();
|
---|
359 | }
|
---|
360 |
|
---|
361 | /**
|
---|
362 | * Clear the list of last warnings
|
---|
363 | */
|
---|
364 | public static void clearLastErrorAndWarnings() {
|
---|
365 | WARNINGS.clear();
|
---|
366 | }
|
---|
367 |
|
---|
368 | /**
|
---|
369 | * Get the last error and warning messages in the order in which they were received.
|
---|
370 | * @return The last errors and warnings.
|
---|
371 | */
|
---|
372 | public static List<String> getLastErrorAndWarnings() {
|
---|
373 | return WARNINGS.getMessages();
|
---|
374 | }
|
---|
375 |
|
---|
376 | /**
|
---|
377 | * Provides direct access to the logger used. Use of methods like {@link #warn(String)} is prefered.
|
---|
378 | * @return The logger
|
---|
379 | */
|
---|
380 | public static Logger getLogger() {
|
---|
381 | return LOGGER;
|
---|
382 | }
|
---|
383 |
|
---|
384 | private static class RememberWarningHandler extends Handler {
|
---|
385 | private final String[] log = new String[10];
|
---|
386 | private int messagesLogged;
|
---|
387 |
|
---|
388 | RememberWarningHandler() {
|
---|
389 | setLevel(LEVEL_WARN);
|
---|
390 | }
|
---|
391 |
|
---|
392 | synchronized void clear() {
|
---|
393 | messagesLogged = 0;
|
---|
394 | Arrays.fill(log, null);
|
---|
395 | }
|
---|
396 |
|
---|
397 | @Override
|
---|
398 | public synchronized void publish(LogRecord record) {
|
---|
399 | if (!isLoggable(record)) {
|
---|
400 | return;
|
---|
401 | }
|
---|
402 |
|
---|
403 | String msg = getPrefix(record) + record.getMessage();
|
---|
404 |
|
---|
405 | // Only remember first line of message
|
---|
406 | int idx = msg.indexOf('\n');
|
---|
407 | if (idx > 0) {
|
---|
408 | msg = msg.substring(0, idx);
|
---|
409 | }
|
---|
410 | log[messagesLogged % log.length] = msg;
|
---|
411 | messagesLogged++;
|
---|
412 | }
|
---|
413 |
|
---|
414 | private static String getPrefix(LogRecord record) {
|
---|
415 | if (record.getLevel().equals(LEVEL_WARN)) {
|
---|
416 | return "W: ";
|
---|
417 | } else {
|
---|
418 | // worse than warn
|
---|
419 | return "E: ";
|
---|
420 | }
|
---|
421 | }
|
---|
422 |
|
---|
423 | synchronized List<String> getMessages() {
|
---|
424 | List<String> logged = Arrays.asList(log);
|
---|
425 | ArrayList<String> res = new ArrayList<>();
|
---|
426 | int logOffset = messagesLogged % log.length;
|
---|
427 | if (messagesLogged > logOffset) {
|
---|
428 | res.addAll(logged.subList(logOffset, log.length));
|
---|
429 | }
|
---|
430 | res.addAll(logged.subList(0, logOffset));
|
---|
431 | return res;
|
---|
432 | }
|
---|
433 |
|
---|
434 | @Override
|
---|
435 | public synchronized void flush() {
|
---|
436 | // nothing to do
|
---|
437 | }
|
---|
438 |
|
---|
439 | @Override
|
---|
440 | public void close() {
|
---|
441 | // nothing to do
|
---|
442 | }
|
---|
443 | }
|
---|
444 | }
|
---|