The println
function writes to STDOUT
. How can we write messages to STDERR
instead?
(println "Hello, STDOUT!")
There is no specific function for this, however you can dynamically rebind the var holding the stream that println
writes to like this:
(println "Hello, STDOUT!") (binding [*out* *err*] (println "Hello, STDERR!"))
In my REPL, the colour indicates the stream (red is STDERR):
Plain Java methods work fine and will be understood easily by Clojure novices:
(.println *err* "Hello, STDERR!")
Another alternative, just for fun, is clojure.java.io/copy
:
(require '[clojure.java.io :as cjio]) (cjio/copy "Hello, STDERR!\n" *err*) (.flush *err*)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With