Clojure:
Printing debug output

How to:

In Clojure, you often print debug output using println, printf, pr, or prn. Here’s how you sprinkle in some debug prints:

(defn add-and-print [a b]
  (println "Adding:" a "and" b) ; Prints the operation
  (let [result (+ a b)]
    (println "Result:" result)  ; Prints the result
    result))                    ; Returns the result

(add-and-print 3 4)

Sample Output:

Adding: 3 and 4
Result: 7

Or, to debug values in the middle of a threading macro:

(require '[clojure.pprint :refer [pprint]])

(-> 3
    (+ 5)
    (pprint)             ; Prints intermediate result
    (* 2))

Sample Output:

8

Deep Dive:

Print debugging has a long history, probably as old as programming itself. It’s straightforward: you insert print statements where you suspect problems might lie, run the code, and look at the output.

Clojure’s functions for debug printing are quite similar to those in other Lisp languages, but with the usual functional flavor. println and prn differ in that the latter writes data in a way that can be read by the Clojure reader. pprint (pretty print) from clojure.pprint can be used when you want a nicer format.

A Clojure-specific tool for debugging is tap>. Introduced in Clojure 1.10, it allows for non-blocking ’taps’ into running code without having to litter your code with print statements.

For larger or more complex projects, consider a logging library like clojure.tools.logging or timbre.

See Also: