# Use environment variable to control output

**URL:** <https://discuss.elastic.co/t/use-environment-variable-to-control-output/185052>\
**Category:** Logstash\
**Created:** [June 10, 2019, 8:45pm UTC](https://discuss.elastic.co/t/use-environment-variable-to-control-output/185052 "2019-06-10T20:45:22Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![nobound](https://avatars.discourse-cdn.com/v4/letter/n/f07891/32.png) [@nobound](https://discuss.elastic.co/u/nobound)\
**Post date:** [June 10, 2019, 8:45pm UTC](https://discuss.elastic.co/t/use-environment-variable-to-control-output/185052/1 "2019-06-10T20:45:22Z")

</div>

Hi,

From the logstash.conf, I want to control the output based on the environment variable. I have tried the following, but it does not work. I find the use of the environment variable is not consistent throughout the configuration file. Is there any good reference on this?

Thanks  
Kc

```
output {
    ....
    if "${LS_OUTPUT_DEBUG:0}" == 1 {
        # debugging
        file {
            path => "${LOGS_DIR}/output-debug-%{+YYYYMMdd}.log"
            codec => "rubydebug"
        }
    }
```

---

<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:** [June 10, 2019, 9:47pm UTC](https://discuss.elastic.co/t/use-environment-variable-to-control-output/185052/2 "2019-06-10T21:47:15Z")

</div>

> [@nobound](#):
>
> Is there any good reference on this?

Not that I know of. Does it help you to do this?

```
mutate { add_field => { "[@metadata][LS_DEBUG]" => "${LS_DEBUG:0}" "[@metadata][LOGS_DIR]" => "${LOGS_DIR}" } }

```

---

<div class="post-metadata">

**Author:** ![nobound](https://avatars.discourse-cdn.com/v4/letter/n/f07891/32.png) [@nobound](https://discuss.elastic.co/u/nobound)\
**Post date:** [June 11, 2019, 1:35am UTC](https://discuss.elastic.co/t/use-environment-variable-to-control-output/185052/3 "2019-06-11T01:35:24Z")

</div>

Hi Badger.

I don't have an issue with the LOGS\_DIR. Only the LS\_OUTPUT\_DEBUG does not work. I support the use of environment variable for conditionals is not supported

Thanks for the suggestion. It works for the LS\_OUTPUT\_DEBUG now. Wondering if there is a more efficient way.

```
if [@metadata][LS_DEBUG] {
    # debugging
    file {
        path => "${LOGS_DIR}/restccu-debug-%{+YYYYMMdd}.log"
        codec => "rubydebug"
    }
}

```

Say, if I want to use an environment variable to control the filter, does it mean I will have to use mutate with add\_field at the very beginning of the filter section?

---

<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:** [June 11, 2019, 2:04am UTC](https://discuss.elastic.co/t/use-environment-variable-to-control-output/185052/4 "2019-06-11T02:04:42Z")

</div>

> [@nobound](#):
>
> Say, if I want to use an environment variable to control the filter, does it mean I will have to use mutate with add\_field at the very beginning of the filter section?

You can use a single mutate+add\_field to import everything from the environment.

---

<div class="post-metadata">

**Author:** ![nobound](https://avatars.discourse-cdn.com/v4/letter/n/f07891/32.png) [@nobound](https://discuss.elastic.co/u/nobound)\
**Post date:** [June 11, 2019, 1:25pm UTC](https://discuss.elastic.co/t/use-environment-variable-to-control-output/185052/5 "2019-06-11T13:25:08Z")

</div>

I suppose that is the only way. Thanks again. 😄

---

<div class="post-metadata">

**Author:** ![nobound](https://avatars.discourse-cdn.com/v4/letter/n/f07891/32.png) [@nobound](https://discuss.elastic.co/u/nobound)\
**Post date:** [June 11, 2019, 3:36pm UTC](https://discuss.elastic.co/t/use-environment-variable-to-control-output/185052/6 "2019-06-11T15:36:51Z")

</div>

Hmm. I think I might have spoken too soon. It does not actually work. When I set the LS\_OUTPUT\_DEBUG=0, it still generates the output file. I am not certain what Logstash sees the value of this environment variable. Is there any way to debug the configuration file?

---

<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:** [June 11, 2019, 3:54pm UTC](https://discuss.elastic.co/t/use-environment-variable-to-control-output/185052/7 "2019-06-11T15:54:19Z")

</div>

> [@nobound](#):
>
> if [@metadata][LS\_DEBUG] {

That is testing whether the field exists, which it always does.

---

<div class="post-metadata">

**Author:** ![nobound](https://avatars.discourse-cdn.com/v4/letter/n/f07891/32.png) [@nobound](https://discuss.elastic.co/u/nobound)\
**Post date:** [June 11, 2019, 4:17pm UTC](https://discuss.elastic.co/t/use-environment-variable-to-control-output/185052/8 "2019-06-11T16:17:15Z")

</div>

Yep. I was suspecting that also so I have changed to

```
if [@metadata][LS_OUTPUT_DEBUG] == 1 {
...
}

```

Do the "export LS\_OUTPUT\_DEBUG=1", then start the logstash.  
However, there is still no output file generated.

---

<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:** [June 11, 2019, 4:22pm UTC](https://discuss.elastic.co/t/use-environment-variable-to-control-output/185052/9 "2019-06-11T16:22:54Z")

</div>

> [@nobound](#):
>
> if [@metadata][LS\_OUTPUT\_DEBUG] == 1 { ... }

mutate+add\_field never generates integers, you need to use "1" there.

---

<div class="post-metadata">

**Author:** ![nobound](https://avatars.discourse-cdn.com/v4/letter/n/f07891/32.png) [@nobound](https://discuss.elastic.co/u/nobound)\
**Post date:** [June 11, 2019, 4:45pm UTC](https://discuss.elastic.co/t/use-environment-variable-to-control-output/185052/10 "2019-06-11T16:45:47Z")

</div>

Ouch!

---

<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 9, 2019, 4:45pm UTC](https://discuss.elastic.co/t/use-environment-variable-to-control-output/185052/11 "2019-07-09T16:45:51Z")

</div>

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