Fork me on GitHub
#timbre
<
2023-11-30
>
Joakim Verona10:11:35

I have a problem rebinding out so it works inside a catch statement, using timbre. With println it works

Joakim Verona10:11:06

the test program looks like this:

(require '[babashka.process :as p]
          '[clojure.edn :as edn]
          '[babashka.fs :as fs])
(require '[babashka.cli :as cli])
(require '[clojure.string :as str])
(require '[taoensso.timbre :as timbre])
(import 'java.time.format.DateTimeFormatter
        'java.time.LocalDateTime)

(def ^:dynamic *foo* "foo root value")

(defn clone-and-build-repo []
  (println "(stdout) start, nothing should go to stdout")
  (let [writer  (  "/tmp/bindproblem.txt")]
    (with-open [out-writer writer]
      (binding [*out* out-writer
                *foo* "foo inside binding"]
        (timbre/info "out:" *out*)
        (timbre/info "foo inside binding:" *foo*)
        (try
          (timbre/info "this should go to the file  " )
          (println "this too")

          (throw (new Exception "an error im trhowing"))

          (catch Exception e
            ;;*out* seems to revert to default value here?
            (binding [*out* out-writer]
              (timbre/info "foo inside catch:" *foo*)
              (timbre/info "out:" *out*)
              (timbre/error e " not going to the file? it should!" ))
            )))))
  (println "(stdout) ... until now")
  (println "(stdout) start file content")
  (println (slurp  "/tmp/bindproblem.txt"))
  (println "(stdout) end file content")
  )

( clone-and-build-repo)

1
Peter Taoussanis08:12:01

Hi Joakim, From skimming your example code, my guess is that timbre/error may be printing to *err* when you’re logging an error. Depends on your appender though, and it wasn’t obvious to me from your example if only the error call is producing unexpected output or not. I’d try binding both *out* and *err* and see if that helps. BTW if it does help, you shouldn’t need to rebind again in the catch block. Some general feedback- • Please try strip unnecessary/irrelevant code in examples to make them easier to read. • Please consider including relevant info when possible (e.g. here what appender/s you’re using, which log calls are/not producing expected output, etc.). These kinds of things can make it easier for maintainers or other users to assist, which means you’re likely to get useful help sooner. Hope that helps, cheers! 🙂

Peter Taoussanis09:12:44

You’re welcome, cheers!

Joakim Verona10:11:34

the output inside the catch should also go to the file, instead it goes to stdout