Strftime is not working Logshtash ruby code


(Ruben Papovyan) #1

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.


(Paris Mermigkas) #2

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'))"
}

(Guy Boertje) #3

The LogStash::Timestamp class has a time method.

Try...

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

(Ruben Papovyan) #4

Thank you This was great.


(system) #5

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