1
|
package eu.dnetlib.functionality.modular.ui.utils;
|
2
|
|
3
|
import java.text.SimpleDateFormat;
|
4
|
import java.util.Date;
|
5
|
import java.util.List;
|
6
|
import java.util.concurrent.LinkedBlockingQueue;
|
7
|
|
8
|
import org.apache.log4j.AppenderSkeleton;
|
9
|
import org.apache.log4j.spi.LoggingEvent;
|
10
|
|
11
|
import com.google.common.base.Joiner;
|
12
|
import com.google.common.base.Predicate;
|
13
|
import com.google.common.collect.Iterables;
|
14
|
import com.google.common.collect.Lists;
|
15
|
import com.google.common.collect.Queues;
|
16
|
|
17
|
public class LogUiAppender extends AppenderSkeleton {
|
18
|
|
19
|
public static final int MAX_LOGS_IN_QUEUE = 2000;
|
20
|
|
21
|
private final LinkedBlockingQueue<LogLine> logQueue = Queues.newLinkedBlockingQueue(MAX_LOGS_IN_QUEUE);
|
22
|
|
23
|
private int count = 0;
|
24
|
|
25
|
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
26
|
|
27
|
@Override
|
28
|
protected void append(final LoggingEvent event) {
|
29
|
synchronized (logQueue) {
|
30
|
if (logQueue.remainingCapacity() == 0) {
|
31
|
logQueue.poll();
|
32
|
}
|
33
|
try {
|
34
|
final String date = dateFormat.format(new Date(event.getTimeStamp()));
|
35
|
|
36
|
final String[] arr = event.getThrowableStrRep();
|
37
|
final String strace = arr != null && arr.length > 0 ? Joiner.on("\n").join(arr) : "";
|
38
|
|
39
|
logQueue.put(new LogLine(count++, event.getLevel().toString(), event.getLoggerName(), date, event.getRenderedMessage(), strace));
|
40
|
} catch (InterruptedException e) {
|
41
|
throw new RuntimeException(e);
|
42
|
}
|
43
|
}
|
44
|
}
|
45
|
|
46
|
public List<LogLine> tail_N(final int n) throws Exception {
|
47
|
synchronized (logQueue) {
|
48
|
int qSize = logQueue.size();
|
49
|
|
50
|
if (n <= 0 || qSize == 0) {
|
51
|
return Lists.newArrayList();
|
52
|
} else if (n < qSize) {
|
53
|
return Lists.newArrayList(logQueue).subList(qSize - n, qSize);
|
54
|
} else {
|
55
|
return Lists.newArrayList(logQueue);
|
56
|
}
|
57
|
}
|
58
|
}
|
59
|
|
60
|
public List<LogLine> tail_continue(final int after) throws Exception {
|
61
|
synchronized (logQueue) {
|
62
|
return Lists.newArrayList(Iterables.filter(logQueue, new Predicate<LogLine>() {
|
63
|
|
64
|
@Override
|
65
|
public boolean apply(final LogLine ll) {
|
66
|
return ll.getId() > after;
|
67
|
}
|
68
|
}));
|
69
|
}
|
70
|
}
|
71
|
|
72
|
@Override
|
73
|
public void close() {}
|
74
|
|
75
|
@Override
|
76
|
public boolean requiresLayout() {
|
77
|
return false;
|
78
|
}
|
79
|
|
80
|
}
|