# Is there a way to tag for different grok matches?

**URL:** https://discuss.elastic.co/t/is-there-a-way-to-tag-for-different-grok-matches/52785
**Category:** Logstash
**Created:** [June 14, 2016, 10:21pm UTC](https://discuss.elastic.co/t/is-there-a-way-to-tag-for-different-grok-matches/52785 "2016-06-14T22:21:56Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Josh\_Reichardt](https://avatars.discourse-cdn.com/v4/letter/j/f08c70/32.png) [@Josh\_Reichardt](https://discuss.elastic.co/u/Josh_Reichardt)
#### Post date: [June 14, 2016, 10:21pm UTC](https://discuss.elastic.co/t/is-there-a-way-to-tag-for-different-grok-matches/52785/1 "2016-06-14T22:21:56Z")

</div>

I am using the GELF Docker driver to send logs to Logstash. It has been working well so far but I am having trouble finding a good way to separate nginx access logs and nginx error logs. I was thinking that I would just add a take for each match like the following:

```auto
  if [image_name] =~ /nginx/ {
    grok {
        match => ["message", "%{INT:status} - %{NOTSPACE:referrer} - %{NOTSPACE:domain} - %{IPORHOST:clientip} - %{QS:request} %{INT:body_bytes_sent} %{QS:http_referer}"]
        add_tag => ["nginx_access"]
    }
    grok {
        match => ["message", "(?<timestamp>%{YEAR}[./]%{MONTHNUM}[./]%{MONTHDAY} %{TIME}) \[%{LOGLEVEL:severity}\] %{POSINT:pid}#%{NUMBER}: \*%{NUMBER:tid} %{GREEDYDATA:errormessage}, client: %{IP:client}" ]
        add_tag => ["nginx_err"]
    }
  }

```

This matches the first pattern, adds the tag but then fails on the second pattern and I end up with a `_grokparsefailure`. What's a good way to match and tag access/error logs correctly with GELF?

---

<div class="post-metadata">

### Author: ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)
#### Post date: [June 15, 2016, 5:55am UTC](https://discuss.elastic.co/t/is-there-a-way-to-tag-for-different-grok-matches/52785/2 "2016-06-15T05:55:08Z")

</div>

Wrap all but the first grok filter in a conditional so that the subsequent ones are only tried if there hasn't been a match earlier.

```nohighlight
grok {
  ...
}
if "_grokparsefailure" in [tags] {
  grok {
    ...
    remove_tag => ["_grokparsefailure"]
  }
}

```

---

<div class="post-metadata">

### Author: ![Josh\_Reichardt](https://avatars.discourse-cdn.com/v4/letter/j/f08c70/32.png) [@Josh\_Reichardt](https://discuss.elastic.co/u/Josh_Reichardt)
#### Post date: [June 15, 2016, 10:37pm UTC](https://discuss.elastic.co/t/is-there-a-way-to-tag-for-different-grok-matches/52785/3 "2016-06-15T22:37:15Z")

</div>

Thank, I'll give this a try. Will this work for a scenario where I have many grok patterns to try and many different tags to apply?

---

<div class="post-metadata">

### Author: ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)
#### Post date: [June 16, 2016, 6:17am UTC](https://discuss.elastic.co/t/is-there-a-way-to-tag-for-different-grok-matches/52785/4 "2016-06-16T06:17:17Z")

</div>

Sure, it'll work. Matching lots and lots of regexps against all messages is of course computationally expensive so you'll want to order your expressions wisely.

---

<div class="post-metadata">

### Author: ![Josh\_Reichardt](https://avatars.discourse-cdn.com/v4/letter/j/f08c70/32.png) [@Josh\_Reichardt](https://discuss.elastic.co/u/Josh_Reichardt)
#### Post date: [June 16, 2016, 7:06pm UTC](https://discuss.elastic.co/t/is-there-a-way-to-tag-for-different-grok-matches/52785/5 "2016-06-16T19:06:24Z")

</div>

Makes sense. Thanks again.

---

<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:52am UTC](https://discuss.elastic.co/t/is-there-a-way-to-tag-for-different-grok-matches/52785/6 "2017-07-06T04:52:27Z")

</div>


