1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.async;
18
19 import java.io.IOException;
20 import java.util.HashMap;
21 import java.util.Map;
22
23 import org.apache.logging.log4j.Level;
24 import org.apache.logging.log4j.Marker;
25 import org.apache.logging.log4j.ThreadContext.ContextStack;
26 import org.apache.logging.log4j.core.LogEvent;
27 import org.apache.logging.log4j.core.config.Property;
28 import org.apache.logging.log4j.core.impl.Log4jLogEvent;
29 import org.apache.logging.log4j.core.impl.ThrowableProxy;
30 import org.apache.logging.log4j.core.lookup.StrSubstitutor;
31 import org.apache.logging.log4j.message.Message;
32 import org.apache.logging.log4j.message.SimpleMessage;
33 import org.apache.logging.log4j.util.Strings;
34
35 import com.lmax.disruptor.EventFactory;
36
37
38
39
40
41 public class RingBufferLogEvent implements LogEvent {
42 private static final long serialVersionUID = 8462119088943934758L;
43
44
45
46
47 private static class Factory implements EventFactory<RingBufferLogEvent> {
48
49 @Override
50 public RingBufferLogEvent newInstance() {
51 return new RingBufferLogEvent();
52 }
53 }
54
55
56 public static final Factory FACTORY = new Factory();
57
58 private transient AsyncLogger asyncLogger;
59 private String loggerName;
60 private Marker marker;
61 private String fqcn;
62 private Level level;
63 private Message message;
64 private transient Throwable thrown;
65 private ThrowableProxy thrownProxy;
66 private Map<String, String> contextMap;
67 private ContextStack contextStack;
68 private String threadName;
69 private StackTraceElement location;
70 private long currentTimeMillis;
71 private boolean endOfBatch;
72 private boolean includeLocation;
73 private long nanoTime;
74
75 public void setValues(final AsyncLogger asyncLogger, final String loggerName, final Marker marker,
76 final String fqcn, final Level level, final Message data, final Throwable throwable,
77 final Map<String, String> map, final ContextStack contextStack, final String threadName,
78 final StackTraceElement location, final long currentTimeMillis, final long nanoTime) {
79 this.asyncLogger = asyncLogger;
80 this.loggerName = loggerName;
81 this.marker = marker;
82 this.fqcn = fqcn;
83 this.level = level;
84 this.message = data;
85 this.thrown = throwable;
86 this.thrownProxy = null;
87 this.contextMap = map;
88 this.contextStack = contextStack;
89 this.threadName = threadName;
90 this.location = location;
91 this.currentTimeMillis = currentTimeMillis;
92 this.nanoTime = nanoTime;
93 }
94
95
96
97
98
99
100 public void execute(final boolean endOfBatch) {
101 this.endOfBatch = endOfBatch;
102 asyncLogger.actualAsyncLog(this);
103 }
104
105
106
107
108
109
110 @Override
111 public boolean isEndOfBatch() {
112 return endOfBatch;
113 }
114
115 @Override
116 public void setEndOfBatch(final boolean endOfBatch) {
117 this.endOfBatch = endOfBatch;
118 }
119
120 @Override
121 public boolean isIncludeLocation() {
122 return includeLocation;
123 }
124
125 @Override
126 public void setIncludeLocation(final boolean includeLocation) {
127 this.includeLocation = includeLocation;
128 }
129
130 @Override
131 public String getLoggerName() {
132 return loggerName;
133 }
134
135 @Override
136 public Marker getMarker() {
137 return marker;
138 }
139
140 @Override
141 public String getLoggerFqcn() {
142 return fqcn;
143 }
144
145 @Override
146 public Level getLevel() {
147 if (level == null) {
148 level = Level.OFF;
149 }
150 return level;
151 }
152
153 @Override
154 public Message getMessage() {
155 if (message == null) {
156 message = new SimpleMessage(Strings.EMPTY);
157 }
158 return message;
159 }
160
161 @Override
162 public Throwable getThrown() {
163
164 if (thrown == null) {
165 if (thrownProxy != null) {
166 thrown = thrownProxy.getThrowable();
167 }
168 }
169 return thrown;
170 }
171
172 @Override
173 public ThrowableProxy getThrownProxy() {
174
175 if (thrownProxy == null) {
176 if (thrown != null) {
177 thrownProxy = new ThrowableProxy(thrown);
178 }
179 }
180 return this.thrownProxy;
181 }
182
183 @Override
184 public Map<String, String> getContextMap() {
185 return contextMap;
186 }
187
188 @Override
189 public ContextStack getContextStack() {
190 return contextStack;
191 }
192
193 @Override
194 public String getThreadName() {
195 return threadName;
196 }
197
198 @Override
199 public StackTraceElement getSource() {
200 return location;
201 }
202
203 @Override
204 public long getTimeMillis() {
205 return currentTimeMillis;
206 }
207
208 @Override
209 public long getNanoTime() {
210 return nanoTime;
211 }
212
213
214
215
216
217
218
219
220 public void mergePropertiesIntoContextMap(final Map<Property, Boolean> properties,
221 final StrSubstitutor strSubstitutor) {
222 if (properties == null) {
223 return;
224 }
225
226 final Map<String, String> map = contextMap == null ? new HashMap<String, String>()
227 : new HashMap<>(contextMap);
228
229 for (final Map.Entry<Property, Boolean> entry : properties.entrySet()) {
230 final Property prop = entry.getKey();
231 if (map.containsKey(prop.getName())) {
232 continue;
233 }
234 final String value = entry.getValue().booleanValue() ? strSubstitutor.replace(prop.getValue()) : prop
235 .getValue();
236 map.put(prop.getName(), value);
237 }
238 contextMap = map;
239 }
240
241
242
243
244 public void clear() {
245 setValues(null,
246 null,
247 null,
248 null,
249 null,
250 null,
251 null,
252 null,
253 null,
254 null,
255 null,
256 0,
257 0
258 );
259 }
260
261 private void writeObject(final java.io.ObjectOutputStream out) throws IOException {
262 getThrownProxy();
263 out.defaultWriteObject();
264 }
265
266
267
268
269
270
271 public LogEvent createMemento() {
272 final LogEvent result = new Log4jLogEvent.Builder(this).build();
273 return result;
274 }
275
276
277
278
279
280 public void initializeBuilder(Log4jLogEvent.Builder builder) {
281 builder.setContextMap(contextMap)
282 .setContextStack(contextStack)
283 .setEndOfBatch(endOfBatch)
284 .setIncludeLocation(includeLocation)
285 .setLevel(getLevel())
286 .setLoggerFqcn(fqcn)
287 .setLoggerName(loggerName)
288 .setMarker(marker)
289 .setMessage(getMessage())
290 .setNanoTime(nanoTime)
291 .setSource(location)
292 .setThreadName(threadName)
293 .setThrown(getThrown())
294 .setThrownProxy(thrownProxy)
295 .setTimeMillis(currentTimeMillis)
296 ;
297 }
298 }