From 66689b4ff989102da7805e22060212bfb5c9992e Mon Sep 17 00:00:00 2001 From: "Dr. Christian Betz" Date: Mon, 9 Jan 2023 16:16:15 +0100 Subject: [PATCH] fzakaria/slf4j-timbre#54, fzakaria/slf4j-timbre#45: Update to work with SLF4j 2.x, timbre 6.x Replaces the SLF4J 1.7.x Binders with an SLF4J 2.x ServiceProvider. Works in example ("integration-test"). --- integration_tests/timbre5/project.clj | 10 +- .../timbre5/src/example/core.clj | 6 +- project.clj | 24 ++-- .../org.slf4j.spi.SLF4JServiceProvider | 1 + src/slf4j_timbre/static_logger_binder.clj | 22 ---- src/slf4j_timbre/static_marker_binder.clj | 22 ---- src/slf4j_timbre/static_mdc_binder.clj | 14 --- src/slf4j_timbre/timbre_service_provider.clj | 40 +++++++ test/slf4j_timbre/t_adapter.clj | 108 +++++++++--------- test/slf4j_timbre/t_factory.clj | 15 ++- 10 files changed, 128 insertions(+), 134 deletions(-) create mode 100644 resources/META-INF/services/org.slf4j.spi.SLF4JServiceProvider delete mode 100644 src/slf4j_timbre/static_logger_binder.clj delete mode 100644 src/slf4j_timbre/static_marker_binder.clj delete mode 100644 src/slf4j_timbre/static_mdc_binder.clj create mode 100644 src/slf4j_timbre/timbre_service_provider.clj diff --git a/integration_tests/timbre5/project.clj b/integration_tests/timbre5/project.clj index e90c115..936eaab 100644 --- a/integration_tests/timbre5/project.clj +++ b/integration_tests/timbre5/project.clj @@ -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]) \ No newline at end of file + :main example.core + :aot [example.core]) \ No newline at end of file diff --git a/integration_tests/timbre5/src/example/core.clj b/integration_tests/timbre5/src/example/core.clj index bccb643..c548524 100644 --- a/integration_tests/timbre5/src/example/core.clj +++ b/integration_tests/timbre5/src/example/core.clj @@ -15,4 +15,8 @@ (.warn logger "Hello from SLF4J") (.info logger "Hello from SLF4J") (.debug logger "Hello from SLF4J") - (.trace logger "Hello from SLF4J"))) \ No newline at end of file + (.trace logger "Hello from SLF4J"))) + + +#_; + (-main) \ No newline at end of file diff --git a/project.clj b/project.clj index 86679b1..d61532b 100644 --- a/project.clj +++ b/project.clj @@ -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"}) \ No newline at end of file + :url "https://github.com/fzakaria/slf4j-timbre"}) \ No newline at end of file diff --git a/resources/META-INF/services/org.slf4j.spi.SLF4JServiceProvider b/resources/META-INF/services/org.slf4j.spi.SLF4JServiceProvider new file mode 100644 index 0000000..113d4e3 --- /dev/null +++ b/resources/META-INF/services/org.slf4j.spi.SLF4JServiceProvider @@ -0,0 +1 @@ +com.github.fzakaria.slf4j.timbre.TimbreServiceProvider \ No newline at end of file diff --git a/src/slf4j_timbre/static_logger_binder.clj b/src/slf4j_timbre/static_logger_binder.clj deleted file mode 100644 index 8826e21..0000000 --- a/src/slf4j_timbre/static_logger_binder.clj +++ /dev/null @@ -1,22 +0,0 @@ -(ns slf4j-timbre.static-logger-binder - (:gen-class :name org.slf4j.impl.StaticLoggerBinder - :implements [org.slf4j.spi.LoggerFactoryBinder] - :factory getSingleton - :init init) - (:import com.github.fzakaria.slf4j.timbre.TimbreLoggerFactory)) - -(def ^:private singleton-instance - (atom nil)) - -(defn -init - [] - (compare-and-set! singleton-instance nil (TimbreLoggerFactory.)) - [[]]) - -(defn -getLoggerFactory - [_] - @singleton-instance) - -(defn -getLoggerFactoryClassStr - [_] - (.getName TimbreLoggerFactory)) \ No newline at end of file diff --git a/src/slf4j_timbre/static_marker_binder.clj b/src/slf4j_timbre/static_marker_binder.clj deleted file mode 100644 index d4d2219..0000000 --- a/src/slf4j_timbre/static_marker_binder.clj +++ /dev/null @@ -1,22 +0,0 @@ -(ns slf4j-timbre.static-marker-binder - (:gen-class :name org.slf4j.impl.StaticMarkerBinder - :implements [org.slf4j.spi.MarkerFactoryBinder] - :factory getSingleton - :init init) - (:import org.slf4j.helpers.BasicMarkerFactory)) - -(def ^:private singleton-instance - (atom nil)) - -(defn -init - [] - (compare-and-set! singleton-instance nil (BasicMarkerFactory.)) - [[]]) - -(defn -getMarkerFactory - [_] - @singleton-instance) - -(defn -getMarkerFactoryClassStr - [_] - (.getName BasicMarkerFactory)) \ No newline at end of file diff --git a/src/slf4j_timbre/static_mdc_binder.clj b/src/slf4j_timbre/static_mdc_binder.clj deleted file mode 100644 index baa66cd..0000000 --- a/src/slf4j_timbre/static_mdc_binder.clj +++ /dev/null @@ -1,14 +0,0 @@ -(ns slf4j-timbre.static-mdc-binder - (:gen-class :name org.slf4j.impl.StaticMDCBinder - :factory getSingleton - :methods [[getMDCA [] org.slf4j.spi.MDCAdapter] - [getMDCAdapterClassStr [] String]]) - (:import org.slf4j.helpers.NOPMDCAdapter)) - -(defn -getMDCA - [_] - (NOPMDCAdapter.)) - -(defn -getMDCAdapterClassStr - [_] - (.getName NOPMDCAdapter)) \ No newline at end of file diff --git a/src/slf4j_timbre/timbre_service_provider.clj b/src/slf4j_timbre/timbre_service_provider.clj new file mode 100644 index 0000000..7b2d634 --- /dev/null +++ b/src/slf4j_timbre/timbre_service_provider.clj @@ -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") \ No newline at end of file diff --git a/test/slf4j_timbre/t_adapter.clj b/test/slf4j_timbre/t_adapter.clj index 058e61f..b7eaabb 100644 --- a/test/slf4j_timbre/t_adapter.clj +++ b/test/slf4j_timbre/t_adapter.clj @@ -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))) @@ -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")))) \ No newline at end of file + (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")))) \ No newline at end of file diff --git a/test/slf4j_timbre/t_factory.clj b/test/slf4j_timbre/t_factory.clj index dd66c10..4adb3ff 100644 --- a/test/slf4j_timbre/t_factory.clj +++ b/test/slf4j_timbre/t_factory.clj @@ -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)