Skip to content

Commit

Permalink
fzakaria#54, fzakaria#45: Update to work with SLF4j 2.x, timbre 6.x
Browse files Browse the repository at this point in the history
Replaces the SLF4J 1.7.x Binders with an SLF4J 2.x ServiceProvider. Works in example ("integration-test").
  • Loading branch information
Dr. Christian Betz committed Jan 9, 2023
1 parent 9f1d565 commit 66689b4
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 134 deletions.
10 changes: 5 additions & 5 deletions integration_tests/timbre5/project.clj
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
(defproject example "1.0.0"
:dependencies [[org.clojure/clojure "1.11.1"]
[com.taoensso/timbre "5.2.1"]
[org.slf4j/slf4j-api "1.7.36"]
:dependencies [[org.clojure/clojure "1.11.1"]
[com.taoensso/timbre "6.0.4"]
[org.slf4j/slf4j-api "2.0.6"]
; slf4j-timbre will be added here by lein update-in
]
:main example.core
:aot [example.core])
:main example.core
:aot [example.core])
6 changes: 5 additions & 1 deletion integration_tests/timbre5/src/example/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@
(.warn logger "Hello from SLF4J")
(.info logger "Hello from SLF4J")
(.debug logger "Hello from SLF4J")
(.trace logger "Hello from SLF4J")))
(.trace logger "Hello from SLF4J")))


#_;
(-main)
24 changes: 11 additions & 13 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,27 @@
:description "SLF4J binding for Timbre"
:url "https://github.com/fzakaria/slf4j-timbre"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.11.1"]
[com.taoensso/timbre "5.2.1"]
[org.slf4j/slf4j-api "1.7.36"]]
[com.taoensso/timbre "6.0.4"]
[org.slf4j/slf4j-api "2.0.6"]]
:profiles {:dev
{:dependencies [[midje "1.10.5"]]
:plugins [[lein-midje "3.2.2"]
[lein-sub "0.3.0"]
[day8/lein-git-inject "0.0.15"]]
:sub ["integration_tests/timbre5"]
:aliases {"run-integration-tests" ["sub" "do" "clean," "deps,"
"update-in" ":dependencies" "conj" "[com.fzakaria/slf4j-timbre \"lein-git-inject/version\"]" "--" "run"]}}}
:plugins [[lein-midje "3.2.2"]
[lein-sub "0.3.0"]
[day8/lein-git-inject "0.0.15"]]
:sub ["integration_tests/timbre5"]
:aliases {"run-integration-tests" ["sub" "do" "clean," "deps,"
"update-in" ":dependencies" "conj" "[com.fzakaria/slf4j-timbre \"lein-git-inject/version\"]" "--" "run"]}}}
:middleware [leiningen.git-inject/middleware]
:git-inject {:version-pattern #"^(\d+\.\d+\.\d+)$"}
:aot [slf4j-timbre.adapter
slf4j-timbre.factory
slf4j-timbre.static-logger-binder
slf4j-timbre.static-marker-binder
slf4j-timbre.static-mdc-binder]
slf4j-timbre.timbre-service-provider]
:jar-exclusions [#"\.class$"]
:jar-inclusions [#"slf4j.*\.class$"]
:release-tasks [["vcs" "assert-committed"]
["deploy"]
["vcs" "push"]]
:scm {:name "git"
:url "https://github.com/fzakaria/slf4j-timbre"})
:url "https://github.com/fzakaria/slf4j-timbre"})
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.github.fzakaria.slf4j.timbre.TimbreServiceProvider
22 changes: 0 additions & 22 deletions src/slf4j_timbre/static_logger_binder.clj

This file was deleted.

22 changes: 0 additions & 22 deletions src/slf4j_timbre/static_marker_binder.clj

This file was deleted.

14 changes: 0 additions & 14 deletions src/slf4j_timbre/static_mdc_binder.clj

This file was deleted.

