Strftime is not working Logshtash ruby code

Hi,
Im running logstash 5.4 and i need new add_field with sub date string

im trying filter like this

ruby {
code => "event.set('read_time' , event.get('@timestamp').strftime('%Y-%m'))"
}

And it is saying '[2017-06-02T02:05:32,867][ERROR][logstash.filters.ruby ] Ruby exception occurred: undefined method `strftime' for 2017-06-02T01:54:11.000Z:LogStash::Timestamp'

What should I do or what im doing wrong
Please help on this.

Thanks in advance.

From what I gather, strftime needs to operate on a Time instance, and @timestamp is not one of those.
The code below should work instead

ruby {
    code => "event.set('read_time' , Time.at(event.get('@timestamp').to_i).strftime('%Y-%m'))"
}

The LogStash::Timestamp class has a time method.

Try...

ruby {
code => "event.set('read_time' , event.get('@timestamp').time.strftime('%Y-%m'))"
}
2 Likes

Thank you This was great.

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