Last active
August 21, 2024 01:56
-
-
Save potetm/f789ab1a070f2de2f2cb7ab5f3f61a00 to your computer and use it in GitHub Desktop.
Structured logging in Clojure
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
| (require '[clojure.tools.logging :as log] | |
| '[clojure.tools.logging.impl :as impl]) | |
| (defmacro logp | |
| {:arglists '([level message & more] [level throwable message & more])} | |
| [level x & more] | |
| (cond | |
| (nil? more) | |
| `(log/log ~level (str ~x)) | |
| (instance? String x) | |
| `(log/log ~level (str ~x " " (pr-str ~@more))) | |
| :else | |
| `(let [logger# (impl/get-logger log/*logger-factory* ~*ns*)] | |
| (if (impl/enabled? logger# ~level) | |
| (let [x# ~x] | |
| (if (instance? Throwable x#) | |
| (log/log* logger# ~level x# (pr-str ~@more)) | |
| (log/log* logger# ~level nil (str x# " " (pr-str ~@more))))))))) | |
| (defmacro trace | |
| "Trace level logging using print-style args." | |
| {:arglists '([message & more] [throwable message & more])} | |
| [& args] | |
| `(logp :trace ~@args)) | |
| (defmacro debug | |
| "Debug level logging using print-style args." | |
| {:arglists '([message & more] [throwable message & more])} | |
| [& args] | |
| `(logp :debug ~@args)) | |
| (defmacro info | |
| "Info level logging using print-style args." | |
| {:arglists '([message & more] [throwable message & more])} | |
| [& args] | |
| `(logp :info ~@args)) | |
| (defmacro warn | |
| "Warn level logging using print-style args." | |
| {:arglists '([message & more] [throwable message & more])} | |
| [& args] | |
| `(logp :warn ~@args)) | |
| (defmacro error | |
| "Error level logging using print-style args." | |
| {:arglists '([message & more] [throwable message & more])} | |
| [& args] | |
| `(logp :error ~@args)) | |
| (defmacro fatal | |
| "Fatal level logging using print-style args." | |
| {:arglists '([message & more] [throwable message & more])} | |
| [& args] | |
| `(logp :fatal ~@args)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment