# Logstash grok pattern - optional fields

**URL:** <https://discuss.elastic.co/t/logstash-grok-pattern-optional-fields/68880>\
**Category:** Logstash\
**Created:** [December 13, 2016, 2:23pm UTC](https://discuss.elastic.co/t/logstash-grok-pattern-optional-fields/68880 "2016-12-13T14:23:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![txisme](https://avatars.discourse-cdn.com/v4/letter/t/dbc845/32.png) [@txisme](https://discuss.elastic.co/u/txisme)\
**Post date:** [December 13, 2016, 2:23pm UTC](https://discuss.elastic.co/t/logstash-grok-pattern-optional-fields/68880/1 "2016-12-13T14:23:58Z")

</div>

Hey,

im using Logstash 2.4 and i need to process log files so i am using grok patterns. I am having trouble with optional fields, i have several logs wich can have or not have a specific field, in this case "correlationID"

Logs example:

> ```
> 2016-12-13T11:08:07,370 DEBUG [main|tc.im.pm.PMModuleController] [PM] Loaded.
> 2016-12-13T11:08:10,194 INFO ce75b152-7c2d-4489-83f2-cb0cda7ded9a [im-trip konsumer|tc.controller.api.ControllerHandler] State change is for myself. Checking if I need to do anything...
> 
> ```

Grok pattern used:

> (?m)%{TIMESTAMP\_ISO8601:time}\s%{WORD:loglevel}\s\s(%{corID:correlationID})?\s%{GREEDYDATA:value}

grok pattern for correlationID used:

> corID ([^\s]+)

What happens is that in the first log, where no correlationID exists, this grok pattern is putting "[main|tc.im.pm.PMModuleController]" in the correlationID field.

Anyone knows how to do this so it puts the correlationID empty in the first log?

---

<div class="post-metadata">

**Author:** ![txisme](https://avatars.discourse-cdn.com/v4/letter/t/dbc845/32.png) [@txisme](https://discuss.elastic.co/u/txisme)\
**Post date:** [December 13, 2016, 2:39pm UTC](https://discuss.elastic.co/t/logstash-grok-pattern-optional-fields/68880/2 "2016-12-13T14:39:33Z")

</div>

Dont know why but a lot of times i organize my thoughts to post my doubts here, things get much clearer 😃

i managed to solve this by changing the corID grok pattern to ([^\s[]]+)

Thanks

---

<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:** [January 10, 2017, 2:39pm UTC](https://discuss.elastic.co/t/logstash-grok-pattern-optional-fields/68880/3 "2017-01-10T14:39:44Z")

</div>

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