Skip to content

Instantly share code, notes, and snippets.

@TimoKramer
Last active July 14, 2020 11:06
Show Gist options
  • Select an option

  • Save TimoKramer/e826aa8d17c4b694d9d75b7fce61715d to your computer and use it in GitHub Desktop.

Select an option

Save TimoKramer/e826aa8d17c4b694d9d75b7fce61715d to your computer and use it in GitHub Desktop.
Using Timbre and a Clojure Macro to centralise logging and error handling
(ns macroraise
(:require
[taoensso.timbre :as log]))
(defmacro raise [& fragments]
(let [msgs (butlast fragments)
data (last fragments)]
(list `(log/error ~@fragments)
`(log/log! :error :p ~fragments ~{:?line (:line (meta &form))})
`(throw (ex-info (str ~@(map (fn [m#] (if (string? m#) m# (list 'pr-str m#))) msgs)) ~data)))))
(defn -main []
(log/info "macroraise running!")
(raise "open" "fridge" "door"
{:causes #{:fridge-door-open :dangerously-high-temperature}
:current-temperature {:value 25 :unit :celsius}}))
@TimoKramer
Copy link
Author

TimoKramer commented Jul 14, 2020

  1. copy script into src/ folder
  2. put {:deps {com.taoensso/timbre {:mvn/version "4.10.0"}}} into deps.edn in project-root
  3. run with clj -m macroraise

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment