clojure

Parsing logs with clojure

Parse a line of log with record & regex

(defrecord Logline [datetime action user id])
(def pattern #"(\d{8}-\d{2}:\d{2}:\d{2}.\d{3})\|.*\|(\w*),(\w*),(\d*)")
(defn parser [line] 
  (if-let [[_ dt a u i] (re-find pattern line)] 
          (->Logline dt a u i)))

Define a sample line :

(def sample "20170426-17:20:04.005|bip.com|1.0.0|alert|Update,john,12")

Test it :

(parser sample)

Result :

#user.Logline{:datetime "20170426-17:20:04.005", :action "Update", :user "john", :id "12"}

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow