taoensso / timbre

Pure Clojure/Script logging library
https://www.taoensso.com/timbre
Eclipse Public License 1.0
1.44k stars 171 forks source link

How to see Timbre logs when connected to a remote nREPL? #309

Closed theronic closed 4 years ago

theronic commented 4 years ago

I'm using Timbre for my logging. When I connect to an nREPL in production, I can't see the log output unless I SSH into the machine and run journalctl, since my Java process is managed by systemd.

Is there a way to multiplex Timbre logs to the nREPL client?

theronic commented 4 years ago

This seems to work (via SO question):

;; run this code on the repl where you wish to see all output.
;; You will need to add the dependency [commons-io "2.4"] to your
;; leiningen dependencies.
(import 'org.apache.commons.io.output.WriterOutputStream)
(import 'java.io.PrintStream)

;; First, we redirect the raw stdout of the server to this repl
(System/setOut (PrintStream. (WriterOutputStream. *out*)
                             true)) ;; Auto-flush the PrintStream

;; Next, we alter the root binding of *out* so that new threads
;; send their output to THIS repl rather than the original System/out.
(alter-var-root #'*out* (fn [_] *out*))

;; Now the snippets should both send output to this repl:
(.println System/out "Hello stdout.")
(.start (Thread. #(println "Hello from a new thread.")))