Sortable, readable, and standard time format for magazines

Log Timeline Format

Most log lines contain a timestamp and a description of the event:

[When] [What] 

eg:.

 [23/Jul/2013:19:35:11 +0000] Processing started. [23/Jul/2013:19:36:11 +0000] Processed 1000 items. [23/Jul/2013:19:37:11 +0000] Processing finished successfully. 

I am trying to find a standard timestamp for my log lines. My criteria:

  • Human readable . I want to easily understand when the event occurred.
  • Sort alphabetically . When I grep events from several files and sort them using POSIX sort or even the word / excel, I want the alphabetical sort to match the chronological type. For example, [23/Jul/2012:19:35:11 +0000] and [22/Jul/2013:19:35:11 +0000] not sorted - the 2013 line will appear before the 2012 line.
  • Easily parsed by all common languages . The timestamp should be easily analyzed using standard strptime if the log is being processed by the script.

The only standard I have found so far is ISO_8601 , which has many options (e.g. 2007-04-05T14:30Z and 2007-03-01T13:00:00Z ), and does not have a specific standard for log line events.

Could you recommend a standard timestamp format for log lines?

+4
source share
2 answers

enter image description here

@JF Sebestian - Thanks for your comment.

After some research, I chose RFC 3339 / ISO 8601 in UTC format , for example:

 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:

  • Sort (the most significant date item is on the left)
  • Read
  • Clear time zone
  • It is denoted by [ , ] , useful for redefining date.
  • Easy to analyze
  • Exact: uses nanoseconds (maybe almost milliseconds on some machines, which is good enough).

I also created a good github project that helps with date formatting - feel free to watch and suggest your own formats!

+7
source

The date unix command has this 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 
+2
source

Source: https://habr.com/ru/post/1493050/


All Articles