/
ErrorMessageHandler.java
45 lines (39 loc) · 1.39 KB
/
ErrorMessageHandler.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
package org.triplea.debug;
import com.google.common.annotations.VisibleForTesting;
import java.util.function.Consumer;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.LogRecord;
/**
* A {@link Handler} that displays an error message dialog if any log record with a level equal to
* or greater than {@link Level#WARNING} is published.
*
* <p><strong>Configuration:</strong> This handler does not currently support configuration through
* the {@link LogManager}. It always uses the following default configuration:
*
* <ul>
* <li>Level: {@code Level.ALL}
* <li>Filter: No {@code Filter}
* <li>Formatter: No {@code Formatter}
* <li>Encoding: default platform encoding
* </ul>
*/
public final class ErrorMessageHandler extends Handler {
@VisibleForTesting static final int THRESHOLD_LEVEL_VALUE = Level.WARNING.intValue();
@Override
public void close() {}
@Override
public void flush() {}
@Override
public synchronized void publish(final LogRecord record) {
publish(
record, logRecord -> ErrorMessage.show(LoggerRecordAdapter.fromJavaUtilLogRecord(record)));
}
@VisibleForTesting
void publish(final LogRecord record, final Consumer<LogRecord> consumer) {
if (isLoggable(record) && (record.getLevel().intValue() >= THRESHOLD_LEVEL_VALUE)) {
consumer.accept(record);
}
}
}