KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > lf5 > util > LogFileParser


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.log4j.lf5.util;
17
18 import org.apache.log4j.lf5.Log4JLogRecord;
19 import org.apache.log4j.lf5.LogLevel;
20 import org.apache.log4j.lf5.LogLevelFormatException;
21 import org.apache.log4j.lf5.LogRecord;
22 import org.apache.log4j.lf5.viewer.LogBrokerMonitor;
23 import org.apache.log4j.lf5.viewer.LogFactor5ErrorDialog;
24 import org.apache.log4j.lf5.viewer.LogFactor5LoadingDialog;
25
26 import javax.swing.*;
27 import java.io.*;
28 import java.text.ParseException JavaDoc;
29 import java.text.SimpleDateFormat JavaDoc;
30 import java.util.Date JavaDoc;
31
32 /**
33  * Provides utility methods for input and output streams.
34  *
35  * @author Brad Marlborough
36  * @author Richard Hurst
37  */

38
39 // Contributed by ThoughtWorks Inc.
40

41 public class LogFileParser implements Runnable JavaDoc {
42   //--------------------------------------------------------------------------
43
// Constants:
44
//--------------------------------------------------------------------------
45
public static final String JavaDoc RECORD_DELIMITER = "[slf5s.start]";
46   public static final String JavaDoc ATTRIBUTE_DELIMITER = "[slf5s.";
47   public static final String JavaDoc DATE_DELIMITER = ATTRIBUTE_DELIMITER + "DATE]";
48   public static final String JavaDoc THREAD_DELIMITER = ATTRIBUTE_DELIMITER + "THREAD]";
49   public static final String JavaDoc CATEGORY_DELIMITER = ATTRIBUTE_DELIMITER + "CATEGORY]";
50   public static final String JavaDoc LOCATION_DELIMITER = ATTRIBUTE_DELIMITER + "LOCATION]";
51   public static final String JavaDoc MESSAGE_DELIMITER = ATTRIBUTE_DELIMITER + "MESSAGE]";
52   public static final String JavaDoc PRIORITY_DELIMITER = ATTRIBUTE_DELIMITER + "PRIORITY]";
53   public static final String JavaDoc NDC_DELIMITER = ATTRIBUTE_DELIMITER + "NDC]";
54
55   //--------------------------------------------------------------------------
56
// Protected Variables:
57
//--------------------------------------------------------------------------
58

59   //--------------------------------------------------------------------------
60
// Private Variables:
61
//--------------------------------------------------------------------------
62
private static SimpleDateFormat JavaDoc _sdf = new SimpleDateFormat JavaDoc("dd MMM yyyy HH:mm:ss,S");
63   private LogBrokerMonitor _monitor;
64   LogFactor5LoadingDialog _loadDialog;
65   private InputStream _in = null;
66
67   //--------------------------------------------------------------------------
68
// Constructors:
69
//--------------------------------------------------------------------------
70
public LogFileParser(File file) throws IOException,
71       FileNotFoundException {
72     this(new FileInputStream(file));
73   }
74
75   public LogFileParser(InputStream stream) throws IOException {
76     _in = stream;
77   }
78   //--------------------------------------------------------------------------
79
// Public Methods:
80
//--------------------------------------------------------------------------
81

82   /**
83    * Starts a new thread to parse the log file and create a LogRecord.
84    * See run().
85    * @param LogBrokerMonitor
86    */

87   public void parse(LogBrokerMonitor monitor) throws RuntimeException JavaDoc {
88     _monitor = monitor;
89     Thread JavaDoc t = new Thread JavaDoc(this);
90     t.start();
91   }
92
93   /**
94    * Parses the file and creates new log records and adds the record
95    * to the monitor.
96    */

97   public void run() {
98
99     int index = 0;
100     int counter = 0;
101     LogRecord temp;
102     boolean isLogFile = false;
103
104     _loadDialog = new LogFactor5LoadingDialog(
105         _monitor.getBaseFrame(), "Loading file...");
106
107
108     try {
109       String JavaDoc logRecords = loadLogFile(_in);
110
111       while ((counter = logRecords.indexOf(RECORD_DELIMITER, index)) != -1) {
112         temp = createLogRecord(logRecords.substring(index, counter));
113         isLogFile = true;
114
115         if (temp != null) {
116           _monitor.addMessage(temp);
117         }
118
119         index = counter + RECORD_DELIMITER.length();
120       }
121
122       if (index < logRecords.length() && isLogFile) {
123         temp = createLogRecord(logRecords.substring(index));
124
125         if (temp != null) {
126           _monitor.addMessage(temp);
127         }
128       }
129
130       if (isLogFile == false) {
131         throw new RuntimeException JavaDoc("Invalid log file format");
132       }
133       SwingUtilities.invokeLater(new Runnable JavaDoc() {
134         public void run() {
135           destroyDialog();
136         }
137       });
138
139     } catch (RuntimeException JavaDoc e) {
140       destroyDialog();
141       displayError("Error - Invalid log file format.\nPlease see documentation"
142           + " on how to load log files.");
143     } catch (IOException e) {
144       destroyDialog();
145       displayError("Error - Unable to load log file!");
146     }
147
148     _in = null;
149   }
150
151   //--------------------------------------------------------------------------
152
// Protected Methods:
153
//--------------------------------------------------------------------------
154
protected void displayError(String JavaDoc message) {
155     LogFactor5ErrorDialog error = new LogFactor5ErrorDialog(
156         _monitor.getBaseFrame(), message);
157
158   }
159
160   //--------------------------------------------------------------------------
161
// Private Methods:
162
//--------------------------------------------------------------------------
163
private void destroyDialog() {
164     _loadDialog.hide();
165     _loadDialog.dispose();
166   }
167
168   /**
169    * Loads a log file from a web server into the LogFactor5 GUI.
170    */

171   private String JavaDoc loadLogFile(InputStream stream) throws IOException {
172     BufferedInputStream br = new BufferedInputStream(stream);
173
174     int count = 0;
175     int size = br.available();
176
177     StringBuffer JavaDoc sb = null;
178     if (size > 0) {
179       sb = new StringBuffer JavaDoc(size);
180     } else {
181       sb = new StringBuffer JavaDoc(1024);
182     }
183
184     while ((count = br.read()) != -1) {
185       sb.append((char) count);
186     }
187
188     br.close();
189     br = null;
190     return sb.toString();
191
192   }
193
194   private String JavaDoc parseAttribute(String JavaDoc name, String JavaDoc record) {
195
196     int index = record.indexOf(name);
197
198     if (index == -1) {
199       return null;
200     }
201
202     return getAttribute(index, record);
203   }
204
205   private long parseDate(String JavaDoc record) {
206     try {
207       String JavaDoc s = parseAttribute(DATE_DELIMITER, record);
208
209       if (s == null) {
210         return 0;
211       }
212
213       Date JavaDoc d = _sdf.parse(s);
214
215       return d.getTime();
216     } catch (ParseException JavaDoc e) {
217       return 0;
218     }
219   }
220
221   private LogLevel parsePriority(String JavaDoc record) {
222     String JavaDoc temp = parseAttribute(PRIORITY_DELIMITER, record);
223
224     if (temp != null) {
225       try {
226         return LogLevel.valueOf(temp);
227       } catch (LogLevelFormatException e) {
228         return LogLevel.DEBUG;
229       }
230
231     }
232
233     return LogLevel.DEBUG;
234   }
235
236   private String JavaDoc parseThread(String JavaDoc record) {
237     return parseAttribute(THREAD_DELIMITER, record);
238   }
239
240   private String JavaDoc parseCategory(String JavaDoc record) {
241     return parseAttribute(CATEGORY_DELIMITER, record);
242   }
243
244   private String JavaDoc parseLocation(String JavaDoc record) {
245     return parseAttribute(LOCATION_DELIMITER, record);
246   }
247
248   private String JavaDoc parseMessage(String JavaDoc record) {
249     return parseAttribute(MESSAGE_DELIMITER, record);
250   }
251
252   private String JavaDoc parseNDC(String JavaDoc record) {
253     return parseAttribute(NDC_DELIMITER, record);
254   }
255
256   private String JavaDoc parseThrowable(String JavaDoc record) {
257     return getAttribute(record.length(), record);
258   }
259
260   private LogRecord createLogRecord(String JavaDoc record) {
261     if (record == null || record.trim().length() == 0) {
262       return null;
263     }
264
265     LogRecord lr = new Log4JLogRecord();
266     lr.setMillis(parseDate(record));
267     lr.setLevel(parsePriority(record));
268     lr.setCategory(parseCategory(record));
269     lr.setLocation(parseLocation(record));
270     lr.setThreadDescription(parseThread(record));
271     lr.setNDC(parseNDC(record));
272     lr.setMessage(parseMessage(record));
273     lr.setThrownStackTrace(parseThrowable(record));
274
275     return lr;
276   }
277
278
279   private String JavaDoc getAttribute(int index, String JavaDoc record) {
280     int start = record.lastIndexOf(ATTRIBUTE_DELIMITER, index - 1);
281
282     if (start == -1) {
283       return record.substring(0, index);
284     }
285
286     start = record.indexOf("]", start);
287
288     return record.substring(start + 1, index).trim();
289   }
290   //--------------------------------------------------------------------------
291
// Nested Top-Level Classes or Interfaces
292
//--------------------------------------------------------------------------
293

294 }
295
Popular Tags