timbre

Joakim Verona 2023-11-30T10:52:00.899439Z

Hhello

Joakim Verona 2023-11-30T10:52:35.087879Z

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

Joakim Verona 2023-11-30T10:53:06.382759Z

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 Taoussanis 2023-12-01T08:39:01.908099Z

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! 🙂

Joakim Verona 2023-12-19T09:37:32.645959Z

thanks!

Peter Taoussanis 2023-12-19T09:38:44.971579Z

You’re welcome, cheers!

Joakim Verona 2023-11-30T10:53:34.814779Z

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