Last active
July 14, 2020 11:06
-
-
Save TimoKramer/e826aa8d17c4b694d9d75b7fce61715d to your computer and use it in GitHub Desktop.
Using Timbre and a Clojure Macro to centralise logging and error handling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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}})) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
src/folder{:deps {com.taoensso/timbre {:mvn/version "4.10.0"}}}into deps.edn in project-rootclj -m macroraise