I have the following in my logback.xml for logging in JSON. I want to print the log_timestamp field below in Epoch time format(in seconds) instead of the date and time.
<appender name="JSON_CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<providers>
<pattern>
<pattern>
{
"appNname": "${appName}",
"log_timestamp":"%date",
"level": "%level",
"method":"%M",
"class":"%c",
"user_id": "%mdc{user_id}"
}
</pattern>
</pattern>
</providers>
</encoder>
</appender>
The question is related to print an epoch timestamp instead of the date and time
I tried the suggestion in the above link by including the following directly under the encoder.But it didn't work.
<encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<layout class="net.logstash.logback.layout.LogstashLayout">
<timestampPattern>[UNIX_TIMESTAMP_AS_NUMBER]</timestampPattern>
</layout>
I appreciate if someone gives me a hint to achieve this.
Since logback's %date
conversion word does not support epoch time, you'll need to use logstash-logback-encoder's timestamp
provider to add the timestamp field.
<appender name="JSON_CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<providers>
<timestamp>
<fieldName>log_timestamp</fieldName>
<pattern>[UNIX_TIMESTAMP_AS_NUMBER]</pattern>
</timestamp>
<pattern>
<pattern>
{
"appName": "${appName}",
"level": "%level",
"method":"%M",
"class":"%c",
"user_id": "%mdc{user_id}"
}
</pattern>
</pattern>
</providers>
</encoder>
</appender>
Note that the timestamp value will be in milliseconds, not seconds.
If you want seconds, then I think the only way would be to implement a custom JsonProvider