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 <p.taoussanis@gmail.com>
This commit is contained in:
Peter Taoussanis 2012-05-31 00:05:36 +07:00
parent fe7e69bf92
commit 2c16d6783c
2 changed files with 12 additions and 12 deletions

View File

@ -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.

View File

@ -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]