Sortable, readable and standard time format for logs

前端 未结 2 633
Happy的楠姐
Happy的楠姐 2021-01-17 21:32

Timestamp format in logs

Most log lines contain a timestamp and event description:

[When] [What]

e.g.:

[23/Jul/20         


        
相关标签:
2条回答
  • 2021-01-17 22:23

    The unix date command has such an option. Use

    date -Iseconds
    

    or

    date -Ins
    

    The manpage says:

       -I[FMT], --iso-8601[=FMT]
              output  date/time  in  ISO  8601  format.   FMT='date' for date only (the default),
              'hours', 'minutes', 'seconds', or 'ns' for date and time to  the  indicated  preci‐
              sion.  Example: 2006-08-14T02:34:56-0600
    
    0 讨论(0)
  • 2021-01-17 22:26

    enter image description here

    @J.F. Sebestian - Thanks for your comment.

    After some research I chose RFC 3339 / ISO 8601 in UTC, e.g.:

    date -u "+[%Y-%m-%d %H:%M:%S%z (%Z)]"       # Space separated with tz abbreviation
    [2013-07-31 23:56:34+0000 (UTC)]                   
    
    date -u "+[%Y-%m-%d %H:%M:%S.%N %z (%Z)]"  # Space separated with nanoseconds and tz abbreviation
    [2013-07-31 23:56:34.812572000 +0000 (UTC)]
    

    Features:

    • Sortable (Most significant date item is on the left)
    • Readable
    • Unambiguous, time zone clearly stated
    • Delimited by [,], useful for regexing the date away
    • Easily parsable
    • Accurate: uses nanoseconds (might be practically milliseconds on some machines, which is good enough)

    I've also created a nice github project that helps with date formatting - feel free to take a look and suggest your own formats!

    0 讨论(0)
提交回复
热议问题