1 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 ; 29 import java.text.SimpleDateFormat ; 30 import java.util.Date ; 31 32 38 39 41 public class LogFileParser implements Runnable { 42 public static final String RECORD_DELIMITER = "[slf5s.start]"; 46 public static final String ATTRIBUTE_DELIMITER = "[slf5s."; 47 public static final String DATE_DELIMITER = ATTRIBUTE_DELIMITER + "DATE]"; 48 public static final String THREAD_DELIMITER = ATTRIBUTE_DELIMITER + "THREAD]"; 49 public static final String CATEGORY_DELIMITER = ATTRIBUTE_DELIMITER + "CATEGORY]"; 50 public static final String LOCATION_DELIMITER = ATTRIBUTE_DELIMITER + "LOCATION]"; 51 public static final String MESSAGE_DELIMITER = ATTRIBUTE_DELIMITER + "MESSAGE]"; 52 public static final String PRIORITY_DELIMITER = ATTRIBUTE_DELIMITER + "PRIORITY]"; 53 public static final String NDC_DELIMITER = ATTRIBUTE_DELIMITER + "NDC]"; 54 55 59 private static SimpleDateFormat _sdf = new SimpleDateFormat ("dd MMM yyyy HH:mm:ss,S"); 63 private LogBrokerMonitor _monitor; 64 LogFactor5LoadingDialog _loadDialog; 65 private InputStream _in = null; 66 67 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 82 87 public void parse(LogBrokerMonitor monitor) throws RuntimeException { 88 _monitor = monitor; 89 Thread t = new Thread (this); 90 t.start(); 91 } 92 93 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 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 ("Invalid log file format"); 132 } 133 SwingUtilities.invokeLater(new Runnable () { 134 public void run() { 135 destroyDialog(); 136 } 137 }); 138 139 } catch (RuntimeException 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 protected void displayError(String message) { 155 LogFactor5ErrorDialog error = new LogFactor5ErrorDialog( 156 _monitor.getBaseFrame(), message); 157 158 } 159 160 private void destroyDialog() { 164 _loadDialog.hide(); 165 _loadDialog.dispose(); 166 } 167 168 171 private String loadLogFile(InputStream stream) throws IOException { 172 BufferedInputStream br = new BufferedInputStream(stream); 173 174 int count = 0; 175 int size = br.available(); 176 177 StringBuffer sb = null; 178 if (size > 0) { 179 sb = new StringBuffer (size); 180 } else { 181 sb = new StringBuffer (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 parseAttribute(String name, String 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 record) { 206 try { 207 String s = parseAttribute(DATE_DELIMITER, record); 208 209 if (s == null) { 210 return 0; 211 } 212 213 Date d = _sdf.parse(s); 214 215 return d.getTime(); 216 } catch (ParseException e) { 217 return 0; 218 } 219 } 220 221 private LogLevel parsePriority(String record) { 222 String 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 parseThread(String record) { 237 return parseAttribute(THREAD_DELIMITER, record); 238 } 239 240 private String parseCategory(String record) { 241 return parseAttribute(CATEGORY_DELIMITER, record); 242 } 243 244 private String parseLocation(String record) { 245 return parseAttribute(LOCATION_DELIMITER, record); 246 } 247 248 private String parseMessage(String record) { 249 return parseAttribute(MESSAGE_DELIMITER, record); 250 } 251 252 private String parseNDC(String record) { 253 return parseAttribute(NDC_DELIMITER, record); 254 } 255 256 private String parseThrowable(String record) { 257 return getAttribute(record.length(), record); 258 } 259 260 private LogRecord createLogRecord(String 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 getAttribute(int index, String 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 294 } 295 | Popular Tags |