How can we visually add separators in log files, based on conditions between consecutive lines?

Sample log file:

[DEBUG][2016-05-26 10:24:51,714][FileHelper] - [realContextPath]
[DEBUG][2016-05-26 10:24:52,369][DbConnectionImpl] - [DELETE FROM xct_log]
[DEBUG][2016-05-26 10:24:56,925][DSRestrictionConverter] - [parseRestrict]
[DEBUG][2016-05-26 10:24:56,976][DbConnectionImpl] - [UPDATE hd_log]

The goal is to have a visual separator when there is more than 2 seconds between consecutive timestamps. In the above case, it'd be between the 2nd and the 3rd line.

To make that separator visible, we could use overline or underline properties, for example. No need to physically add something to the buffer...

(This is a slightly rewritten version of question Format SQL code, with a MUCH better title.)

  • 1
    Please delete that other question, if this one essentially replaces it. Or delete this one and edit the other one to improve it. Do not keep two essentially identical questions. Otherwise, they risk being closed by others.
    – Drew
    Dec 17 '16 at 22:44

You could do something like this:

(let (last  ov)
  (while (re-search-forward "[0-9]\\{4\\}-[01][1-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9]" nil t)
    (when last
      (when (< 2 (- (float-time (date-to-time (match-string 0)))
            (float-time (date-to-time last))))
    (setq ov (make-overlay (line-beginning-position) (line-end-position)))
    (ov-put ov 'face '(:foreground "red"))))
    (setq last (match-string 0))))

This could be in a function you run on a buffer (maybe in a hook function?) containing the log, and it will make any line that happens more than two seconds after the previous line red.

  • I've had to replace ov-put by overlay-put. And that works like a charm! Thanks a lot. Any idea where I should put it so that it gets executed in the auto-revert-mode? BTW, it will have to be updated so that it does not reparse the full buffer. That'd be unmanageable. Will try to make that change. Dec 22 '16 at 9:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.