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