Skip to content

Instantly share code, notes, and snippets.

@caevyn
Last active August 29, 2015 13:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caevyn/9985310 to your computer and use it in GitHub Desktop.
Save caevyn/9985310 to your computer and use it in GitHub Desktop.
From weird log format to hash. (for logstash filter)
log = "DateTime: 23/04/1998\r\nI'm just a message\r\nKey: value\r\nIm a url: http://cats.com\r\nError Description: I'm an error here is my stacktrace\r\n at stuff\r\n at more stuff\r\n\key with no value: \r\nI'm another random message line\r\n--------------------------------------------"
rows = log.split(/\r\n/).partition{|x| x.start_with?('Error Description: ',' at')}
event = {'Error'=>rows[0].join($/).sub('Error Description: ','')}
remove = []
rows[1].each do |i|
i.scan(/(.+): (.*)/) do |x,y|
event[x.delete(' ')] = y
remove.push(i)
end
end
event['message'] = rows[1].reject{|x| x.start_with? *remove, '------'}.join($/)
print event
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment