# How to account for different number of blank spaces in Log4j logs?

**URL:** https://discuss.elastic.co/t/how-to-account-for-different-number-of-blank-spaces-in-log4j-logs/53580
**Category:** Logstash
**Created:** [June 21, 2016, 9:39pm UTC](https://discuss.elastic.co/t/how-to-account-for-different-number-of-blank-spaces-in-log4j-logs/53580 "2016-06-21T21:39:25Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![ZillaG](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zillag/32/10505_2.png) [@ZillaG](https://discuss.elastic.co/u/ZillaG)
#### Post date: [June 21, 2016, 9:39pm UTC](https://discuss.elastic.co/t/how-to-account-for-different-number-of-blank-spaces-in-log4j-logs/53580/1 "2016-06-21T21:39:25Z")

</div>

I have the following log4j logs

```
2016-04-22 16:43:25,172 ERROR :SomeThread : 2 [com.mycompany.SomeClass] The Log Message.
2016-06-11 01:22:40,894 INFO :SomeThread1 : 0 [com.mycompany.SomeClass1] The Log message

```

I have the following Grok filter

```
filter {
    mutate {
      strip => "message"
    }
    grok {
      match => {
        "message" => "%{TIMESTAMP_ISO8601:logdate} %{LOGLEVEL:loglevel} :%{DATA:thread} : %{NUMBER:thread_pool} \[(?<classname>[^\]]+)\] %{SPACE} %{GREEDYDATA:msgbody}"
      }
    }
    date {
      match => ["logdate", "yyyy-MM-dd HH:mm:ss,SSS", "ISO8601"]
    }
}

```

However it only works for the ERROR log and not the INFO log, since ERROR takes up 5 spaces, and INFO 4 spaces, so there is ONE space between ERROR and :SomeThread, whereas there are TWO spaces between INFO and :SomeThread1.

How can I account for the different number of spaces between fields when writing Grok patterns?

---

<div class="post-metadata">

### Author: ![marke72](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marke72/32/10832_2.png) [@marke72](https://discuss.elastic.co/u/marke72)
#### Post date: [June 21, 2016, 9:59pm UTC](https://discuss.elastic.co/t/how-to-account-for-different-number-of-blank-spaces-in-log4j-logs/53580/2 "2016-06-21T21:59:54Z")

</div>

I was actually messing with some extra spaces in grok today. My logs varied between 1 to 5 spaces and instead of putting actual spaces I used `\s+` which is the regular expression for 1 or more spaces.  
Try putting `\s+` at the end of `%{LOGLEVEL:loglevel}` and see if that does the trick.

---

<div class="post-metadata">

### Author: ![ZillaG](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zillag/32/10505_2.png) [@ZillaG](https://discuss.elastic.co/u/ZillaG)
#### Post date: [June 21, 2016, 10:02pm UTC](https://discuss.elastic.co/t/how-to-account-for-different-number-of-blank-spaces-in-log4j-logs/53580/3 "2016-06-21T22:02:50Z")

</div>

Using %{SPACE} did the trick for me. So here's the filter

`%{TIMESTAMP_ISO8601:logdate}%{SPACE}%{LOGLEVEL:loglevel}%{SPACE}:%{DATA:thread}%{SPACE}:%{SPACE}%{NUMBER:thread_pool}%{SPACE}\[(?<classname>[^\]]+)\]%{SPACE}%{GREEDYDATA:msgbody}`

I'll try @marke72 idea too.

---

<div class="post-metadata">

### Author: ![marke72](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marke72/32/10832_2.png) [@marke72](https://discuss.elastic.co/u/marke72)
#### Post date: [June 21, 2016, 10:08pm UTC](https://discuss.elastic.co/t/how-to-account-for-different-number-of-blank-spaces-in-log4j-logs/53580/4 "2016-06-21T22:08:13Z")

</div>

They both should work. `%{SPACE}` is just `\s*` I just use `\s+` because I'm expecting at least one space.  
Here's a reference [https://github.com/logstash-plugins/logstash-patterns-core/blob/master/patterns/grok-patterns](https://github.com/logstash-plugins/logstash-patterns-core/blob/master/patterns/grok-patterns)

---

<div class="post-metadata">

### Author: ![ZillaG](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zillag/32/10505_2.png) [@ZillaG](https://discuss.elastic.co/u/ZillaG)
#### Post date: [June 21, 2016, 10:14pm UTC](https://discuss.elastic.co/t/how-to-account-for-different-number-of-blank-spaces-in-log4j-logs/53580/5 "2016-06-21T22:14:08Z")

</div>

Yep makes sense now. I'm new to this too.

---

<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: [July 6, 2017, 4:51am UTC](https://discuss.elastic.co/t/how-to-account-for-different-number-of-blank-spaces-in-log4j-logs/53580/6 "2017-07-06T04:51:22Z")

</div>


