From f9563cc10d625630f4cfde18b6cc03c366ea2f0a Mon Sep 17 00:00:00 2001 From: Josh Tilles Date: Fri, 29 Apr 2016 21:55:15 -0400 Subject: [PATCH] [#177] Improvements to clojure.tools.logging integration (@MerelyAPseudonym) --- src/taoensso/timbre/tools/logging.clj | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/taoensso/timbre/tools/logging.clj b/src/taoensso/timbre/tools/logging.clj index ec630f5..c09b7d0 100644 --- a/src/taoensso/timbre/tools/logging.clj +++ b/src/taoensso/timbre/tools/logging.clj @@ -6,31 +6,33 @@ Timbre through tools.logging unless you absolutely must (e.g. you're working with a legacy codebase)." - (:require [clojure.tools.logging] - [taoensso.encore :as enc] + (:require clojure.tools.logging [taoensso.timbre :as timbre])) -(deftype Logger [logger-ns] +(deftype Logger [logger-ns config] clojure.tools.logging.impl/Logger (enabled? [_ level] - ;; No support for explicit config, nor ns filtering: - (timbre/log? level)) + (timbre/log? level (str logger-ns) config)) (write! [_ level throwable message] (timbre/log! level :p [message] ; No support for pre-msg raw args - {:config timbre/*config* ; No support for explicit config - :?ns-str nil ; Not provided by tools.logging API + {:config config + :?ns-str (str logger-ns) :?file nil ; '' :?line nil ; '' :?err throwable}))) -(deftype LoggerFactory [] +(deftype LoggerFactory [cache] clojure.tools.logging.impl/LoggerFactory (name [_] "Timbre") - (get-logger [_ logger-ns] (Logger. logger-ns))) + (get-logger [_ logger-ns] + (or (get @cache logger-ns) + (let [logger (Logger. logger-ns timbre/*config*)] + (swap! cache assoc logger-ns logger) + logger)))) (defn use-timbre [] (alter-var-root (var clojure.tools.logging/*logger-factory*) - (constantly (LoggerFactory.)))) + (constantly (LoggerFactory. (atom {})))))