# Grok Parsing for multiple patterns in single log file

**URL:** <https://discuss.elastic.co/t/grok-parsing-for-multiple-patterns-in-single-log-file/302948>\
**Category:** Logstash\
**Created:** [April 21, 2022, 4:35pm UTC](https://discuss.elastic.co/t/grok-parsing-for-multiple-patterns-in-single-log-file/302948 "2022-04-21T16:35:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![venkatkumar229](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/venkatkumar229/32/104663_2.png) [@venkatkumar229](https://discuss.elastic.co/u/venkatkumar229)\
**Post date:** [April 21, 2022, 4:35pm UTC](https://discuss.elastic.co/t/grok-parsing-for-multiple-patterns-in-single-log-file/302948/1 "2022-04-21T16:35:39Z")

</div>

Hi Everyone,

I am writing a grok pattern to parse a log file where i have two types of log lines.

1. 2022-04-13 06:38:24.472 DBG Microsoft.AspNet.server --- Connection Id "Xxxxxxxx" accepted

2. 2022-04-13 06:38:19.330 dd513002748agdv-2-244-avsgg PRAVEEN1234:000000003 DBG Microsoft.AspNet.server --- Health check api health check completed

In 2nd log pattern we have extra 2 strings after datestamp where as we don't have those two strings in 1 pattern.

Can some one please help me in creating a combined grok pattern for this log file.

Grok which i have tried but not working properly.

GROK:  
%{DATESTAMP:date}%{SPACE}(%{NOTSPACE:id}%{SPACE}%{NOTSPACE:user}|%{SPACE})%{SPACE}%{LOGLEVEL:log\_level}%{SPACE}%{NOTSPACE: service}%{SPACE}---%{SPACE}%{GREEDYDATA: response}

O/p:

 ![16505588792834517137422477495454](https://us1.discourse-cdn.com/elastic/original/3X/5/0/5061a549f8af87fdcb40ae53d9aca7e9400772c4.jpeg)

Regards,  
Praveen Kumar

---

<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 21, 2022, 6:26pm UTC](https://discuss.elastic.co/t/grok-parsing-for-multiple-patterns-in-single-log-file/302948/2 "2022-04-21T18:26:46Z")

</div>

> [@venkatkumar229](#):
>
> %{DATESTAMP:date}%{SPACE}(%{NOTSPACE:id}%{SPACE}%{NOTSPACE:user}|%{SPACE})%{SPACE}%{LOGLEVEL:log\_level}%{SPACE}%{NOTSPACE: service}%{SPACE}---%{SPACE}%{GREEDYDATA: response}

Well, I learned something new today! %{SPACE} matches \s\*. That is, zero or more whitespace characters. So "DEBUGINFO" (with no space) will match "%{LOG\_LEVEL}%{SPACE}%{LOG\_LEVEL}".

That can result in "Microsoft.AspNet.server" matching %{NOTSPACE:id}%{SPACE}%{NOTSPACE:user} and user being set to "er"!

It also explains why %{SPACE}(%{NOTSPACE:id}%{SPACE}%{NOTSPACE:user}|%{SPACE})%{SPACE}%{LOGLEVEL:log\_level}, which sometimes reduces to %{SPACE}%{SPACE}%{SPACE}%{LOGLEVEL:log\_level} can match when there is only a single space.

We can fix things by changing some of those %{SPACE} to \s+ (one or more whitespace characters, rather than zero or more). Also you have spaces in %{NOTSPACE: service} and %{GREEDYDATA: response} which does something weird that I do not understand. You need to change

```
%{NOTSPACE: service}

```

to

```
%{NOTSPACE:service}

```

Try

```
"%{DATESTAMP:date}%{SPACE}(%{NOTSPACE:id}\s+%{NOTSPACE:user}\s+)?%{LOGLEVEL:log_level}%{SPACE}%{NOTSPACE:service}%{SPACE}---%{SPACE}%{GREEDYDATA:response}"

```

Also, DBG does not match LOGLEVEL, so you may need a

```
 mutate { gsub => ["message", "DBG", "DEBUG"] }

```

---

<div class="post-metadata">

**Author:** ![venkatkumar229](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/venkatkumar229/32/104663_2.png) [@venkatkumar229](https://discuss.elastic.co/u/venkatkumar229)\
**Post date:** [April 22, 2022, 5:18am UTC](https://discuss.elastic.co/t/grok-parsing-for-multiple-patterns-in-single-log-file/302948/3 "2022-04-22T05:18:15Z")

</div>

Thanks alot Badger...Your solution worked perfectly.

---

<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 20, 2022, 5:18am UTC](https://discuss.elastic.co/t/grok-parsing-for-multiple-patterns-in-single-log-file/302948/4 "2022-05-20T05:18:53Z")

</div>

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