How to check if Clojure code is running inside REPL?

I would like to format my logs differently depending on whether my code is running from REPL or if I run a compiled jar.

Is there an easy way to do this? I thought Leiningen was leaving a mark somewhere while doing REPL.

+4
source share
1 answer
(defn current-stack-trace []
      (.getStackTrace (Thread/currentThread)))

(defn is-repl-stack-element [stack-element]
      (and (= "clojure.main$repl" (.getClassName  stack-element))
           (= "doInvoke"          (.getMethodName stack-element))))

(defn is-in-repl []
      (some is-repl-stack-element (current-stack-trace)))

(defn my-log [msg]
      (if (is-in-repl)
          (prn (str "RUNNING IN REPL : " msg))
          (prn msg)))
+2
source

Source: https://habr.com/ru/post/1589533/


All Articles