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
74 public void setValues(final AsyncLogger asyncLogger, final String loggerName, final Marker marker,
75 final String fqcn, final Level level, final Message data, final Throwable throwable,
76 final Map<String, String> map, final ContextStack contextStack, final String threadName,
77 final StackTraceElement location, final long currentTimeMillis) {
78 this.asyncLogger = asyncLogger;
79 this.loggerName = loggerName;
80 this.marker = marker;
81 this.fqcn = fqcn;
82 this.level = level;
83 this.message = data;
84 this.thrown = throwable;
85 this.thrownProxy = null;
86 this.contextMap = map;
87 this.contextStack = contextStack;
88 this.threadName = threadName;
89 this.location = location;
90 this.currentTimeMillis = currentTimeMillis;
91 }
92
93
94
95
96
97
98 public void execute(final boolean endOfBatch) {
99 this.endOfBatch = endOfBatch;
100 asyncLogger.actualAsyncLog(this);
101 }
102
103
104
105
106
107
108 @Override
109 public boolean isEndOfBatch() {
110 return endOfBatch;
111 }
112
113 @Override
114 public void setEndOfBatch(final boolean endOfBatch) {
115 this.endOfBatch = endOfBatch;
116 }
117
118 @Override
119 public boolean isIncludeLocation() {
120 return includeLocation;
121 }
122
123 @Override
124 public void setIncludeLocation(final boolean includeLocation) {
125 this.includeLocation = includeLocation;
126 }
127
128 @Override
129 public String getLoggerName() {
130 return loggerName;
131 }
132
133 @Override
134 public Marker getMarker() {
135 return marker;
136 }
137
138 @Override
139 public String getLoggerFqcn() {
140 return fqcn;
141 }
142
143 @Override
144 public Level getLevel() {
145 if (level == null) {
146 level = Level.OFF;
147 }
148 return level;
149 }
150
151 @Override
152 public Message getMessage() {
153 if (message == null) {
154 message = new SimpleMessage(Strings.EMPTY);
155 }
156 return message;
157 }
158
159 @Override
160 public Throwable getThrown() {
161
162 if (thrown == null) {
163 if (thrownProxy != null) {
164 thrown = thrownProxy.getThrowable();
165 }
166 }
167 return thrown;
168 }
169
170 @Override
171 public ThrowableProxy getThrownProxy() {
172
173 if (thrownProxy == null) {
174 if (thrown != null) {
175 thrownProxy = new ThrowableProxy(thrown);
176 }
177 }
178 return this.thrownProxy;
179 }
180
181 @Override
182 public Map<String, String> getContextMap() {
183 return contextMap;
184 }
185
186 @Override
187 public ContextStack getContextStack() {
188 return contextStack;
189 }
190
191 @Override
192 public String getThreadName() {
193 return threadName;
194 }
195
196 @Override
197 public StackTraceElement getSource() {
198 return location;
199 }
200
201 @Override
202 public long getTimeMillis() {
203 return currentTimeMillis;
204 }
205
206
207
208
209
210
211
212
213 public void mergePropertiesIntoContextMap(final Map<Property, Boolean> properties,
214 final StrSubstitutor strSubstitutor) {
215 if (properties == null) {
216 return;
217 }
218
219 final Map<String, String> map = contextMap == null ? new HashMap<String, String>()
220 : new HashMap<String, String>(contextMap);
221
222 for (final Map.Entry<Property, Boolean> entry : properties.entrySet()) {
223 final Property prop = entry.getKey();
224 if (map.containsKey(prop.getName())) {
225 continue;
226 }
227 final String value = entry.getValue().booleanValue() ? strSubstitutor.replace(prop.getValue()) : prop
228 .getValue();
229 map.put(prop.getName(), value);
230 }
231 contextMap = map;
232 }
233
234
235
236
237 public void clear() {
238 setValues(null,
239 null,
240 null,
241 null,
242 null,
243 null,
244 null,
245 null,
246 null,
247 null,
248 null,
249 0
250 );
251 }
252
253 private void writeObject(final java.io.ObjectOutputStream out) throws IOException {
254 getThrownProxy();
255 out.defaultWriteObject();
256 }
257
258
259
260
261
262
263 public LogEvent createMemento() {
264
265
266 return new Log4jLogEvent(loggerName, marker, fqcn, level, message, thrown, contextMap, contextStack,
267 threadName, location, currentTimeMillis);
268 }
269 }