# How to save next line with ruby filter

**URL:** <https://discuss.elastic.co/t/how-to-save-next-line-with-ruby-filter/107773>\
**Category:** Logstash\
**Created:** [November 15, 2017, 2:52pm UTC](https://discuss.elastic.co/t/how-to-save-next-line-with-ruby-filter/107773 "2017-11-15T14:52:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cy8el1us](https://avatars.discourse-cdn.com/v4/letter/c/e9bcb4/32.png) [@cy8el1us](https://discuss.elastic.co/u/cy8el1us)\
**Post date:** [November 15, 2017, 2:52pm UTC](https://discuss.elastic.co/t/how-to-save-next-line-with-ruby-filter/107773/1 "2017-11-15T14:52:10Z")

</div>

I wanted to know if it's possible to write an advanced ruby code into a logstash filter in order to keep specific value.  
I have something like:

```
ruby {
	code => "		
		_isInCyclic = false
		_isJump = false
		_loggerName = ''
		
		_currentLine = event.get('message')
		
		if _loggerName == ''
			if _currentLine['[SynchronizedThreadPoolStatistics] Cyclic thread pool statistics']
				_loggerName = 'SynchronizedThreadPoolStatistics'
				event.tag('inCyclicSynchro')
			else if _currentLine['[ThreadPoolStatistics] Cyclic thread pool statistics']
				_loggerName = 'ThreadPoolStatistics'
				event.tag('inCyclicThread')
				end
			end
		end

		if !_isInCyclic && _currentLine['[' + _loggerName + '] Cyclic thread pool statistics']
			print(_loggerName)
			_isInCyclic = true
			_isJump = true
		elsif _isInCyclic && _currentLine['Total thread pool statistics']
			_isInCyclic = false
		elsif _isInCyclic && _currentLine['Cyclic thread pool statistics']
			_isJump = true
		elsif _isInCyclic
			if _isJump
				_isJump = false
			else 
				if _currentLine['[' + _loggerName + ']' + ' ']
					event.tag('LineIwantToKeep')
				end
			end
		end
		"
}

```

When I run the code, logstash works normaly but it seems like he never going through the second if statement "if !\_isInCyclic && \_currentLine['[' + \_loggerName......" so I don't see my tag "LineIwantToKeep" appears on lines that I want to send to ES.

I thought that this is because a filter is apply line by line no? Is it possible to tell logstash that we want have variables outside of the loop?

---

<div class="post-metadata">

**Author:** ![guyboertje](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/guyboertje/32/31592_2.png) [@guyboertje](https://discuss.elastic.co/u/guyboertje)\
**Post date:** [November 15, 2017, 3:03pm UTC](https://discuss.elastic.co/t/how-to-save-next-line-with-ruby-filter/107773/2 "2017-11-15T15:03:02Z")

</div>

Yes. You need to declare those variables in a `init` block. They need to be instance variable prefix `@`. The others can be local variables.

```ruby
    ruby {
      init => "
        @isInCyclic = false
        @isJump = false
      "
      code => "
        _loggerName = ''
        _currentLine = event.get('message')
        
        if _loggerName == ''
          if _currentLine['[SynchronizedThreadPoolStatistics] Cyclic thread pool statistics']
            _loggerName = 'SynchronizedThreadPoolStatistics'
            event.tag('inCyclicSynchro')
          else if _currentLine['[ThreadPoolStatistics] Cyclic thread pool statistics']
            _loggerName = 'ThreadPoolStatistics'
            event.tag('inCyclicThread')
            end
          end
        end

        if !@isInCyclic && _currentLine['[' + _loggerName + '] Cyclic thread pool statistics']
          print(_loggerName)
          @isInCyclic = true
          @isJump = true
        elsif @isInCyclic && _currentLine['Total thread pool statistics']
          @isInCyclic = false
        elsif @isInCyclic && _currentLine['Cyclic thread pool statistics']
          @isJump = true
        elsif @isInCyclic
          if @isJump
            @isJump = false
          else 
            if _currentLine['[' + _loggerName + ']' + ' ']
              event.tag('LineIwantToKeep')
            end
          end
        end
      "
    }

```

---

<div class="post-metadata">

**Author:** ![cy8el1us](https://avatars.discourse-cdn.com/v4/letter/c/e9bcb4/32.png) [@cy8el1us](https://discuss.elastic.co/u/cy8el1us)\
**Post date:** [November 15, 2017, 3:23pm UTC](https://discuss.elastic.co/t/how-to-save-next-line-with-ruby-filter/107773/3 "2017-11-15T15:23:07Z")

</div>

Thanks. That's exactly what I needed

---

<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, 3:23pm UTC](https://discuss.elastic.co/t/how-to-save-next-line-with-ruby-filter/107773/4 "2017-12-13T15:23:19Z")

</div>

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