# Logstash Filter If loop

**URL:** <https://discuss.elastic.co/t/logstash-filter-if-loop/352119>\
**Category:** Logstash\
**Created:** [January 30, 2024, 6:53pm UTC](https://discuss.elastic.co/t/logstash-filter-if-loop/352119 "2024-01-30T18:53:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![adityak248](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@adityak248](https://discuss.elastic.co/u/adityak248)\
**Post date:** [January 30, 2024, 6:53pm UTC](https://discuss.elastic.co/t/logstash-filter-if-loop/352119/1 "2024-01-30T18:53:21Z")

</div>

Which one is correct:

```auto
filter {
    if [kubernetes_labels][app] == "app-name" and [kubernetes_container_name] == "nginx" {
        grok {
            match => {"message" => 'XXX'}}}
    else if [kubernetes_labels][app] == "app-name" and [kubernetes_container_name] == "app" {
        grok {
            match => {"message" => "XXX"}}}
}

```

or

```auto
filter {
    if [kubernetes_labels][app] == "app-name" {
        if [kubernetes_container_name] == "nginx" {
            grok {
                match => {"message" => 'XXX'}}
        }
        else if [kubernetes_container_name] == "app" { 
            grok {
                match => {"message" => "XXX"}}
        }
    }
}

```

or

```auto
filter {
    if [kubernetes_labels][app] == "app-name" {
        if [kubernetes_container_name] == "nginx" {
            grok {
                match => {"message" => 'XXX'}}
        }
        if [kubernetes_container_name] == "app" {
            grok {
                match => {"message" => "XXX"}}
        }
    }
}

```

I know case 1 works fine but am trying to fine tuning the filters and wanted to check with community.  
There is no much documentation on if loops.  
Please help. Thanks

---

<div class="post-metadata">

**Author:** ![leandrojmp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leandrojmp/32/107231_2.png) [@leandrojmp](https://discuss.elastic.co/u/leandrojmp)\
**Post date:** [January 30, 2024, 7:50pm UTC](https://discuss.elastic.co/t/logstash-filter-if-loop/352119/2 "2024-01-30T19:50:53Z")

</div>

All of them are correct and will work, which one is better depend users preference.

All the documentation needed for if conditionals in Logstash is [here](https://www.elastic.co/guide/en/logstash/current/event-dependent-configuration.html#conditionals).

---

<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:** [February 27, 2024, 7:51pm UTC](https://discuss.elastic.co/t/logstash-filter-if-loop/352119/3 "2024-02-27T19:51:31Z")

</div>

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