Skip to content
Advertisement

Generate log4j timestamp format in linux date

I want to mimic the format used in log4j for timestamps using the linux date (just some quick scripting)

On log4j the format is defined as %d{dd MMM yyyy HH:mm:ss,SSS} That would translate into lines like:

2016-03-10 07:01:09,778
2016-03-10 07:01:09,784

Can this be accomplished in date? The closer I got was using

date +"%Y-%m-%d %H:%M:%S,%N"

But this still has 9 digits instead of 3, any ideas? I’d prefer not using sed/cut,etc, but if there’s no alternative it’d be fine.

Advertisement

Answer

That %N is giving you the number of nanoseconds, (one billion per second) hence the nine digits, and GNU date doesn’t have a milliseconds option.

Thankfully we can specify the field width like this

date +"%Y-%m-%d %H:%M:%S,%3N"

To turn billionths into thousandths.

M.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement