Logstash config:检查是否存在布尔字段

时间:2014-10-09 20:06:47

标签: logstash

使用Logstash 1.4.2,我的字段myfield是我的JSON文档中的布尔值。

检查它是否存在(不关心布尔值)我用过:

if[myfield] { ...exists... } else { ...doesn't exist... } 

测试此条件语句的结果是:

[myfield] does not exist   --> false
[myfield] exists, is true  --> true
[myfield] exists, is false --> false //expected true because the field exists

检查布尔值,而不是存在

如何检查是否存在布尔字段?

2 个答案:

答案 0 :(得分:3)

有点蹩脚,它不能正常工作,但你可以像这样破解 - 添加布尔值的字符串表示,与字符串进行比较,然后删除添加的字段:

filter {
   mutate {
     add_field => { "test" => "%{boolean}" }
   }
   if [test] == 'true' or [test] == 'false' {
     // field is present and set right
   } else {
     // field isn't present or set to something other than true/false
   }
   mutate {
     remove_field => [ "test" ]
   }
}

答案 1 :(得分:1)

您可以使用Logstash红宝石插件。

filter {  
      ruby {
        code => '
          myfield= event.get("[myfield]")
          if !myfield.nil?
              event.set("[myfield]", "some_value_1")
            else
              event.set("[myfield]", "some_value_2")
            end
        '
      }
    }