fzakaria / slf4j-timbre

SLF4J binding for Clojure's Timbre
Eclipse Public License 1.0
94 stars 24 forks source link

can't silence logs in tests #39

Closed barkanido closed 4 years ago

barkanido commented 4 years ago

Hi, I have a library that wraps caffeine (which uses java.util.logging). And I am trying to silence the logs during tests.

Added [org.slf4j/jul-to-slf4j "1.7.30"] [com.fzakaria/slf4j-timbre "0.3.19"] to project.clj under :profiles/:dev/:dependencies and my deps are:

[com.fzakaria/slf4j-timbre "0.3.19" :scope "test"]
...
[com.taoensso/timbre "4.10.0" :scope "test"]
...
 [org.slf4j/jul-to-slf4j "1.7.30" :scope "test"]

on my tests I have added a fixture:

(defn configure-logger [test-fn]
  (let [initial-config timbre/*config*]
    (timbre/merge-config! {:appenders {:spit {:enabled? false}}})
    ;; (prn timbre/*config*)
    (test-fn)
    (timbre/set-config! initial-config)))

(use-fixtures :each configure-logger)

also, tried the same during compile time ((timbre/merge-config! {:appenders {:spit {:enabled? false}}}))

But I can still see some logging generated as a result of the unit tests:

WARNING: Exception thrown during refresh
java.util.concurrent.CompletionException: clojure.lang.ExceptionInfo: fail {}
        at java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:273)
        at java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:280)
        at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1592)
        at java.util.concurrent.CompletableFuture$AsyncSupply.exec(CompletableFuture.java:1582)
        at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
        at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
        at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
        at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)
Caused by: clojure.lang.ExceptionInfo: fail {}
        at cloffeine.core_test$fn__4845$fn__4846.invoke(core_test.clj:82)
        at cloffeine.common$reify_cache_loader$reify__464.load(common.clj:59)
        at com.github.benmanes.caffeine.cache.CacheLoader.reload(CacheLoader.java:166)
        at com.github.benmanes.caffeine.cache.CacheLoader.lambda$asyncReload$2(CacheLoader.java:190)
        at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1590)
        ... 5 more
rufoa commented 4 years ago

Hi @barkanido, thanks for bringing this to my attention.

(defn configure-logger [test-fn]
  (let [initial-config timbre/*config*]
    (timbre/merge-config! {:appenders {:spit {:enabled? false}}})
    ;; (prn timbre/*config*)
    (test-fn)
    (timbre/set-config! initial-config)))

Please could you uncomment (prn timbre/*config*) and post the output.

Does this problem only affect JUL messages? Are logs emitted directly from Timbre successfully silenced?

barkanido commented 4 years ago

Eventually, I figured out that I do not really need timbre nor slf4j-timbre. Configuring JUL directly at runtime did exactly what I needed:

(defn configure-logger [test-fn]
  (let [logger (Logger/getLogger "com.github.benmanes.caffeine")
        initial-level (.getLevel logger)]
    (.setLevel logger (Level/SEVERE))
    (test-fn)
    (.setLevel logger initial-level)))

(use-fixtures :each configure-logger)

Thanks for looking into that!