1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.log4j.xml;
19
20 import org.apache.log4j.helpers.Constants;
21 import org.apache.log4j.plugins.Receiver;
22 import org.apache.log4j.rule.ExpressionRule;
23 import org.apache.log4j.rule.Rule;
24 import org.apache.log4j.spi.Decoder;
25 import org.apache.log4j.spi.LoggingEvent;
26
27 import java.io.*;
28 import java.net.MalformedURLException;
29 import java.net.URL;
30 import java.util.Collection;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 public class LogFileXMLReceiver extends Receiver {
59 private String fileURL;
60 private Rule expressionRule;
61 private String filterExpression;
62 private String decoder = "org.apache.log4j.xml.XMLDecoder";
63 private boolean tailing = false;
64
65 private Decoder decoderInstance;
66 private Reader reader;
67 private static final String FILE_KEY = "file";
68 private String host;
69 private String path;
70 private boolean useCurrentThread;
71
72
73
74
75
76
77 public String getFileURL() {
78 return fileURL;
79 }
80
81
82
83
84
85
86 public void setFileURL(String fileURL) {
87 this.fileURL = fileURL;
88 }
89
90
91
92
93
94
95 public String getDecoder() {
96 return decoder;
97 }
98
99
100
101
102
103
104 public void setDecoder(String _decoder) {
105 decoder = _decoder;
106 }
107
108
109
110
111
112
113 public String getFilterExpression() {
114 return filterExpression;
115 }
116
117
118
119
120
121
122 public boolean isTailing() {
123 return tailing;
124 }
125
126
127
128
129
130
131
132 public void setTailing(boolean tailing) {
133 this.tailing = tailing;
134 }
135
136
137
138
139
140
141
142 public void setFilterExpression(String filterExpression) {
143 this.filterExpression = filterExpression;
144 }
145
146 private boolean passesExpression(LoggingEvent event) {
147 if (event != null) {
148 if (expressionRule != null) {
149 return (expressionRule.evaluate(event, null));
150 }
151 }
152 return true;
153 }
154
155 public static void main(String[] args) {
156
157
158
159
160
161 }
162
163
164
165
166 public void shutdown() {
167 try {
168 if (reader != null) {
169 reader.close();
170 reader = null;
171 }
172 } catch (IOException ioe) {
173 ioe.printStackTrace();
174 }
175 }
176
177
178
179
180 public void activateOptions() {
181 Runnable runnable = () -> {
182 try {
183 URL url = new URL(fileURL);
184 host = url.getHost();
185 if (host != null && host.equals("")) {
186 host = FILE_KEY;
187 }
188 path = url.getPath();
189 } catch (MalformedURLException e1) {
190
191 e1.printStackTrace();
192 }
193
194 try {
195 if (filterExpression != null) {
196 expressionRule = ExpressionRule.getRule(filterExpression);
197 }
198 } catch (Exception e) {
199 getLogger().warn("Invalid filter expression: " + filterExpression, e);
200 }
201
202 Class c;
203 try {
204 c = Class.forName(decoder);
205 Object o = c.newInstance();
206 if (o instanceof Decoder) {
207 decoderInstance = (Decoder) o;
208 }
209 } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
210
211 e.printStackTrace();
212 }
213
214 try {
215 reader = new InputStreamReader(new URL(getFileURL()).openStream());
216 process(reader);
217 } catch (FileNotFoundException fnfe) {
218 getLogger().info("file not available");
219 } catch (IOException ioe) {
220 getLogger().warn("unable to load file", ioe);
221 return;
222 }
223 };
224 if (useCurrentThread) {
225 runnable.run();
226 } else {
227 Thread thread = new Thread(runnable, "LogFileXMLReceiver-" + getName());
228
229 thread.start();
230
231 }
232 }
233
234 private void process(Reader unbufferedReader) throws IOException {
235 BufferedReader bufferedReader = new BufferedReader(unbufferedReader);
236 char[] content = new char[10000];
237 getLogger().debug("processing starting: " + fileURL);
238 int length;
239 do {
240 System.out.println("in do loop-about to process");
241 while ((length = bufferedReader.read(content)) > -1) {
242 processEvents(decoderInstance.decodeEvents(String.valueOf(content, 0, length)));
243 }
244 if (tailing) {
245 try {
246 Thread.sleep(5000);
247 } catch (InterruptedException e) {
248
249 e.printStackTrace();
250 }
251 }
252 } while (tailing);
253 getLogger().debug("processing complete: " + fileURL);
254
255 shutdown();
256 }
257
258 private void processEvents(Collection<LoggingEvent> c) {
259 if (c == null) {
260 return;
261 }
262
263 for (Object aC : c) {
264 LoggingEvent evt = (LoggingEvent) aC;
265 if (passesExpression(evt)) {
266 if (evt.getProperty(Constants.HOSTNAME_KEY) != null) {
267 evt.setProperty(Constants.HOSTNAME_KEY, host);
268 }
269 if (evt.getProperty(Constants.APPLICATION_KEY) != null) {
270 evt.setProperty(Constants.APPLICATION_KEY, path);
271 }
272 doPost(evt);
273 }
274 }
275 }
276
277
278
279
280
281
282
283 public final boolean isUseCurrentThread() {
284 return useCurrentThread;
285 }
286
287
288
289
290
291
292
293 public final void setUseCurrentThread(boolean useCurrentThread) {
294 this.useCurrentThread = useCurrentThread;
295 }
296
297 }