fzakaria / slf4j-timbre

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

context is not passed to output-fn #30

Closed harti2006 closed 6 years ago

harti2006 commented 6 years ago

Hi,

I'm currently facing an issue with custom context, that is declared via timbre/with-context, not being delivered to the middleware and output-fn, when using the clojure.tools.logging adapter.

Steps to reproduce

Project dependencies

[com.taoensso/timbre "4.10.0"]
[com.fzakaria/slf4j-timbre "0.3.10"]
[org.apache.logging.log4j/log4j-1.2-api "2.11.0"]
[org.slf4j/log4j-over-slf4j "1.7.25"]
[org.slf4j/jul-to-slf4j "1.7.25"]
[org.slf4j/jcl-over-slf4j "1.7.25"]

Snippet

(do
  (require '[taoensso.timbre :as log])
  (require '[clojure.tools.logging :as clj-log])
  (require '[clojure.pprint :as pprint])

  (defn print-context [data]
    (pprint/pprint (:context data))
    data)

  (log/swap-config! assoc :middleware [print-context])

  (log/with-context {:hello "world"}
    (log/info "Log with Timbre")
    (clj-log/info "Log with Clojure Tools")))

Output

{:hello "world"}
18-06-28 08:28:21 my-machine INFO [user:13] - Log with Timbre
nil
18-06-28 08:28:21 my-machine INFO [user:286] - Log with Clojure Tools

Expected behavior

The custom application context should be supplied regardless of the log api that was used.

rufoa commented 6 years ago

Thanks for such a clear write-up!

Fixed in 0.3.11


Please note that you may still lose the context if you call clj-log/info etc. within an STM transaction.

This is because timbre uses a thread-local binding for *context* but clojure.tools.logging may use agents to do the actual logging in a separate thread using send-off.

(A pretty unlikely edge case but I'm documenting it here for posterity)

harti2006 commented 6 years ago

Awesome. Thank you so much for the quick fix. 0.3.11 works perfectly fine for me.