1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codelibs.fess.helper;
17
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.concurrent.ConcurrentLinkedQueue;
21 import java.util.concurrent.atomic.AtomicInteger;
22
23 import org.codelibs.core.timer.TimeoutManager;
24 import org.codelibs.core.timer.TimeoutTask;
25 import org.codelibs.fess.timer.LogNotificationTarget;
26 import org.codelibs.fess.util.ComponentUtil;
27
28 import jakarta.annotation.PostConstruct;
29 import jakarta.annotation.PreDestroy;
30
31
32
33
34
35 public class LogNotificationHelper {
36
37
38
39
40 public LogNotificationHelper() {
41
42 }
43
44 private TimeoutTask timeoutTask;
45 private LogNotificationTarget logNotificationTarget;
46 private final ConcurrentLinkedQueue<LogNotificationEvent> queue = new ConcurrentLinkedQueue<>();
47 private final AtomicInteger size = new AtomicInteger(0);
48
49
50
51
52 @PostConstruct
53 public void init() {
54 logNotificationTarget = new LogNotificationTarget();
55 final int interval = ComponentUtil.getFessConfig().getLogNotificationFlushIntervalAsInteger();
56 timeoutTask = TimeoutManager.getInstance().addTimeoutTarget(logNotificationTarget, interval, true);
57 }
58
59
60
61
62 @PreDestroy
63 public void destroy() {
64 if (timeoutTask != null) {
65 timeoutTask.cancel();
66 }
67 if (logNotificationTarget != null) {
68 logNotificationTarget.flush();
69 }
70 }
71
72
73
74
75
76
77 public void offer(final LogNotificationEvent event) {
78 int maxBufferSize;
79 try {
80 maxBufferSize = ComponentUtil.getFessConfig().getLogNotificationBufferSizeAsInteger();
81 } catch (final Exception e) {
82 maxBufferSize = 1000;
83 }
84 queue.offer(event);
85 if (size.incrementAndGet() > maxBufferSize) {
86 if (queue.poll() != null) {
87 size.decrementAndGet();
88 }
89 }
90 }
91
92
93
94
95
96
97 public List<LogNotificationEvent> drainAll() {
98 final List<LogNotificationEvent> events = new ArrayList<>();
99 LogNotificationEvent event;
100 while ((event = queue.poll()) != null) {
101 events.add(event);
102 size.decrementAndGet();
103 }
104 return events;
105 }
106
107
108
109
110 public static class LogNotificationEvent {
111
112 private final long timestamp;
113
114 private final String level;
115
116 private final String loggerName;
117
118 private final String message;
119
120 private final String throwable;
121
122
123
124
125
126
127
128
129
130
131 public LogNotificationEvent(final long timestamp, final String level, final String loggerName, final String message,
132 final String throwable) {
133 this.timestamp = timestamp;
134 this.level = level;
135 this.loggerName = loggerName;
136 this.message = message;
137 this.throwable = throwable;
138 }
139
140
141
142
143
144
145 public long getTimestamp() {
146 return timestamp;
147 }
148
149
150
151
152
153
154 public String getLevel() {
155 return level;
156 }
157
158
159
160
161
162
163 public String getLoggerName() {
164 return loggerName;
165 }
166
167
168
169
170
171
172 public String getMessage() {
173 return message;
174 }
175
176
177
178
179
180
181 public String getThrowable() {
182 return throwable;
183 }
184 }
185 }