log4-hi-mode.el
author Oleksandr Gavenko <gavenkoa@gmail.com>
Wed, 01 Mar 2017 10:02:30 +0200
changeset 1499 d64d7d3699b9
parent 1497 9a881de3da24
child 1518 7965eec19342
permissions -rw-r--r--
Highlight optional year and milliseconds parts.

;;; log4-hi-mode.el --- Syntax highlighting logs keywords.

;;; Commentary:
;; Used for highlighting ERROR/WARN/INFO like keywords and dates in logs.

;;; Code:

(defface log4-hi-error-face
  '((t :inherit error))
  "Face for critical message.")

(defface log4-hi-warn-face
  '((t :inherit warning))
  "Face for urgent message.")

(defface log4-hi-info-face
  '((t :inherit success))
  "Face for informational message.")

(defface log4-hi-func-face
  '((t :inherit font-lock-function-name-face))
  "Face for functions in trace.")

(defface log4-hi-time-face
  '((t :inherit font-lock-builtin-face))
  "Face for time.")

(defvar log4-hi-keywords
  '(("FATAL\\|ERROR" . 'log4-hi-error-face)
    ("WARN\\|SEVERE" . 'log4-hi-warn-face)
    ("INFO\\|DEBUG\\|TRACE" . 'log4-hi-info-face) ))

(defvar log4-hi-func-keywords
  '(("at [[:alnum:].]+\\.\\([[:alnum:]$]+\\.[[:alnum:]$<>]+\\)([[:alnum:]]+\\.java:[0-9]+)" 1 'log4-hi-func-face) ))

(defvar log4-hi-time-keywords
  '(("\\(:?[0-9]\\{2\\}?[0-9]\\{2\\}.[0-9]\\{2\\}.[0-9]\\{2\\}.\\|\\<\\)[0-9]?[0-9]:[0-9][0-9]:[0-9][0-9]\\>\\(:?\\.[0-9]\\{3\\}\\)?" . 'log4-hi-time-face)))

(defvar log4-hi-func t
  "Mark to use hilighting of function names in stacktraces. Currently only Java supported.")

(defvar log4-hi-mode-map (make-sparse-keymap)
  "Keymap for `log4-hi-mode'.")

;;;###autoload
(define-minor-mode log4-hi-mode
  "Highlight standard elements in log4* like log-files."
  nil " log4-hi" log4-hi-mode-map
  :global nil
  (catch 'exit
    (when log4-hi-mode
      (font-lock-mode 1)
      (font-lock-add-keywords nil log4-hi-keywords)
      (font-lock-add-keywords nil log4-hi-time-keywords)
      (when log4-hi-func (font-lock-add-keywords nil log4-hi-func-keywords))
      (throw 'exit nil))
    (font-lock-remove-keywords nil log4-hi-keywords)
    (font-lock-remove-keywords nil log4-hi-time-keywords)
    (font-lock-remove-keywords nil log4-hi-func-keywords))
  (font-lock-fontify-buffer))

(provide 'log4-hi-mode)

(provide 'log4-hi-mode)

;;; log4-hi-mode.el ends here