logback icon indicating copy to clipboard operation
logback copied to clipboard

provide a %date{EPOCH_SECONDS} to get the date printed as seconds from epoch

Open ghost opened this issue 1 year ago • 2 comments

The documentation at https://logback.qos.ch/manual/layouts.html

Currently it seems there is no way to get the epoch seconds, it delegates to java.time.format.DateTimeFormatter and that has no way to print the seconds from the epoch.

I think it would be useful to provide

  • %date{EPOCH_SECONDS} like 1758543352 (integer, non fractional , no float) (Monday, September 22, 2025 12:15:52.005 PM losing the milliseconds)
  • %date{EPOCH_MILLIS} like 1758543352005 (Monday, September 22, 2025 12:15:52.005 PM)

This those two it will be possible to use %date{EPOCH_SECONDS}.%date{n} to get 1758543352.005999999 etc.

For example I'm trying to use logstash-logback-encoder to get two fields in JSON timestampSeconds and timestampNanos to produce structured logs that are compliant with that the google logging agent expects ( Logging agent: special JSON fields and Time-related fields), I can get the timestampNanos from %date{n} but I can't get the value for timestampSeconds from logback.

       <encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
            <providers>
                <timestamp />
                <pattern>
                    <pattern>
                        {
                        "timestampSeconds": "%date{EPOCH_SECONDS}",
                        "timestampNanos":"%date{n}"
                        }
                    </pattern>
                </pattern>
                <logLevel>
                    <fieldName>severity</fieldName>
                </logLevel>

                <loggerName />
                <message />
                <mdc />
                <stackTrace />
            </providers>
        </encoder>

ghost avatar Sep 22 '25 12:09 ghost

Hopefully https://github.com/qos-ch/logback/pull/1000, which adds EpochConverter and %epoch and %epoch{seconds} meet this requirement, so this issue can be closed?

duncanj avatar Jan 16 '26 10:01 duncanj

I agree @duncanj , @celki

Since 1.5.25 (#1000) already includes %epoch as an alternative for my proposed %date{EPOCH_MILLIS} and %epoch{seconds} as an alternative to my proposed %date{EPOCH_SECONDS} and #970 (which implemented those %date{EPOCH_xxx} was rejected , I think it's sensible to close this.

ecerulm avatar Jan 19 '26 12:01 ecerulm