如果您只想禁用特定调用中发生的所有日志记录,可以将tools.logging设置为您提供的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))
如果您希望基于消息的来源命名空间来删除消息,
您可以为您的应用程序创建自己的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))
如果您还希望根据消息的级别进行筛选,
您还需要实现一个自定义的日志记录器。
(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)))))
有很多改进、抛光和优化的空间,但看起来已经有了实现所需功能所需的所有必要组件。
个人来说,我倾向于将这些类型的东西放在后端特定的配置文件中,但这显然不是你想要的。 :)