# Conditional mutate not working

**URL:** <https://discuss.elastic.co/t/conditional-mutate-not-working/101821>\
**Category:** Logstash\
**Created:** [September 26, 2017, 10:10am UTC](https://discuss.elastic.co/t/conditional-mutate-not-working/101821 "2017-09-26T10:10:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pkaramol](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pkaramol/32/22610_2.png) [@pkaramol](https://discuss.elastic.co/u/pkaramol)\
**Post date:** [September 26, 2017, 10:10am UTC](https://discuss.elastic.co/t/conditional-mutate-not-working/101821/1 "2017-09-26T10:10:35Z")

</div>

I am logging to more than one ports using syslog plugin.

However the following conditional filter does not work (although logs are received in more than one port as expected.

Any suggestions?

```
input {
  tcp {
    port => 5151
    type => syslog
  }
  tcp {
    port => 5152
    type => syslog
  }
}

filter {
    if [port] == 5151 {
        mutate {
            add_field => {'received_from' => 'service1'}
        }
    }
    if [port] == 5152 {
        mutate {
            add_field => {'received_from' => 'service2'}
        }
    }
}

output {
  elasticsearch { hosts => ["localhost:9200"] }
  stdout { codec => rubydebug }
}
```

---

<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:** [September 26, 2017, 10:30am UTC](https://discuss.elastic.co/t/conditional-mutate-not-working/101821/2 "2017-09-26T10:30:37Z")

</div>

The tcp input doesn't create a `port` field on incoming events, but you can configure it to add such a field by putting an `add_field` declaration in each tcp input.

---

<div class="post-metadata">

**Author:** ![pkaramol](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pkaramol/32/22610_2.png) [@pkaramol](https://discuss.elastic.co/u/pkaramol)\
**Post date:** [September 26, 2017, 10:35am UTC](https://discuss.elastic.co/t/conditional-mutate-not-working/101821/3 "2017-09-26T10:35:15Z")

</div>

Many thanks, now it works (no need for filter I guess):

```
input {
  tcp {
    port => 5151
    type => syslog
    add_field => {'received_from' => 'service1'}
  }
  tcp {
    port => 5152
    type => syslog
    add_field => {'received_from' => 'service2'}
  }
}

output {
  elasticsearch { hosts => ["localhost:9200"] }
  stdout { codec => rubydebug }
}
```

---

<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 24, 2017, 10:35am UTC](https://discuss.elastic.co/t/conditional-mutate-not-working/101821/4 "2017-10-24T10:35:21Z")

</div>

Closed on October 24, 2017, 10:35am UTC

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