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
31 import com.lmax.disruptor.EventFactory;
32
33
34
35
36
37 public class RingBufferLogEvent implements LogEvent {
38 private static final long serialVersionUID = 8462119088943934758L;
39
40
41
42
43 private static class Factory implements EventFactory<RingBufferLogEvent> {
44
45 @Override
46 public RingBufferLogEvent newInstance() {
47 return new RingBufferLogEvent();
48 }
49 }
50
51
52 public static final Factory FACTORY = new Factory();
53
54 private AsyncLogger asyncLogger;
55 private String loggerName;
56 private Marker marker;
57 private String fqcn;
58 private Level level;
59 private Message message;
60 private Throwable thrown;
61 private Map<String, String> contextMap;
62 private ContextStack contextStack;
63 private String threadName;
64 private StackTraceElement location;
65 private long currentTimeMillis;
66 private boolean endOfBatch;
67 private boolean includeLocation;
68
69 public void setValues(final AsyncLogger asyncLogger,
70 final String loggerName, final Marker marker, final String fqcn,
71 final Level level, final Message data, final Throwable t,
72 final Map<String, String> map, final ContextStack contextStack,
73 final String threadName, final StackTraceElement location,
74 final long currentTimeMillis) {
75 this.asyncLogger = asyncLogger;
76 this.loggerName = loggerName;
77 this.marker = marker;
78 this.fqcn = fqcn;
79 this.level = level;
80 this.message = data;
81 this.thrown = t;
82 this.contextMap = map;
83 this.contextStack = contextStack;
84 this.threadName = threadName;
85 this.location = location;
86 this.currentTimeMillis = currentTimeMillis;
87 }
88
89
90
91
92
93
94
95
96 public void execute(final boolean endOfBatch) {
97 this.endOfBatch = endOfBatch;
98 asyncLogger.actualAsyncLog(this);
99 }
100
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 getFQCN() {
140 return fqcn;
141 }
142
143 @Override
144 public Level getLevel() {
145 return level;
146 }
147
148 @Override
149 public Message getMessage() {
150 if (message == null) {
151 message = new SimpleMessage("");
152 }
153 return message;
154 }
155
156 @Override
157 public Throwable getThrown() {
158 return thrown;
159 }
160
161 @Override
162 public Map<String, String> getContextMap() {
163 return contextMap;
164 }
165
166 @Override
167 public ContextStack getContextStack() {
168 return contextStack;
169 }
170
171 @Override
172 public String getThreadName() {
173 return threadName;
174 }
175
176 @Override
177 public StackTraceElement getSource() {
178 return location;
179 }
180
181 @Override
182 public long getMillis() {
183 return currentTimeMillis;
184 }
185
186
187
188
189
190
191
192
193
194 public void mergePropertiesIntoContextMap(
195 final Map<Property, Boolean> properties,
196 final StrSubstitutor strSubstitutor) {
197 if (properties == null) {
198 return;
199 }
200
201 final Map<String, String> map = (contextMap == null) ? new HashMap<String, String>()
202 : new HashMap<String, String>(contextMap);
203
204 for (final Map.Entry<Property, Boolean> entry : properties.entrySet()) {
205 final Property prop = entry.getKey();
206 if (map.containsKey(prop.getName())) {
207 continue;
208 }
209 final String value = entry.getValue() ? strSubstitutor.replace(prop
210 .getValue()) : prop.getValue();
211 map.put(prop.getName(), value);
212 }
213 contextMap = map;
214 }
215
216
217
218
219
220 public void clear() {
221 setValues(null,
222 null,
223 null,
224 null,
225 null,
226 null,
227 null,
228 null,
229 null,
230 null,
231 null,
232 0
233 );
234 }
235 }