Hi 👋, I'm a software engineer specializing in backend systems, distributed systems, and scalable architecture. My blog shares practical tutorials based on 3+ years of experience. LeetCode 1756 (Top 10%). Actively seeking SDE roles — let's get in touch!
Comments
Leave a Comment
Success!
Receive Latest Updates 📬
Get every new post, special offers, and more via email. No fee required.
If you are looking for a way to control the flow of your logs in Logstash, you are in the right place.
In this tutorial, I will show you how to use conditionals in Logstash with if/else statements to control the flow of your logs.
This is usually helpful when you want to send logs to different outputs or apply different filters to different logs.
You can use boolean operators in the if/else statement to combine multiple conditions.
There are four boolean operators that you can use in the if/else statement:
and: both conditions must be true
or: at least one condition must be true
nand: both conditions must be false
xor: only one condition must be true
For example, if you want to check if both conditions are true, you can use the and operator.
output { if [type] == "nginx" and [status] == "200" { # type is nginx and status is 200 elasticsearch { hosts => ["localhost:9200"] index => "nginx-%{+YYYY.MM.dd}" } } else { stdout { codec => rubydebug } }}
If you want to check if only one condition is true, you can use the xor operator.
output { if [type] == "nginx" xor [status] == "200" { # type is nginx or status is 200 elasticsearch { hosts => ["localhost:9200"] index => "nginx-%{+YYYY.MM.dd}" } } else { stdout { codec => rubydebug } }}
output { if ![type] { # type does not exist elasticsearch { hosts => ["localhost:9200"] index => "nginx-%{+YYYY.MM.dd}" } } else if [type] { # type exists stdout { codec => rubydebug } }}
Comments