# Creating a temporary variable in logstash

**URL:** <https://discuss.elastic.co/t/creating-a-temporary-variable-in-logstash/107820>\
**Category:** Logstash\
**Created:** [November 15, 2017, 7:59pm UTC](https://discuss.elastic.co/t/creating-a-temporary-variable-in-logstash/107820 "2017-11-15T19:59:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![joconner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joconner/32/24360_2.png) [@joconner](https://discuss.elastic.co/u/joconner)\
**Post date:** [November 15, 2017, 7:59pm UTC](https://discuss.elastic.co/t/creating-a-temporary-variable-in-logstash/107820/1 "2017-11-15T19:59:24Z")

</div>

As I experiment with logstash, I have tried to create temporary variables to hold data that I hope to later use to use in an `add_field` statement. However, I often see configuration errors when I try to do this.

How does one create temporary variables in logstash? Not new tags or fields, but just temporary variables?

Thanks.

---

<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:** [November 15, 2017, 8:19pm UTC](https://discuss.elastic.co/t/creating-a-temporary-variable-in-logstash/107820/2 "2017-11-15T20:19:39Z")

</div>

Variables that persist between events? There's no "official" way to do that from the Logstash configuration language. It either needs to be explicitly implemented in a plugin or I think you can use a ruby filter like this:

```auto
ruby {
  init => "@counter = 0"
  code => "
    @counter += 1
    event.set('message_count', @counter)
  "
}

```

---

<div class="post-metadata">

**Author:** ![joconner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joconner/32/24360_2.png) [@joconner](https://discuss.elastic.co/u/joconner)\
**Post date:** [November 15, 2017, 8:22pm UTC](https://discuss.elastic.co/t/creating-a-temporary-variable-in-logstash/107820/3 "2017-11-15T20:22:47Z")

</div>

Thank you for your rapid response. No, I'm not thinking about persistent variables, just variables that I can use during the processing of a single event.

---

<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:** [November 15, 2017, 8:27pm UTC](https://discuss.elastic.co/t/creating-a-temporary-variable-in-logstash/107820/4 "2017-11-15T20:27:32Z")

</div>

Oh. Then you can store them as subfields to `@metadata`. They work like regular fields except they won't be passed on to outputs.

[https://www.elastic.co/guide/en/logstash/current/event-dependent-configuration.html#metadata](https://www.elastic.co/guide/en/logstash/current/event-dependent-configuration.html#metadata)

---

<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:** [December 13, 2017, 8:27pm UTC](https://discuss.elastic.co/t/creating-a-temporary-variable-in-logstash/107820/5 "2017-12-13T20:27:34Z")

</div>

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