From 2c16d6783ced803a59bb5caf7a7d85404c7ae19c Mon Sep 17 00:00:00 2001
From: Peter Taoussanis
Date: Thu, 31 May 2012 00:05:36 +0700
Subject: [PATCH] Simplified appender timestamp args. Improved performance.
* Removed 'timestamp-fn' from appender args and replaced with ready-formatted 'timestamp' for simpler appender writing.
* Fixed appender wrapping function bug that was unnecessarily recomputing shared args and timestamp function at runtime.
Signed-off-by: Peter Taoussanis
---
README.md | 4 ++--
src/timbre/core.clj | 20 ++++++++++----------
2 files changed, 12 insertions(+), 12 deletions(-)
diff --git a/README.md b/README.md
index 5234328..a9a753a 100644
--- a/README.md
+++ b/README.md
@@ -114,10 +114,10 @@ Writing a custom appender is easy:
:enabled? true
:async? false
:max-message-per-msecs nil ; No rate limiting
- :fn (fn [{:keys [ap-config level error? instant timestamp-fn
+ :fn (fn [{:keys [ap-config level error? instant timestamp
ns message more] :as args}]
(when-not (:production-mode? ap-config)
- (apply println instant "Hello world!" message more)))
+ (apply println timestamp "Hello world!" message more)))
```
And because appender fns are just regular Clojure fns, you have *unlimited power*: write to your database, send a message over the network, check some other state (e.g. environment config) before making a choice, etc.
diff --git a/src/timbre/core.clj b/src/timbre/core.clj
index 8684182..276abeb 100644
--- a/src/timbre/core.clj
+++ b/src/timbre/core.clj
@@ -11,8 +11,8 @@
(defn prefixed-message
"timestamp LEVEL [ns] - message"
- [{:keys [level instant timestamp-fn ns message]}]
- (str (timestamp-fn instant) " " (-> level name str/upper-case)
+ [{:keys [level timestamp ns message]}]
+ (str timestamp " " (-> level name str/upper-case)
" [" ns "] - " message))
(def config
@@ -23,8 +23,7 @@
:doc, :min-level, :enabled?, :async?, :max-message-per-msecs, :fn?
An appender's fn takes a single map argument with keys:
- :ap-config, :level, :error?, :instant, :timestamp-fn, :ns,
- :message, :more
+ :ap-config, :level, :error?, :instant, :timestamp, :ns, :message, :more
See source code for examples."
(atom {:current-level :debug
@@ -99,13 +98,14 @@
[appender-id {apfn :fn :keys [async? max-message-per-msecs] :as appender}]
(->
;; Wrap to add compile-time stuff to runtime appender arguments
- (fn [apfn-args]
- (let [{:keys [timestamp-pattern locale] :as ap-config}
- (@config :shared-appender-config)]
+ (let [{:keys [timestamp-pattern locale] :as ap-config}
+ (@config :shared-appender-config)
+ timestamp-fn (make-timestamp-fn timestamp-pattern locale)]
+
+ (fn [{:keys [instant] :as apfn-args}]
(apfn (assoc apfn-args
- :ap-config ap-config ; Shared appender config map
- :timestamp-fn
- (make-timestamp-fn timestamp-pattern locale)))))
+ :ap-config ap-config
+ :timestamp (timestamp-fn instant)))))
;; Wrap for asynchronicity support
((fn [apfn]