# Clarification on if/else behavior in output

**URL:** <https://discuss.elastic.co/t/clarification-on-if-else-behavior-in-output/200617>\
**Category:** Logstash\
**Created:** [September 23, 2019, 3:28am UTC](https://discuss.elastic.co/t/clarification-on-if-else-behavior-in-output/200617 "2019-09-23T03:28:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![TechGeekNZ](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/techgeeknz/32/54697_2.png) [@TechGeekNZ](https://discuss.elastic.co/u/TechGeekNZ)\
**Post date:** [September 23, 2019, 3:28am UTC](https://discuss.elastic.co/t/clarification-on-if-else-behavior-in-output/200617/1 "2019-09-23T03:28:10Z")

</div>

Heya all,

I want to confirm whether the particular behavior i'm seeing is normal and if there is another approach I should take towards my config.

Situation is that I have multiple inputs on a single pipeline, and am adding a field to the inputs based on the log type so I can process filters accordingly (time/date format, grok, etc)

Based on the same fields, I output using if/else to go to different indexes. Example of configuration below;

```
output {
  if [fields][log_type] == "type1" {
elasticsearch {
  hosts => ["http://10.250.11.22:9200"]
  index => "type1-%{+YYYY.MM.dd}"
}
  }

  if [fields][log_type] == "type2" {
elasticsearch {
  hosts => ["http://10.250.11.22:9200"]
  index => "type2-%{+YYYY.MM.dd}"
}
  }

  if [fields][log_type] == "type3" {
elasticsearch {
  hosts => ["http://10.250.11.22:9200"]
  index => "type3-%{+YYYY.MM.dd}"
}
  }

  if [fields][log_type] == "type4" {
elasticsearch {
  hosts => ["http://10.250.11.22:9200"]
  index => "type4-%{+YYYY.MM.dd}"
}
  }

  else {
elasticsearch {
  hosts => ["http://10.250.11.22:9200"]
  index => "%{[@metadata][beat]}-%{[@metadata][version]}-%{+YYYY.MM.dd}"
}
  }
}

```

This works well, however I am getting all of the logs outputting to the index specified in else as a secondary (ie. filebeat-7.3.1-2019.09.23)

Ideally, I only want the else to be used in the case that there is no output type specified for my logs. Is this possible?

---

<div class="post-metadata">

**Author:** ![harshbajaj16](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/harshbajaj16/32/44970_2.png) [@harshbajaj16](https://discuss.elastic.co/u/harshbajaj16)\
**Post date:** [September 23, 2019, 4:01am UTC](https://discuss.elastic.co/t/clarification-on-if-else-behavior-in-output/200617/2 "2019-09-23T04:01:40Z")

</div>

Hi @TechGeekNZ,

You need to add if and else if condition. Please try it with below format and let me know in case of any issue you are facing.

```
if ... {
  ...
} else if ... {
  ...
} else {
  ...
} 

```

Regards,  
Harsh Bajaj

---

<div class="post-metadata">

**Author:** ![TechGeekNZ](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/techgeeknz/32/54697_2.png) [@TechGeekNZ](https://discuss.elastic.co/u/TechGeekNZ)\
**Post date:** [September 23, 2019, 4:23am UTC](https://discuss.elastic.co/t/clarification-on-if-else-behavior-in-output/200617/3 "2019-09-23T04:23:01Z")

</div>

Great,

Thanks for the Harsh. It worked a treat. The penny should have dropped on that one, however what I had tried was doing an 'else if' on the final output and of course the configuration file wouldn't load 🙂

Cheers

---

<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 21, 2019, 4:23am UTC](https://discuss.elastic.co/t/clarification-on-if-else-behavior-in-output/200617/4 "2019-10-21T04:23:05Z")

</div>

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