FileDocCategorySizeDatePackage
LogFileParser.javaAPI DocApache log4j 1.2.158873Sat Aug 25 00:09:38 BST 2007org.apache.log4j.lf5.util

LogFileParser

public class LogFileParser extends Object implements Runnable
Provides utility methods for input and output streams.
author
Brad Marlborough
author
Richard Hurst

Fields Summary
public static final String
RECORD_DELIMITER
public static final String
ATTRIBUTE_DELIMITER
public static final String
DATE_DELIMITER
public static final String
THREAD_DELIMITER
public static final String
CATEGORY_DELIMITER
public static final String
LOCATION_DELIMITER
public static final String
MESSAGE_DELIMITER
public static final String
PRIORITY_DELIMITER
public static final String
NDC_DELIMITER
private static SimpleDateFormat
_sdf
private LogBrokerMonitor
_monitor
LogFactor5LoadingDialog
_loadDialog
private InputStream
_in
Constructors Summary
public LogFileParser(File file)


  //--------------------------------------------------------------------------
  //   Constructors:
  //--------------------------------------------------------------------------
      
       
    this(new FileInputStream(file));
  
public LogFileParser(InputStream stream)

    _in = stream;
  
Methods Summary
private org.apache.log4j.lf5.LogRecordcreateLogRecord(java.lang.String record)

    if (record == null || record.trim().length() == 0) {
      return null;
    }

    LogRecord lr = new Log4JLogRecord();
    lr.setMillis(parseDate(record));
    lr.setLevel(parsePriority(record));
    lr.setCategory(parseCategory(record));
    lr.setLocation(parseLocation(record));
    lr.setThreadDescription(parseThread(record));
    lr.setNDC(parseNDC(record));
    lr.setMessage(parseMessage(record));
    lr.setThrownStackTrace(parseThrowable(record));

    return lr;
  
private voiddestroyDialog()

    _loadDialog.hide();
    _loadDialog.dispose();
  
protected voiddisplayError(java.lang.String message)

    LogFactor5ErrorDialog error = new LogFactor5ErrorDialog(
        _monitor.getBaseFrame(), message);

  
private java.lang.StringgetAttribute(int index, java.lang.String record)

    int start = record.lastIndexOf(ATTRIBUTE_DELIMITER, index - 1);

    if (start == -1) {
      return record.substring(0, index);
    }

    start = record.indexOf("]", start);

    return record.substring(start + 1, index).trim();
  
private java.lang.StringloadLogFile(java.io.InputStream stream)
Loads a log file from a web server into the LogFactor5 GUI.

    BufferedInputStream br = new BufferedInputStream(stream);

    int count = 0;
    int size = br.available();

    StringBuffer sb = null;
    if (size > 0) {
      sb = new StringBuffer(size);
    } else {
      sb = new StringBuffer(1024);
    }

    while ((count = br.read()) != -1) {
      sb.append((char) count);
    }

    br.close();
    br = null;
    return sb.toString();

  
public voidparse(org.apache.log4j.lf5.viewer.LogBrokerMonitor monitor)
Starts a new thread to parse the log file and create a LogRecord. See run().

param
monitor LogBrokerMonitor

    _monitor = monitor;
    Thread t = new Thread(this);
    t.start();
  
private java.lang.StringparseAttribute(java.lang.String name, java.lang.String record)


    int index = record.indexOf(name);

    if (index == -1) {
      return null;
    }

    return getAttribute(index, record);
  
private java.lang.StringparseCategory(java.lang.String record)

    return parseAttribute(CATEGORY_DELIMITER, record);
  
private longparseDate(java.lang.String record)

    try {
      String s = parseAttribute(DATE_DELIMITER, record);

      if (s == null) {
        return 0;
      }

      Date d = _sdf.parse(s);

      return d.getTime();
    } catch (ParseException e) {
      return 0;
    }
  
private java.lang.StringparseLocation(java.lang.String record)

    return parseAttribute(LOCATION_DELIMITER, record);
  
private java.lang.StringparseMessage(java.lang.String record)

    return parseAttribute(MESSAGE_DELIMITER, record);
  
private java.lang.StringparseNDC(java.lang.String record)

    return parseAttribute(NDC_DELIMITER, record);
  
private org.apache.log4j.lf5.LogLevelparsePriority(java.lang.String record)

    String temp = parseAttribute(PRIORITY_DELIMITER, record);

    if (temp != null) {
      try {
        return LogLevel.valueOf(temp);
      } catch (LogLevelFormatException e) {
        return LogLevel.DEBUG;
      }

    }

    return LogLevel.DEBUG;
  
private java.lang.StringparseThread(java.lang.String record)

    return parseAttribute(THREAD_DELIMITER, record);
  
private java.lang.StringparseThrowable(java.lang.String record)

    return getAttribute(record.length(), record);
  
public voidrun()
Parses the file and creates new log records and adds the record to the monitor.


    int index = 0;
    int counter = 0;
    LogRecord temp;
    boolean isLogFile = false;

    _loadDialog = new LogFactor5LoadingDialog(
        _monitor.getBaseFrame(), "Loading file...");


    try {
      String logRecords = loadLogFile(_in);

      while ((counter = logRecords.indexOf(RECORD_DELIMITER, index)) != -1) {
        temp = createLogRecord(logRecords.substring(index, counter));
        isLogFile = true;

        if (temp != null) {
          _monitor.addMessage(temp);
        }

        index = counter + RECORD_DELIMITER.length();
      }

      if (index < logRecords.length() && isLogFile) {
        temp = createLogRecord(logRecords.substring(index));

        if (temp != null) {
          _monitor.addMessage(temp);
        }
      }

      if (isLogFile == false) {
        throw new RuntimeException("Invalid log file format");
      }
      SwingUtilities.invokeLater(new Runnable() {
        public void run() {
          destroyDialog();
        }
      });

    } catch (RuntimeException e) {
      destroyDialog();
      displayError("Error - Invalid log file format.\nPlease see documentation"
          + " on how to load log files.");
    } catch (IOException e) {
      destroyDialog();
      displayError("Error - Unable to load log file!");
    }

    _in = null;