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.util.HashMap;
20 import java.util.Map;
21
22 import org.apache.logging.log4j.Level;
23 import org.apache.logging.log4j.Marker;
24 import org.apache.logging.log4j.ThreadContext.ContextStack;
25 import org.apache.logging.log4j.core.LogEvent;
26 import org.apache.logging.log4j.core.config.Property;
27 import org.apache.logging.log4j.core.lookup.StrSubstitutor;
28 import org.apache.logging.log4j.message.Message;
29 import org.apache.logging.log4j.message.SimpleMessage;
30 import org.apache.logging.log4j.message.TimestampMessage;
31
32 import com.lmax.disruptor.EventFactory;
33
34
35
36
37
38 public class RingBufferLogEvent implements LogEvent {
39 private static final long serialVersionUID = 8462119088943934758L;
40
41
42
43
44 private static class Factory implements EventFactory<RingBufferLogEvent> {
45
46 @Override
47 public RingBufferLogEvent newInstance() {
48 return new RingBufferLogEvent();
49 }
50 }
51
52
53 public static final Factory FACTORY = new Factory();
54
55 private AsyncLogger asyncLogger;
56 private String loggerName;
57 private Marker marker;
58 private String fqcn;
59 private Level level;
60 private Message message;
61 private Throwable thrown;
62 private Map<String, String> contextMap;
63 private ContextStack contextStack;
64 private String threadName;
65 private StackTraceElement location;
66 private long currentTimeMillis;
67 private boolean endOfBatch;
68 private boolean includeLocation;
69
70 public void setValues(final AsyncLogger asyncLogger,
71 final String loggerName, final Marker marker, final String fqcn,
72 final Level level, final Message data, final Throwable t,
73 final Map<String, String> map, final ContextStack contextStack,
74 final String threadName, final StackTraceElement location,
75 final long currentTimeMillis) {
76 this.asyncLogger = asyncLogger;
77 this.loggerName = loggerName;
78 this.marker = marker;
79 this.fqcn = fqcn;
80 this.level = level;
81 this.message = data;
82 this.thrown = t;
83 this.contextMap = map;
84 this.contextStack = contextStack;
85 this.threadName = threadName;
86 this.location = location;
87 this.currentTimeMillis = currentTimeMillis;
88 }
89
90
91
92
93
94
95
96
97 public void execute(final boolean endOfBatch) {
98 this.endOfBatch = endOfBatch;
99 asyncLogger.actualAsyncLog(this);
100 }
101
102
103
104
105
106
107
108
109 @Override
110 public boolean isEndOfBatch() {
111 return endOfBatch;
112 }
113
114 @Override
115 public void setEndOfBatch(final boolean endOfBatch) {
116 this.endOfBatch = endOfBatch;
117 }
118
119 @Override
120 public boolean isIncludeLocation() {
121 return includeLocation;
122 }
123
124 @Override
125 public void setIncludeLocation(final boolean includeLocation) {
126 this.includeLocation = includeLocation;
127 }
128
129 @Override
130 public String getLoggerName() {
131 return loggerName;
132 }
133
134 @Override
135 public Marker getMarker() {
136 return marker;
137 }
138
139 @Override
140 public String getFQCN() {
141 return fqcn;
142 }
143
144 @Override
145 public Level getLevel() {
146 if (level == null) {
147 level = Level.OFF;
148 }
149 return level;
150 }
151
152 @Override
153 public Message getMessage() {
154 if (message == null) {
155 message = new SimpleMessage("");
156 }
157 return message;
158 }
159
160 @Override
161 public Throwable getThrown() {
162 return thrown;
163 }
164
165 @Override
166 public Map<String, String> getContextMap() {
167 return contextMap;
168 }
169
170 @Override
171 public ContextStack getContextStack() {
172 return contextStack;
173 }
174
175 @Override
176 public String getThreadName() {
177 return threadName;
178 }
179
180 @Override
181 public StackTraceElement getSource() {
182 return location;
183 }
184
185 @Override
186 public long getMillis() {
187 Message msg = getMessage();
188 if (msg instanceof TimestampMessage) {
189 return ((TimestampMessage) msg).getTimestamp();
190 }
191 return currentTimeMillis;
192 }
193
194
195
196
197
198
199
200
201
202 public void mergePropertiesIntoContextMap(
203 final Map<Property, Boolean> properties,
204 final StrSubstitutor strSubstitutor) {
205 if (properties == null) {
206 return;
207 }
208
209 final Map<String, String> map = (contextMap == null) ? new HashMap<String, String>()
210 : new HashMap<String, String>(contextMap);
211
212 for (final Map.Entry<Property, Boolean> entry : properties.entrySet()) {
213 final Property prop = entry.getKey();
214 if (map.containsKey(prop.getName())) {
215 continue;
216 }
217 final String value = entry.getValue() ? strSubstitutor.replace(prop
218 .getValue()) : prop.getValue();
219 map.put(prop.getName(), value);
220 }
221 contextMap = map;
222 }
223
224
225
226
227
228 public void clear() {
229 setValues(null,
230 null,
231 null,
232 null,
233 null,
234 null,
235 null,
236 null,
237 null,
238 null,
239 null,
240 0
241 );
242 }
243 }