Home > Back-end >  Fluentd record transformer conditional statements to add key if not present
Fluentd record transformer conditional statements to add key if not present

Time:12-15

I want to add a message field to the log if it is not present in the logs.

Here's the relevant fluentd configuration:

<filter **>
  @type record_transformer
  enable_ruby true
  <record>
    message ${ if record.has_key?('message'); then record ["message"]; else record["message"] == "nomsg"; end}
  </record>
</filter>

But when the message field is not present I get message=false, when it is present getting message=actual_msg.

Not sure why it is not taking message=nomsg.

Please help and suggest.
Tried above syntax to and fro but no luck.

CodePudding user response:

Simply make changes in else condition as below:

<filter **>
  @type record_transformer
  enable_ruby true
  <record>
    message ${ if record.has_key?('message'); then record ["message"]; else "nomsg"; end}
  </record>
</filter>

I hope this will help you.

  • Related