1

I am using logback and I want to print an epoch timestamp instead of the date and time.

https://logback.qos.ch/manual/layouts.html#contextName references https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html where there is no way to format a date as epoch.

I can't believe this essential functionality isn't available, but I googled for a while and couldn't find it.

What I found is the same question for log4j log4j : current time in milliseconds, but what @thegeko suggests there %d{UNIX_MILLIS} doesn't work for me in logback.

Does anybody know?

sazzad
  • 5,740
  • 6
  • 25
  • 42
Oliver Hausler
  • 4,900
  • 4
  • 35
  • 70
  • What does “epoch timestamp” mean to you? Are you looking for a count of milliseconds from the epoch reference of 1970-01-01T00:00:00Z? Or another granularity from [another epoch](https://en.wikipedia.org/wiki/Epoch_(computing)#Notable_epoch_dates_in_computing)? – Basil Bourque Feb 25 '19 at 04:44
  • Why would expect a count from epoch to be available for logging? Such values are not readable by humans. – Basil Bourque Feb 25 '19 at 04:45
  • @BasilBourque https://en.wikipedia.org/wiki/Unix_time - When logs are collected in a central location, epoch makes more sense than human-readable formats. It's in UTC and always accurate without parsing, no matter where the server is located. The logs are then read by a client, which typically converts it to human readable format, localized to format and time zone. If the log server emits logs in ISO 8601 notation or similar, such logs need to be parsed or translated multiple times until they reach the client, which is a waste of CPU. – Oliver Hausler Feb 27 '19 at 04:41

1 Answers1

1

You can use this snippet to print Unix Epoch:

<layout class="net.logstash.logback.layout.LogstashLayout">
    <timeZone>UTC</timeZone>
    <timestampPattern>[UNIX_TIMESTAMP_AS_NUMBER]</timestampPattern>
</layout>

Reference: https://github.com/logstash/logstash-logback-encoder#customizing-timestamp

Matt Ke
  • 3,599
  • 12
  • 30
  • 49
Suresh T
  • 67
  • 1
  • 4