package sample.parser;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.lightysoft.logmx.business.ParsedEntry;
import com.lightysoft.logmx.mgr.LogFileParser;
/**
* Sample LogMX Parser able to parse a log file with multi-line support and Absolute Date support.
* Here is an example of log file suitable for this parser:
*
* 2010-09-23 18:13:56,469 INFO MxDrive:Y: sajet_motion.MotionController.driveActionDone: driveName = [Y]
* 2010-09-23 18:13:56,469 INFO MxDrive:Y: sajet_motion.MotionController.driveActionDone: action = [move]
* 2010-09-23 18:13:56,589 INFO MxDrive:S: drive_maxon.MxDrive:S.executeMoveToReferencePosition
* 2010-09-23 18:13:56,619 INFO MxDrive:S: drive_maxon.MxDrive:S.enterStateOperationEnable: maxon state = ON
* 2010-09-23 18:13:56,619 INFO MxDrive:S: drive_maxon.MxDrive:S.enterStateOperationEnable: write parameters
*/
public class SampleParser2 extends LogFileParser {
/** Current parsed log entry */
private ParsedEntry entry = null;
/** Entry date format */
private final static SimpleDateFormat DATE_FORMAT = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss,SSS");
/** Pattern for entry begin */
private final static Pattern ENTRY_BEGIN_PATTERN = Pattern
.compile("^(\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2},\\d{1,3}) (\\S+?) (\\S+?) (\\S+?)(?: (.*?))?$");
/** Buffer for Entry message (improves performance for multi-lines entries) */
private StringBuilder entryMsgBuffer = null;
/** Key of user-defined field "Controller" */
private static final String EXTRA_FIELD_KEY = "Controller";
/** User-defined fields names (here, only one) */
private static final List EXTRA_FIELDS_KEYS = Arrays.asList(EXTRA_FIELD_KEY);
/**
* Returns the name of this parser
* @see com.lightysoft.logmx.mgr.LogFileParser#getParserName()
*/
public String getParserName() {
return "Sample Parser";
}
/**
* Returns the supported file type for this parser
* @see com.lightysoft.logmx.mgr.LogFileParser#getSupportedFileType()
*/
public String getSupportedFileType() {
return "LogMX sample log files";
}
/**
* Process the new line of text read from file
* @see com.lightysoft.logmx.mgr.LogFileParser#parseLine(java.lang.String)
*/
protected void parseLine(String line) throws Exception {
// If end of file, records last entry if necessary, and exits
if (line == null) {
recordPreviousEntryIfExists();
return;
}
Matcher matcher = ENTRY_BEGIN_PATTERN.matcher(line);
if (matcher.matches()) {
// Record previous found entry if exists, then create a new one
prepareNewEntry();
entry.setDate(matcher.group(1));
entry.setLevel(matcher.group(2));
entry.getUserDefinedFields().put(EXTRA_FIELD_KEY, matcher.group(3)); // save entry Controller
entry.setEmitter(matcher.group(4));
String msg = matcher.group(5);
if (msg != null) {
entryMsgBuffer.append(matcher.group(5));
}
} else if (entry != null) {
entryMsgBuffer.append('\n').append(line); // appends this line to previous entry's text
}
}
/**
* Returns the ordered list of user-defined fields to display (given by their key), for each entry.
* @see com.lightysoft.logmx.mgr.LogFileParser#getUserDefinedFields()
*/
@Override
public List getUserDefinedFields() {
return EXTRA_FIELDS_KEYS;
}
/**
* Returns a relative Date for the given entry
* @see com.lightysoft.logmx.mgr.LogFileParser#getRelativeEntryDate(com.lightysoft.logmx.business.ParsedEntry)
*/
public Date getRelativeEntryDate(ParsedEntry pEntry) throws Exception {
return getAbsoluteEntryDate(pEntry);
}
/**
* Returns the absolute Date for the given entry
* @see com.lightysoft.logmx.mgr.LogFileParser#getAbsoluteEntryDate(com.lightysoft.logmx.business.ParsedEntry)
*/
public Date getAbsoluteEntryDate(ParsedEntry pEntry) throws Exception {
return DATE_FORMAT.parse(pEntry.getDate()); // (the right-part "T0+..." will be ignored by the formatter)
}
/**
* Send to LogMX the current parsed log entry
* @throws Exception
*/
private void recordPreviousEntryIfExists() throws Exception {
if (entry != null) {
entry.setMessage(entryMsgBuffer.toString());
addEntry(entry);
}
}
/**
* Send to LogMX the current parsed log entry, then create a new one
* @throws Exception
*/
private void prepareNewEntry() throws Exception {
recordPreviousEntryIfExists();
entry = createNewEntry();
entryMsgBuffer = new StringBuilder(80);
entry.setUserDefinedFields(new HashMap(1)); // Create an empty Map with only one element allocated
}
}