# Grok filter by source

**URL:** <https://discuss.elastic.co/t/grok-filter-by-source/252623>\
**Category:** Logstash\
**Created:** [October 20, 2020, 2:48am UTC](https://discuss.elastic.co/t/grok-filter-by-source/252623 "2020-10-20T02:48:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![andi](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@andi](https://discuss.elastic.co/u/andi)\
**Post date:** [October 20, 2020, 2:48am UTC](https://discuss.elastic.co/t/grok-filter-by-source/252623/1 "2020-10-20T02:48:35Z")

</div>

Hi All,

Sorry i'm new on ELK, currently i'm use ELK 6.8.10, it's working properly, with filter by tags,  
this is my grok.

```auto
filter {
   if "nameservice-out-staging" in [tags] {
    grok {
      match => { "message" => "%{WORD:type}\]\[METHOD\]%{WORD:method}\[URL\]%{URIPATHPARAM:url}\[STATUS\]%{NUMBER:respone}\[RESPONSE_TIME\]%{NUMBER:response_time}" }
      remove_field => ["message","@version","beat","offset","prospector","log","fields"]
    }
    mutate {
      convert => {
          "resspone" => "integer"
          "response_time" => "float"
      }
    }
}
}

output {
  if "nameservice-out-staging" in [tags] {
  elasticsearch { hosts => ["ES-HOST:9200"]
    hosts => "ES-HOST:9200"
    manage_template => false
    index => "nameservice-stage-%{+YYYY.MM.dd}"
  }
}
}

```

it's working, but i want to change nameservice-out-staging to by source like  
`if [source] =~ /\/var\/log\/.pm2\/logs\/nameservice-out.log\// {` this is not working,

`if [source] =~ /\/var\/log\/.pm2\/logs\/nameservice-out.log\// {` what is wrong in this regex ?

Thanks

---

<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:** [October 20, 2020, 2:18pm UTC](https://discuss.elastic.co/t/grok-filter-by-source/252623/2 "2020-10-20T14:18:50Z")

</div>

> [@andi](#):
>
> `if [source] =~ /\/var\/log\/.pm2\/logs\/nameservice-out.log\// {` what is wrong in this regex ?

Does your filename really have a trailing / ?

---

<div class="post-metadata">

**Author:** ![andi](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@andi](https://discuss.elastic.co/u/andi)\
**Post date:** [October 21, 2020, 3:03am UTC](https://discuss.elastic.co/t/grok-filter-by-source/252623/3 "2020-10-21T03:03:24Z")

</div>

Hi,

Thanks for your reply,  
`/var/log/.pm2/logs/nameservice-out.log`  
` /var/log/.pm2/logs/nameservice-error.log`  
it's real path log and file log.,  
any idea ?

Thanks

---

<div class="post-metadata">

**Author:** ![fadjar340](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fadjar340/32/43610_2.png) [@fadjar340](https://discuss.elastic.co/u/fadjar340)\
**Post date:** [October 21, 2020, 4:13am UTC](https://discuss.elastic.co/t/grok-filter-by-source/252623/4 "2020-10-21T04:13:11Z")

</div>

Try this

```auto
if [source] =~ /\/var\/log\/.pm2\/logs\/nameservice-out.log/ { 

```

Or even more simple and you can do what ever the name of the file:

in input

```auto
input {
file {
  path => "/var/log/.pm2/logs/nameservice-out.log"
  type => "log"
  tags => ["serviceout"]
  id => "serviceout"
 }

file {
  path => "/var/log/.pm2/logs/nameservice-error.log"
  type => "log"
  tags => ["serviceerror"]
  id => "serviceerror"
 }
}

filter {
    if "serviceout" in [tags] {
    .
    .
   }
  
  if "serviceerror" in [tags] {
  .
  .
  }
}

output {
 .
 .
 .
}

```

Regards,  
Fadjar340

---

<div class="post-metadata">

**Author:** ![andi](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@andi](https://discuss.elastic.co/u/andi)\
**Post date:** [October 21, 2020, 4:49am UTC](https://discuss.elastic.co/t/grok-filter-by-source/252623/5 "2020-10-21T04:49:48Z")

</div>

Hi All,

Thanks for your reply, case solve,

Thank's a lot

---

<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:** [November 18, 2020, 4:50am UTC](https://discuss.elastic.co/t/grok-filter-by-source/252623/6 "2020-11-18T04:50:10Z")

</div>

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