[#98] Update Android appender to 3.x API (@alexander-yakushev)

This commit is contained in:
Alexander Yakushev 2015-03-28 11:23:32 +02:00 committed by Peter Taoussanis
parent 31cce3e6c2
commit 9e79a6935b
1 changed files with 35 additions and 27 deletions

View File

@ -2,32 +2,40 @@
"Android LogCat appender. Depends on the android runtime. This is a
configuration for the timbre logging library"
{:author "Adam Clements"}
(:require [taoensso.timbre :as timbre]))
(:require [taoensso.timbre :as timbre]
clojure.string))
(defn make-logcat-appender
"Returns an appender that writes to Android LogCat. Obviously only works if
running within the Android runtime (device or emulator). You may want to
disable std-out to prevent printing nested timestamps, etc."
[& [appender-opts make-opts]]
(let [default-appender-opts {:enabled? true
:min-level :debug}]
(merge default-appender-opts appender-opts
{:fn (fn [{:keys [level ns throwable message]}]
(let [output (format "%s %s - %s" timestamp
(-> level name clojure.string/upper-case)
(or message ""))]
(if throwable
(case level
:trace (android.util.Log/d ns output throwable)
:debug (android.util.Log/d ns output throwable)
:info (android.util.Log/i ns output throwable)
:warn (android.util.Log/w ns output throwable)
:error (android.util.Log/e ns output throwable)
:fatal (android.util.Log/e ns output throwable)
:report (android.util.Log/i ns output throwable))
(case level
:trace (android.util.Log/d ns output)
:debug (android.util.Log/d ns output)
:info (android.util.Log/i ns output)
:warn (android.util.Log/w ns output)
:error (android.util.Log/e ns output)
:fatal (android.util.Log/e ns output)
:report (android.util.Log/i ns output)))))})))
(def logcat-appender
{:doc (str "Appends to Android logcat. Obviously only works if "
"running within the Android runtime (device or emulator)."
"You may want to disable std-out to prevent printing nested "
"timestamps, etc.")
:min-level :debug
:enabled? true
:prefix-fn :ns
:fn (fn [{:keys [level prefix throwable message]}]
(if throwable
(case level
:trace (android.util.Log/d prefix message throwable)
:debug (android.util.Log/d prefix message throwable)
:info (android.util.Log/i prefix message throwable)
:warn (android.util.Log/w prefix message throwable)
:error (android.util.Log/e prefix message throwable)
:fatal (android.util.Log/e prefix message throwable)
:report (android.util.Log/i prefix message throwable))
(case level
:trace (android.util.Log/d prefix message)
:debug (android.util.Log/d prefix message)
:info (android.util.Log/i prefix message)
:warn (android.util.Log/w prefix message)
:error (android.util.Log/e prefix message)
:fatal (android.util.Log/e prefix message)
:report (android.util.Log/i prefix message))))})
"DEPRECATED: Use `make-logcat-appender` instead."
(make-logcat-appender))