How to print events and fields in Logstash using Ruby code?

Is there anyway I can print the events with ruby in Logstash?

Logstash.conf file:

input {
    http {
        port => 9600
        id => "my_plugin_id"
    }
}

filter {
    ruby {
        code => "puts event['message']"
    }
}

output{
    stdout { 
        codec => rubydebug {
            metadata => true
        }
    }
}

Error:

[2019-06-11T17:59:05,893][ERROR][logstash.filters.ruby ] Ruby exception occurred: undefined method `' for #LogStash::Event:0x642b9c0d

Hi,

You can do it with

puts event.get('fieldName')

1 Like

Yes, that worked, except fieldname needs to be in strings " ", and not ' '. Thanks for your help @shin-higuchi! :slightly_smiling_face:

Solution:

filter {
    ruby {
        code => 'puts event.get("message")'
    }
}

or

filter {
    ruby {
        code => 'puts event.get("[message]")'
    }
}
2 Likes

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.