用于在fluentd筛选器中检查字符串的正则表达式

ajsxfq5m  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(284)

如果日志字段包含特定字符串,我需要创建一个新字段“status”。我在fluentd中尝试了下面的代码,但这不起作用。我需要检查日志字段是否包含字符串“error:”,然后新字段的状态应该是error,否则如果它有ok,它应该有ok。

<filter**>
  @type record_transformer
  enable_ruby true
  <record>
    status "${\
      if record['log'].downcase.include? 'error:'
        puts 'error'
      elsif record['log'].downcase.include? 'ok:'
        puts 'ok'
      end}"
  </record>
</filter>

我们可以用regexp来做这个吗?
我也试着使用扫描。

<filter**>
  @type record_transformer
  enable_ruby true  
  <record>
   status ${record["log"].scan(/^.* ([[:<:]]error[[:>:]]|[[:<:]]ok[[:>:]]):.*$/i).first.compact} 
  </record>  
</filter>
jtoj6r0c

jtoj6r0c1#

除了现在阅读他们的文档之外,我对fluentd没有太多的经验。但是你能检查一下下面的代码片段吗?

<filter**>
  @type record_transformer
  enable_ruby true
  <record>
    status "${record['log'].downcase.include?('error:') ? 'error' : (record['log'].downcase.include?('ok:') ? 'ok' : '')}"
  </record>
</filter>

我怀疑 puts 在你的代码片段上运行。所以我对代码做了一些修改。如果这是你要找的东西,请告诉我。

相关问题