# Pattern matching issues in Logstash

**URL:** <https://discuss.elastic.co/t/pattern-matching-issues-in-logstash/284255>\
**Category:** Logstash\
**Created:** [September 15, 2021, 7:43am UTC](https://discuss.elastic.co/t/pattern-matching-issues-in-logstash/284255 "2021-09-15T07:43:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vajb12](https://avatars.discourse-cdn.com/v4/letter/v/bbe5ce/32.png) [@Vajb12](https://discuss.elastic.co/u/Vajb12)\
**Post date:** [September 15, 2021, 7:43am UTC](https://discuss.elastic.co/t/pattern-matching-issues-in-logstash/284255/1 "2021-09-15T07:43:39Z")

</div>

I'm having issues with Pattern matching with Logstash.

Sample log pattern

```
[DEBUG] 2021-09-13T23:58:24.361 [http-nio-8080-exec-1] [FB-3D] localhost - [i.i.i.a.f.AuthFilter] :: doFilter :: formName B-3D

```

Grok Pattern that works

```
\s?\[%{DATA:loglevel}\] %{TIMESTAMP_ISO8601:logts} \[%{DATA:threadname}\] \[?%{DATA:formname}\] %{DATA:podname} %{DATA:filler1} \[%{DATA:classname}\] %{GREEDYDATA:fullmesg}

```

For the sample log mentioned above, the above grok pattern works fine. But I have some log files where the fourth field does not exist 'not even the empty . I want to know how to handle the same.

Sample log (which is not working using the above pattern)

```
[DEBUG] 2021-09-13T23:58:22.633 [http-nio-8080-exec-1] localhost - [i.i.i.a.f.AuthFilter] :: Requested going to check the

```

In the above case, the fourth field [?%{DATA:formname}] does not exist. With the optional condition included in the above grok pattern for formname, it still does not work. It expects the presence of an empty field. Is there a way to make the 4th field optional?. I.e pattern to accomodate even if the field does not exist.

Any help on this is much appreciated.  
Thanks in Advance

---

<div class="post-metadata">

**Author:** ![Cad](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cad/32/86661_2.png) [@Cad](https://discuss.elastic.co/u/Cad)\
**Post date:** [September 15, 2021, 10:27am UTC](https://discuss.elastic.co/t/pattern-matching-issues-in-logstash/284255/2 "2021-09-15T10:27:13Z")

</div>

Hi,

To make a part of your pattern optional, you have to print it like this `(%{optional})?`  
In your case, it give you somtehing like this:

```auto
\s?\[%{DATA:loglevel}\] %{TIMESTAMP_ISO8601:logts} \[%{DATA:threadname}\] (\[?%{DATA:formname}\] )?%{DATA:podname} %{DATA:filler1} \[%{DATA:classname}\] %{GREEDYDATA:fullmesg}

```

Cad.

---

<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:** [October 13, 2021, 10:27am UTC](https://discuss.elastic.co/t/pattern-matching-issues-in-logstash/284255/3 "2021-10-13T10:27:15Z")

</div>

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