40 changes: 40 additions & 0 deletions src/slf4j_timbre/timbre_service_provider.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
(ns slf4j-timbre.timbre-service-provider
(:import (com.github.fzakaria.slf4j.timbre TimbreLoggerFactory)
(org.slf4j ILoggerFactory IMarkerFactory)
(org.slf4j.helpers BasicMDCAdapter BasicMarkerFactory)
(org.slf4j.spi MDCAdapter))
(:gen-class :name com.github.fzakaria.slf4j.timbre.TimbreServiceProvider
:implements [org.slf4j.spi.SLF4JServiceProvider]
#_#_:methods [[getLoggerFactory [] org.slf4j.ILoggerFactory]
[getMarkerFactory [] org.slf4j.IMarkerFactory]
[getMDCAdapter [] org.slf4j.spi.MDCAdapter]
[getRequestedApiVersion [] String]
[initialize [] void]]
)
)


(def ^:private logger-factory
(TimbreLoggerFactory.))

(def ^:private marker-factory
(BasicMarkerFactory.))

(def ^:private mdc-adapter
(BasicMDCAdapter.))


(defn -initialize [_]
[[]])

(defn -getLoggerFactory [_]
logger-factory)

(defn -getMarkerFactory [_]
marker-factory)

(defn -getMDCAdapter [_]
mdc-adapter)

(defn -getRequestedApiVersion [_]
"2.0.99")
108 changes: 56 additions & 52 deletions test/slf4j_timbre/t_adapter.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
slf4j-timbre.adapter)
(:use midje.sweet)
(:import org.slf4j.spi.LocationAwareLogger
(com.github.fzakaria.slf4j.timbre TimbreServiceProvider)
(org.slf4j LoggerFactory MarkerFactory)))


Expand All @@ -17,65 +18,68 @@
(defn invoke-each-lal
[logger marker fqcn message arg-array t]
(dorun
(for [level [LocationAwareLogger/ERROR_INT LocationAwareLogger/WARN_INT LocationAwareLogger/INFO_INT LocationAwareLogger/DEBUG_INT LocationAwareLogger/TRACE_INT]]
(.log logger marker fqcn level message arg-array t))))
(for [level [LocationAwareLogger/ERROR_INT LocationAwareLogger/WARN_INT LocationAwareLogger/INFO_INT LocationAwareLogger/DEBUG_INT LocationAwareLogger/TRACE_INT]]
(.log logger marker fqcn level message arg-array t))))

(def log-entries
(atom []))

(timbre/set-config!
{:level :trace :appenders {:counter {:enabled? true :fn (fn [data] (swap! log-entries conj data))}}})
{:level :trace :appenders {:counter {:enabled? true :fn (fn [data] (swap! log-entries conj data))}}})


