Home//

Logstash Conditional Filter Cheat Sheet: The Ultimate Guide

Logstash Conditional Filter Cheat Sheet: The Ultimate Guide

Minh Vu

By Minh Vu

Updated Nov 19, 2023

Conditional filtering is a very important feature that helps you to process data based on different conditions.

This tutorial, will show you how to use conditional filtering in Logstash with the if/else statement.

1. Conditional Filtering in Logstash

To filter data conditionally in Logstash, you can use the if/else statement: if <condition> { ... } else { ... }.

The syntax is:

filter.logstash.conf
if <condition> { # do something } else if <condition> { # do something else } else { # do something else }

For example, to check if the field [event][duration] is greater than 1000, you can use the following code:

filter.logstash.conf
if [event][duration] > 1000 { # do something } else { # do something else }

2. Check if Field Exists in Logstash

To check if a field exists in Logstash, you can use if [field], which will return true if the field exists, and false if the field does not exist.

For example, to check if the field [user][name] exists, you can use the following code:

filter.logstash.conf
if [user][name] { # do something } else { # do something else }

3. Check if Field is Empty in Logstash

Similarly, to check if a field is empty in Logstash, you can use if [field] == "", which will return true if the field is empty, and false if the field is not empty.

For example, to check if the field [user][age] is missing, you can use the following code:

filter.logstash.conf
if [user][age] == "" { # do something } else { # do something else }

4. Check if Field is Null in Logstash

To check if a field is null in Logstash, you can use if [field] == nil, which will return true if the field is null, and false if the field is not null.

For example, to check if the field [user][email] is null, you can use the following code:

filter.logstash.conf
if [user][email] == nil { # do something } else { # do something else }

5. Check if Field Starts With a String in Logstash

To check if a field starts with a string in Logstash, you can use if [field] =~ /^<string>/, which will return true if the field starts with the string, and false if the field does not start with the string.

For example, to check if the field [user][name] starts with the string Minh, you can use the following code:

filter.logstash.conf
if [user][name] =~ /^Minh/ { # do something } else { # do something else }

Conclusion

In this tutorial, we learned how to use conditional filtering in Logstash with the if/else statement.

There are some common cases that need the help of conditional filtering, such as:

  1. Check if a field exists in Logstash: if [field]
  2. Check if a field is empty in Logstash: if [field] == ""
  3. Check if a field is null in Logstash: if [field] == nil
  4. Check if a field starts with a string in Logstash: if [field] =~ /^<string>/

I hope this helps and see you in the next tutorial!

You can search for other posts at home page.
Minh Vu

Minh Vu

Software Engineer

Hi guys, I'm the author of WiseCode Blog. I mainly work with the Elastic Stack and build AI & Python projects. I also love writing technical articles, hope you guys have good experience reading my blog!