Skip to content

Instantly share code, notes, and snippets.

@Deraen
Created January 27, 2017 22:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Deraen/bcee211411a1154ed5437f88528b8c35 to your computer and use it in GitHub Desktop.
Save Deraen/bcee211411a1154ed5437f88528b8c35 to your computer and use it in GitHub Desktop.
Proof of Concept, catch test output
(defonce ^:private log-buffer (atom nil))
(defn get-log-message []
(let [message (first @log-buffer)]
(swap! log-buffer #(vec (rest %)))
message))
(defn catch-logging [f]
(reset! log-buffer nil)
(let [printer (java.io.PrintStream.
(proxy [java.io.ByteArrayOutputStream] []
(flush []
; deal with reflection in proxy-super
(let [^java.io.ByteArrayOutputStream this this]
(proxy-super flush)
(let [message (.trim (.toString this))]
(proxy-super reset)
(if (> (.length message) 0)
(swap! log-buffer conj message))))))
true)
orig-out System/out]
(System/setOut printer)
(try
(f)
(finally
(System/setOut orig-out)))))
(test/use-fixtures :each catch-logging)
(deftest foo-test
(log/infof "Hello world")
(is (re-find #"^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3} \+\d{4} \[[^\]]*\] INFO \[lokit.core-test\] - Hello world$" (get-log-message))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment