please help with a simple parser

Here developers can talk about how to write a Parser for LogMX

Moderator: admin

Posts: 4
Joined: Thu Jun 10, 2010 2:36 pm

please help with a simple parser

Postby shemesh » Thu Jun 10, 2010 2:48 pm

i need some help with writing a parser.

this is the format of a log entry:
yyyy-MM-dd HH:mm:ss,SSS | level | emiter | user name | user | class name | method name | message

and this is the code of my parser:

Code: Select all

private ParsedEntry entry = null;

private final static SimpleDateFormat DATE_FORMAT = new SimpleDateFormat(
   "yyyy-MM-dd HH:mm:ss,SSS");

private final static Pattern ENTRY_BEGIN_PATTERN = Pattern
   .compile("^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2},\\d{3}.*$");

private StringBuilder entryMsgBuffer = null;

/** Key of user-defined fields */
private static final String USER_NAME = "User Name";
private static final String USER = "User";
private static final String CALLING_CLASS = "Calling Class";
private static final String CALLING_METHOD = "Calling Method";

private static final List<String> EXTRA_FIELDS_KEYS = Arrays

protected void parseLine(String line) throws Exception {
   //yyyy-MM-dd HH:mm:ss,SSS | level | emiter | user name | user | class name | method name | message
   // If end of file, records last entry if necessary, and exits
   if (line == null) {
   String trimmedLine = line;//.replaceAll("\\s+", "");
   Matcher matcher = ENTRY_BEGIN_PATTERN.matcher(trimmedLine);
   if (matcher.matches()) {
      // Record previous found entry if exists, then create a new one

      String[] fields = trimmedLine.split("\\|");

      entry.getUserDefinedFields().put(USER_NAME, fields[3].trim());
      entry.getUserDefinedFields().put(USER, fields[4].trim());
      entry.getUserDefinedFields().put(CALLING_CLASS, fields[5].trim());
      entry.getUserDefinedFields().put(CALLING_METHOD, fields[6].trim());
   } else if (entry != null) {
      entryMsgBuffer.append('\n').append(trimmedLine); // appends this line to previous entry's text

public List<String> getUserDefinedFields() {

public Date getRelativeEntryDate(ParsedEntry pEntry) throws Exception {
   return DATE_FORMAT.parse(pEntry.getDate());

public Date getAbsoluteEntryDate(ParsedEntry pEntry) throws Exception {
   return DATE_FORMAT.parse(pEntry.getDate());

private void recordPreviousEntryIfExists() throws Exception {
   if (entry != null) {

private void prepareNewEntry() throws Exception {
   entry = createNewEntry();
   entryMsgBuffer = new StringBuilder(80);
   entry.setUserDefinedFields(new HashMap<String, Object>(1)); // Create an empty Map with only one element allocated

so far so good...
sometimes my log entry starts with some empty space at the beginning of a line (i cannot control this).
this is where it is all getting messy.
i tried line = line.trim() ... tried .replaceAll("\\s+", ""); but i only getting errors from LOGMX.

plz help with a solution.

Site Admin
Posts: 418
Joined: Sun Dec 17, 2006 10:30 pm

Re: please help with a simple parser

Postby admin » Thu Jun 10, 2010 7:51 pm


You were close to the solution :wink: , just replace

Code: Select all

String trimmedLine = line;//.replaceAll("\\s+", "");


Code: Select all

String trimmedLine = line.replaceAll("^\\s*(.*)$", "$1");

It will work for any number of spaces, from 0 to N thanks to the '*' in "\\s*" (instead of '+' which means from 1 to N).
Then "$1" will copy the text captured by "(.*)".
To learn more about regular expressions: ... ttern.html

Feel free to let me know if you have any other problem or question.


Return to “Writing a Parser”

Who is online

Users browsing this forum: No registered users and 1 guest