Matching a field (exception name) on the second line in a multiline event

(Ilhami Visne) #1

I have multiline Log4J logs. The lines after the first are optional. The first line contains the log message and after that is the stacktrace. I want to extract the top exception class name on a stacktrace and it is the first part on the second line. An example:

2016-01-18 13:19:34,812 [myScheduler-4] ERROR Hibernate operation: could not load an entity: [];.
java.sql.SQLException: Connection has already been closed.
	at org.apache.tomcat.jdbc.pool.ProxyConnection.invoke(
	at com.sun.proxy.$Proxy47.prepareStatement(Unknown Source)
	at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(

I'm interested on the 'java.sql.SQLException' part. I wrote the following grok pattern:

LOG4J_LOG %{LOG4J_DATESTAMP:timestamp} \[%{GREEDYDATA:thread}\] {LOGLEVEL:level} %{JAVACLASS:class}\-%{SPACE}%{JAVALOGMESSAGE:logmessage}%{SPACE}(^%{JAVACLASS:exception})?

(^%{JAVACLASS:exception})? is the part, which should match the next line. It matches as expected on the but i can't get it working with the logstash 2.1.1 version. All other fields are extracted successfully.

This is the logstash conf file:

input {
 file {
	path => "D:/projects/ELK/localhost.log"
	start_position => beginning
	codec => multiline {
	  patterns_dir => "../patterns"
      pattern => "^%{LOG4J_DATESTAMP}"
      negate => true
      what => "previous"

filter {
 grok {
    match => { "message" => "%{LOG4J_LOG}"}

output { 
  elasticsearch { }

My guess is that logmessage ( JAVALOGMESSAGE (.*) ) matches everything up to the end and therefore there is nothing left to match. But my questions are how is it working on the and how to achieve this with logstash?


(Magnus Bäck) #2

I suspect you have to start the regexp with (?m) so that newline characters don't count as the end of the string.

(Ilhami Visne) #3

i think (?m) does just the opposite. Via this mode, dot (.) starts to match the newline too. See this please

(Magnus Bäck) #4

Ah, I didn't see that you were trying to use ^ to match the beginning of the next line. In that case I don't know.

(system) #5