2017-03-25

Trying to parse key value log lines in go

I want to build a honeytail parser that will take log lines that look like

key1=val1 key2=val2 key3=val3...

There are a few additional constraints

  • if the value is quoted, I'd like it to have the quotes stripped
  • if the value has an equals sign in it, that's cool
  • if the value is multiple words, that's fine even if it's not quoted.  (I won't go so far as to say that unquoted space separated strings should be allowed to have equals signs embedded.)


I found a library on the net to do this: github.com/kr/logfmt.

Here is the code playing with it

Unfortunately, it had a few other ideas.

  • a bare word (no equals sign attached) should be treated as a key with no value
  • equals signs embedded in strings should be treated as separators
You can see these rules coming through in test cases 2 and 4 in the output

Ah well.  It was an interesting attempt.


(one test case that's missing: does a number in quotes turn in to a number (float or int) or stay a string?)

1 comment:

billgo said...

Numbers in quotes should remain in quotes and be treated as a string not an int.. It might be a user id or an id number of some sort.. IMO..

-- bill duncan