If you simply want to disable all logging that happens in a certain call, you can tell tools.logging to use the provided disabled-logger-factory
.
(require '[clojure.tools.logging :as log]
'[clojure.tools.logging.impl :as log-impl])
(binding [log/*logger-factory* log-impl/disabled-logger-factory]
(call-to-noisy-lib))
If you are looking to drop messages based on the originating namespace,
you can create your own LoggerFactory
.
(def default-factory (log-impl/find-factory))
(def custom-factory
(reify log-impl/LoggerFactory
(name [_factory]
"some.arbitrary.name.for.this.factory")
(get-logger [factory logger-ns]
(if (= "namespace.you.want.to.silence" (str logger-ns))
log-impl/disabled-logger
(log-impl/get-logger default-factory logger-ns)))))
(binding [log/*logger-factory* custom-factory]
(call-to-noisy-lib))
If you addtionally want to filter based on the level of the message,
you'll need to implement a custom logger as well.
(defn logger-with-levels [wrapped-logger allowed-levels]
(reify log-impl/logger
(enabled? [logger level]
(when (contains? allowed-levels level)
(log-impl/enabled? wrapped-logger level)))
(write! [logger level throwable message]
(when (contains? allowed-levels level)
(log-impl/write! wrapped-logger level throwable message)))))
(def custom-factory
(reify log-impl/LoggerFactory
(name [_factory]
"some.arbitrary.name.for.this.factory")
(get-logger [factory logger-ns]
(if (= "namespace.you.want.to.silence" (str logger-ns))
(logger-with-levels (log-impl/get-logger default-factory logger-ns) #{:warn})
(log-impl/get-logger default-factory logger-ns)))))
There's room for improvement, polish, and optimization here, but it looks like all the necessary pieces are available to implement the desired functionality.
Personally, I'd lean towards putting this sort of thing in backend-specific configuration files, but that's explicitly not what you wanted. :)