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 configure timbre for `lein test` #293

Closed antonmos closed 3 years ago

antonmos commented 5 years ago

i think this might be similar to what the original question was in https://github.com/ptaoussanis/timbre/issues/9 but it is closed.

So, i need to call timbre/merge-config from somewhere so that i dont get excessive debug logging from various java libs.

For lein repl, I can use

:profiles {:repl {:main turnkey.core
                    :repl-options   {:init-ns   repl}}}

and call timbre/merge-config from the repl ns.

But how do i do it for lein test? Thanks!

rymndhng commented 4 years ago

Hey @antonmos, I have a few recommendations for this (with various levels of granularity).

My personal recommendation is to try and scope it as tightly as possible -- just incase you're throwing away logs that might be important.

Here are my suggestions in order of preference:

1. Configure timbre in the scope of your test statement

(def test-log-settings {:level :warn ..})

(deftest my-test-that-uses-a-noisy-library
  (timbre/with-merged-config timbre-test-settings
     (is (= ..)))

2. Configure timbre on an entire namespace using a fixture

(defn with-timbre-config [config]
  (fn [f]
    (timbre/with-merged-config config
      (f))))

(t/use-fixtures :once (with-timbre-config {:level :debug}))

(t/deftest test-output
  (timbre/info "info level")
  (timbre/warn "warn level"))

3. Configure timbre globally on test run with an injection

(ns test-injections
  (:require [taoensso.timbre :as timbre]))

(timbre/merge-config! {:level :warn})

And in your project.clj file, add/modify your test profile to "inject" test-injections namespace.

(defproject ...
  :profiles {
    :test {:injections [(require 'test-injections)]}
  }
)
antonmos commented 4 years ago

i havent thought using injections for this! i think that's what i was looking for. Thank you!

ptaoussanis commented 3 years ago

@rymndhng Thanks Raymond!