# Parsing variable multiline text from event log

**URL:** <https://discuss.elastic.co/t/parsing-variable-multiline-text-from-event-log/269450>\
**Category:** Logstash\
**Created:** [April 7, 2021, 11:36am UTC](https://discuss.elastic.co/t/parsing-variable-multiline-text-from-event-log/269450 "2021-04-07T11:36:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SimonR](https://avatars.discourse-cdn.com/v4/letter/s/6f9a4e/32.png) [@SimonR](https://discuss.elastic.co/u/SimonR)\
**Post date:** [April 7, 2021, 11:36am UTC](https://discuss.elastic.co/t/parsing-variable-multiline-text-from-event-log/269450/1 "2021-04-07T11:36:38Z")

</div>

Hi Logstash experts.  
Im having big issues parsing MSSQL logs from the Windows event log, and i hope someone can assist.

MSSQL logs event id 33205 to Windows event log, in a somewhat structured format, except the "Statement" field containing the SQL query.

Raw Event id looks like this:

```
server_principal_name:domain.local\username
server_principal_sid:123456789123456798123456
database_principal_name:test1
server_instance_name:coolServerName
database_name:coolDatabaseName
statement:with info as(
		select test123.*,
				lu.test as hellohello,
				lu.hhy as whatIsThisEven,
		gtinInf.GTIN,
		left(case when HelloAgain.cleansedRow is not null and HelloAgain.cleansedRow not in (select * from #stopwords) then HelloAgain.cleansedRow end, 1000) as Party123,
additional_information:someMoreInfoHere

```

By default, logstash does not know how to parse and puts everything in message field.

My first solution was to use KV filter to specify ":" as value split, and "\n" as field split.  
This works, until it meets a multiline log.  
I made a Regex that is able to capture the multiline SQL statement, but i feel like i still need to use KV filter for all the other lines, where KV filter works fine.  
Am i able to use KV filter until a sudden point, and then explicitly specify the statement field using my Regex?  
I have been though the Filter documentation, and KV or Gsub seems to be the way, but im not sure.

Any sugestions would be appereciated.

Note: Because the "statement" field changes ALOT depending on what queries are run, i cannot statically parse it, i think. If im approaching this wrong, please let me know

Current KV filter:

```
filter {
  if [event][code] == 33205 {
  kv {
  field_split => "\n"
  value_split => ":"
  remove_field => ["{param}", "my_extraneous_field"]
  }
 }

```

}

Best regards

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [April 7, 2021, 5:31pm UTC](https://discuss.elastic.co/t/parsing-variable-multiline-text-from-event-log/269450/2 "2021-04-07T17:31:25Z")

</div>

I spent 20 minutes writing a hideous ruby filter (which did not even work quite correctly) and then I realized that you can

```
    mutate { gsub => ["message", "\n\s+", " "] }
    kv { source => "[message]" value_split => ":" field_split_pattern => "\n" }

```

to get

```
   "server_principal_sid" => "123456789123456798123456",
              "statement" => "with info as( select test123.*, lu.test as hellohello, lu.hhy as whatIsThisEven, gtinInf.GTIN, left(case when HelloAgain.cleansedRow is not null and HelloAgain.cleansedRow not in (select * from #stopwords) then HelloAgain.cleansedRow end, 1000) as Party123,",
   "server_instance_name" => "coolServerName",
 "additional_information" => "someMoreInfoHere",
```

---

<div class="post-metadata">

**Author:** ![SimonR](https://avatars.discourse-cdn.com/v4/letter/s/6f9a4e/32.png) [@SimonR](https://discuss.elastic.co/u/SimonR)\
**Post date:** [April 8, 2021, 9:36am UTC](https://discuss.elastic.co/t/parsing-variable-multiline-text-from-event-log/269450/3 "2021-04-08T09:36:33Z")

</div>

Badger, you are truly an angel! I tested the parsing, and so far so good. All kinds of weirds statements gets parsed fine.

Thanks for your time, and effort!

---

<div class="post-metadata">

**Author:** ![SimonR](https://avatars.discourse-cdn.com/v4/letter/s/6f9a4e/32.png) [@SimonR](https://discuss.elastic.co/u/SimonR)\
**Post date:** [April 9, 2021, 11:35am UTC](https://discuss.elastic.co/t/parsing-variable-multiline-text-from-event-log/269450/4 "2021-04-09T11:35:13Z")

</div>

UPDATE:

LS parses ALMOST everything correct with the config @Badger provided, thanks!

However i come to realize that not all of the statements contains newline followed by tab.

A few statements that are run pretty often looks something like this

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 7, 2021, 11:35am UTC](https://discuss.elastic.co/t/parsing-variable-multiline-text-from-event-log/269450/5 "2021-05-07T11:35:22Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