(let [logger (LoggerFactory/getLogger (str *ns*))
marker (MarkerFactory/getMarker "marker1")]
(let [provider (TimbreServiceProvider.)
logger-factory (.getLoggerFactory provider)
logger (.getLogger logger-factory (str *ns*))
marker-factory (.getMarkerFactory provider)
marker (.getMarker marker-factory "marker1")]

(with-state-changes [(before :facts (reset! log-entries []))]

(tabular
(facts
(timbre/with-context {:foo "preserved"}
(invoke-each logger ?args)
(invoke-each logger marker ?args)) => anything ; for side effects only

(count @log-entries) => 10
(map :level @log-entries) => (contains [:error :warn :info :debug :trace] :in-any-order)
(map :context @log-entries) => (has some #{{:foo "preserved" :marker nil}})
(map :context @log-entries) => (has some #{{:foo "preserved" :marker "marker1"}})

@log-entries => (has every? (comp #{(str *ns*)} :?ns-str))
@log-entries => (has every? (comp #{"t_adapter.clj"} :?file))
@log-entries => (has every? (comp pos? :?line))
@log-entries => (has every? (comp #{"one two three four"} force :msg_)))

?args
["one two three four"]
["one two three {}" "four"]
["one two {} {}" "three" "four"]
["one {} {} {}" (to-array ["two" "three" "four"])]
["one two three four" (Exception. "test")]
["one two three four" (identity nil)]
["one two {} {}" (to-array ["three" "four" (Exception. "test")])])

(tabular
(facts
(timbre/with-context {:foo "preserved"}
(invoke-each-lal logger nil "slf4j_timbre.t_adapter" ?message (to-array ?arg-array) ?t)
(invoke-each-lal logger marker "slf4j_timbre.t_adapter" ?message (to-array ?arg-array) ?t)) => anything ; for side effects only

(count @log-entries) => 10
(map :level @log-entries) => (contains [:error :warn :info :debug :trace] :in-any-order)
(map :context @log-entries) => (has some #{{:foo "preserved" :marker nil}})
(map :context @log-entries) => (has some #{{:foo "preserved" :marker "marker1"}})

@log-entries => (has every? (comp #{(str *ns*)} :?ns-str))
@log-entries => (has every? (comp pos? :?line))
@log-entries => (has every? (comp #{"one two three four"} force :msg_)))

?message ?arg-array ?t
"one two three four" nil nil
"one two three {}" ["four"] nil
"one two {} {}" ["three" "four"] nil
"one {} {} {}" ["two" "three" "four"] nil
"one two three four" nil (Exception. "test")
"one two {} {}" ["three" "four"] (Exception. "test"))))
(tabular
(facts
(timbre/with-context {:foo "preserved"}
(invoke-each logger ?args)
(invoke-each logger marker ?args)) => anything ; for side effects only

(count @log-entries) => 10
(map :level @log-entries) => (contains [:error :warn :info :debug :trace] :in-any-order)
(map :context @log-entries) => (has some #{{:foo "preserved" :marker nil}})
(map :context @log-entries) => (has some #{{:foo "preserved" :marker "marker1"}})

@log-entries => (has every? (comp #{(str *ns*)} :?ns-str))
@log-entries => (has every? (comp #{"t_adapter.clj"} :?file))
@log-entries => (has every? (comp pos? :?line))
@log-entries => (has every? (comp #{"one two three four"} force :msg_)))

?args
["one two three four"]
["one two three {}" "four"]
["one two {} {}" "three" "four"]
["one {} {} {}" (to-array ["two" "three" "four"])]
["one two three four" (Exception. "test")]
["one two three four" (identity nil)]
["one two {} {}" (to-array ["three" "four" (Exception. "test")])])

(tabular
(facts
(timbre/with-context {:foo "preserved"}
(invoke-each-lal logger nil "slf4j_timbre.t_adapter" ?message (to-array ?arg-array) ?t)
(invoke-each-lal logger marker "slf4j_timbre.t_adapter" ?message (to-array ?arg-array) ?t)) => anything ; for side effects only

(count @log-entries) => 10
(map :level @log-entries) => (contains [:error :warn :info :debug :trace] :in-any-order)
(map :context @log-entries) => (has some #{{:foo "preserved" :marker nil}})
(map :context @log-entries) => (has some #{{:foo "preserved" :marker "marker1"}})

@log-entries => (has every? (comp #{(str *ns*)} :?ns-str))
@log-entries => (has every? (comp pos? :?line))
@log-entries => (has every? (comp #{"one two three four"} force :msg_)))

?message ?arg-array ?t
"one two three four" nil nil
"one two three {}" ["four"] nil
"one two {} {}" ["three" "four"] nil
"one {} {} {}" ["two" "three" "four"] nil
"one two three four" nil (Exception. "test")
"one two {} {}" ["three" "four"] (Exception. "test"))))
15 changes: 10 additions & 5 deletions test/slf4j_timbre/t_factory.clj
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
(ns slf4j-timbre.t-factory
(:use midje.sweet)
(:import org.slf4j.LoggerFactory))
(:import org.slf4j.LoggerFactory
(com.github.fzakaria.slf4j.timbre TimbreServiceProvider)))

(defn- logger [name]
(.getLogger (.getLoggerFactory (TimbreServiceProvider.))
name))

(let
[logger-1a (LoggerFactory/getLogger "logger-1")
logger-2 (LoggerFactory/getLogger "logger-2")
logger-1b (LoggerFactory/getLogger "logger-1")
logger-1c (future (LoggerFactory/getLogger "logger-1"))]
[logger-1a (logger "logger-1")
logger-2 (logger "logger-2")
logger-1b (logger "logger-1")
logger-1c (future (logger "logger-1"))]

(fact "factory returns same instances for same names"
(= logger-1a logger-1b) => true)
Expand Down

0 comments on commit 66689b4

Please sign in to comment.