Update 3rd-party appenders

This commit is contained in:
Peter Taoussanis 2016-06-20 18:21:58 +07:00
parent 682fa62334
commit fa1740e7fb
11 changed files with 79 additions and 74 deletions

View File

@ -1,6 +1,6 @@
(ns taoensso.timbre.appenders.3rd-party.android-logcat (ns taoensso.timbre.appenders.3rd-party.android-logcat
"Android LogCat appender. Requires Android runtime." "Android LogCat appender. Requires Android runtime."
{:author "Adam Clements"} {:author "Adam Clements (@AdamClements)"}
(:require [clojure.string :as str] (:require [clojure.string :as str]
[taoensso.timbre :as timbre])) [taoensso.timbre :as timbre]))
@ -20,34 +20,34 @@
(fn [data] (fn [data]
(let [{:keys [level timestamp_ msg_]} data] (let [{:keys [level timestamp_ msg_]} data]
(str (str
@timestamp_ " " (force timestamp_) " "
(str/upper-case (name level)) " " (str/upper-case (name level)) " "
@msg_))) (force msg_))))
:fn :fn
(fn [data] (fn [data]
(let [{:keys [level ?ns-str ?err_ output-fn]} data (let [{:keys [level ?ns-str ?err output_]} data
ns (str ?ns-str "") ns-str (str ?ns-str "")
output-str (output-fn data)] output-str (force output_)]
(if-let [throwable @?err_] (if-let [err ?err]
(case level (case level
:trace (android.util.Log/d ns output-str throwable) :trace (android.util.Log/d ns-str output-str err)
:debug (android.util.Log/d ns output-str throwable) :debug (android.util.Log/d ns-str output-str err)
:info (android.util.Log/i ns output-str throwable) :info (android.util.Log/i ns-str output-str err)
:warn (android.util.Log/w ns output-str throwable) :warn (android.util.Log/w ns-str output-str err)
:error (android.util.Log/e ns output-str throwable) :error (android.util.Log/e ns-str output-str err)
:fatal (android.util.Log/e ns output-str throwable) :fatal (android.util.Log/e ns-str output-str err)
:report (android.util.Log/i ns output-str throwable)) :report (android.util.Log/i ns-str output-str err))
(case level (case level
:trace (android.util.Log/d ns output-str) :trace (android.util.Log/d ns-str output-str)
:debug (android.util.Log/d ns output-str) :debug (android.util.Log/d ns-str output-str)
:info (android.util.Log/i ns output-str) :info (android.util.Log/i ns-str output-str)
:warn (android.util.Log/w ns output-str) :warn (android.util.Log/w ns-str output-str)
:error (android.util.Log/e ns output-str) :error (android.util.Log/e ns-str output-str)
:fatal (android.util.Log/e ns output-str) :fatal (android.util.Log/e ns-str output-str)
:report (android.util.Log/i ns output-str)))))}) :report (android.util.Log/i ns-str output-str)))))})
;;;; Deprecated ;;;; Deprecated

View File

@ -1,30 +1,37 @@
(ns taoensso.timbre.appenders.3rd-party.congomongo (ns taoensso.timbre.appenders.3rd-party.congomongo
"MongoDB appender. Requires on https://github.com/aboekhoff/congomongo." "MongoDB appender. Requires on https://github.com/aboekhoff/congomongo."
{:author "Emlyn Corrin"} {:author "Emlyn Corrin (@emlyn)"}
(:require [somnium.congomongo :as mongo] (:require [somnium.congomongo :as mongo]
[taoensso.timbre :as timbre] [taoensso.timbre :as timbre]
[taoensso.encore :as encore])) [taoensso.encore :as encore]))
;; TODO Test port to Timbre v4 ;; TODO Test port to Timbre v4
(def default-args {:host "127.0.0.1" :port 27017}) (defn- connect [{:keys [db server write-concern]}]
(defn connect [{:keys [db server write-concern]}] (let [args (merge {:host "127.0.0.1" :port 27017} server)
(let [args (merge default-args server) c (mongo/make-connection db args)]
c (mongo/make-connection db args)]
(when write-concern (when write-concern
(mongo/set-write-concern c write-concern)) (mongo/set-write-concern c write-concern))
c)) c))
(def conn (atom nil)) (def ^:private conn_ (atom nil))
(defn ensure-conn [config] (swap! conn #(or % (connect config)))) (defn- ensure-conn [config] (swap! conn_ #(or % (connect config))))
(defn log-message [params {:keys [collection logged-keys] :as config}] (defn- default-entry-fn [data]
(let [entry {:instant instant (let [{:keys [instant level hostname_
:level level context ?err ?ns-str ?file ?line msg_]}]
:?ns-str (str (:?ns-str data)) {:instant instant
:hostname (str @(:hostname_ data)) :level level
:vargs (str @(:vargs_ data)) :hostname (force hostname_)
:?err (str @(:?err_ data))}] :context context
:?err (when-let [err ?err] (str err))
:?ns-str ?ns-str
:?file ?file
:?line ?line
:msg (force msg_)}))
(defn- log-message [collection entry-fn data]
(let [entry (entry-fn data)]
(mongo/with-mongo (ensure-conn config) (mongo/with-mongo (ensure-conn config)
(mongo/insert! collection entry)))) (mongo/insert! collection entry))))
@ -33,15 +40,15 @@
(congomongo-appender (congomongo-appender
{:db \"logs\" {:db \"logs\"
:collection \"myapp\" :collection \"myapp\"
:logged-keys [:instant :level :msg_]
:write-concern :acknowledged :write-concern :acknowledged
:server {:host \"127.0.0.1\" :server {:host \"127.0.0.1\"
:port 27017}})" :port 27017}})"
[config]
[congo-config] (let [{:keys [collection entry-fn]
{:enabled? true :or {entry-fn default-entry-fn}} config]
:async? true {:enabled? true
:min-level :warn :async? true
:rate-limit [[1 1000]] ; 1/sec :min-level :warn
:output-fn :inherit :rate-limit [[1 1000]] ; 1/sec
:fn (fn [data] (log-message data congo-config))}) :output-fn :inherit
:fn (fn [data] (log-message collection entry-fn data))}))

View File

@ -52,6 +52,6 @@
(let [{:keys [appender msg_ level hostname_]} data (let [{:keys [appender msg_ level hostname_]} data
gelf-transport (:gelf-transport appender) gelf-transport (:gelf-transport appender)
log-level (timbre-to-gelf-level level) log-level (timbre-to-gelf-level level)
gelf-message (-> (GelfMessageBuilder. @msg_ @hostname_) gelf-message (-> (GelfMessageBuilder. (force msg_) (force hostname_))
(.level log-level) .build)] (.level log-level) .build)]
(.send gelf-transport gelf-message)))}))) (.send gelf-transport gelf-message)))})))

View File

@ -1,6 +1,6 @@
(ns taoensso.timbre.appenders.3rd-party.irclj (ns taoensso.timbre.appenders.3rd-party.irclj
"IRC appender. Requires https://github.com/flatland/irclj." "IRC appender. Requires https://github.com/flatland/irclj."
{:author "Emlyn Corrin"} {:author "Emlyn Corrin (@emlyn)"}
(:require [clojure.string :as str] (:require [clojure.string :as str]
[irclj.core :as irc] [irclj.core :as irc]
[taoensso.timbre :as timbre])) [taoensso.timbre :as timbre]))
@ -34,8 +34,7 @@
:name \"My Logger\" :chan \"#logs\"})" :name \"My Logger\" :chan \"#logs\"})"
[irc-config] [irc-config]
(let [conn (atom nil) (let [conn (atom nil)]
fmt-fn (or (:fmt-output-fn irc-config) default-fmt-output-fn)]
{:enabled? true {:enabled? true
:async? true :async? true
:min-level :info :min-level :info
@ -43,19 +42,19 @@
:output-fn :output-fn
(fn [data] (fn [data]
(let [{:keys [level ?err_ msg_]}] (let [{:keys [level ?err msg_]} data]
(format "[%s] %s%s" (format "[%s] %s%s"
(-> level name (str/upper-case)) (-> level name (str/upper-case))
(or @msg_ "") (or (force msg_) "")
(if-let [err @?err_] (if-let [err ?err]
(str "\n" (timbre/stacktrace err)) (str "\n" (timbre/stacktrace err))
"")))) ""))))
:fn :fn
(fn [data] (fn [data]
(let [{:keys [output-fn]} data] (let [{:keys [output_]} data]
(ensure-conn conn irc-config) (ensure-conn conn irc-config)
(send-message conn (:chan irc-config) (output-fn data))))})) (send-message conn (:chan irc-config) (force output_))))}))
;;;; Deprecated ;;;; Deprecated

View File

@ -1,6 +1,5 @@
(ns taoensso.timbre.appenders.3rd-party.logstash (ns taoensso.timbre.appenders.3rd-party.logstash
"Timbre appender that send output to logstash. "Timbre appender that send output to logstash.
Requires cheshire (https://github.com/dakrone/cheshire)." Requires cheshire (https://github.com/dakrone/cheshire)."
{:author "Mike Sperber (@mikesperber), David Frese (@dfrese)"} {:author "Mike Sperber (@mikesperber), David Frese (@dfrese)"}
(:require [taoensso.timbre :as timbre] (:require [taoensso.timbre :as timbre]
@ -37,7 +36,7 @@
:namespace (:?ns-str data) :namespace (:?ns-str data)
:file (:?file data) :file (:?file data)
:line (:?line data) :line (:?line data)
:stacktrace (some-> (force (:?err_ data)) (stacktrace-str)) :stacktrace (some-> (:?err data) (stacktrace-str))
:hostname (force (:hostname_ data)) :hostname (force (:hostname_ data))
:message (force (:msg_ data)) :message (force (:msg_ data))
"@timestamp" (:instant data)}) "@timestamp" (:instant data)})

View File

@ -31,11 +31,11 @@
(defn log-message [config data] (defn log-message [config data]
(let [entry (let [entry
{:instant (java.sql.Timestamp. (.getTime ^java.util.Date (:instant data))) {:instant (java.sql.Timestamp. (.getTime ^java.util.Date (:instant data)))
:level (str (:level data)) :level (str (:level data))
:namespace (str (:?ns-str data)) :namespace (str (:?ns-str data))
:hostname (str @(:hostname_ data)) :content (str (force (:msg_ data)))
:content (str @(:vargs_ data)) :error (str (:?err data))
:error (str @(:?err_ data))}] :hostname (str (force (:hostname_ data)))}]
(ensure-conn config) (ensure-conn config)
(jdbc/insert! @conn :logs entry))) (jdbc/insert! @conn :logs entry)))

View File

@ -56,8 +56,8 @@
:output-fn :inherit :output-fn :inherit
:fn :fn
(fn [data] (fn [data]
(let [{:keys [instant output-fn]} data (let [{:keys [instant output_]} data
output-str (output-fn data) output-str (force output_)
prev-cal (prev-period-end-cal instant pattern)] prev-cal (prev-period-end-cal instant pattern)]
(when-let [log (io/file path)] (when-let [log (io/file path)]
(try (try

View File

@ -1,5 +1,5 @@
(ns taoensso.timbre.appenders.3rd-party.rotor (ns taoensso.timbre.appenders.3rd-party.rotor
{:author "Karsten Schmidt"} {:author "Karsten Schmidt (@postspectacular)"}
(:require [clojure.java.io :as io] (:require [clojure.java.io :as io]
[taoensso.timbre :as timbre]) [taoensso.timbre :as timbre])
(:import [java.io File FilenameFilter])) (:import [java.io File FilenameFilter]))
@ -57,8 +57,8 @@
:fn :fn
(let [lock (Object.)] (let [lock (Object.)]
(fn [data] (fn [data]
(let [{:keys [output-fn]} data (let [{:keys [output_]} data
output-str (output-fn data)] output-str (str (force output_) "\n")]
(let [log (io/file path)] (let [log (io/file path)]
(try (try
;; all the filesystem manipulations are unsafe in the face of concurrency ;; all the filesystem manipulations are unsafe in the face of concurrency
@ -67,7 +67,7 @@
(io/make-parents log)) (io/make-parents log))
(when (> (.length log) max-size) (when (> (.length log) max-size)
(rotate-logs path backlog))) (rotate-logs path backlog)))
(spit path (str (output-fn data) "\n") :append true) (spit path output-str :append true)
(catch java.io.IOException _))))))}) (catch java.io.IOException _))))))})
;;;; Deprecated ;;;; Deprecated

View File

@ -1,6 +1,6 @@
(ns taoensso.timbre.appenders.3rd-party.server-socket (ns taoensso.timbre.appenders.3rd-party.server-socket
"TCP socket appender. Requires https://github.com/technomancy/server-socket." "TCP socket appender. Requires https://github.com/technomancy/server-socket."
{:author "Emlyn Corrin"} {:author "Emlyn Corrin (@emlyn)"}
(:require [server.socket :refer [create-server]] (:require [server.socket :refer [create-server]]
[taoensso.timbre :refer [stacktrace]]) [taoensso.timbre :refer [stacktrace]])
(:import [java.net Socket InetAddress] (:import [java.net Socket InetAddress]
@ -45,12 +45,12 @@
:output-fn :inherit :output-fn :inherit
:fn :fn
(fn [data] (fn [data]
(let [{:keys [output-fn]} data] (let [{:keys [output_]} data]
(let [c (ensure-conn socket-config)] (let [c (ensure-conn socket-config)]
(doseq [sock @(:connections c)] (doseq [sock @(:connections c)]
(let [out (PrintWriter. (.getOutputStream ^Socket sock))] (let [out (PrintWriter. (.getOutputStream ^Socket sock))]
(binding [*out* out] (binding [*out* out]
(println (output-fn data))))))))})) (println (force output_))))))))}))
;;;; Deprecated ;;;; Deprecated

View File

@ -25,6 +25,6 @@
:output-fn :inherit :output-fn :inherit
:fn :fn
(fn [data] (fn [data]
(let [{:keys [output-fn]} data] (let [{:keys [output_]} data]
(slack.chat/post-message conn channel (output-fn data) (slack.chat/post-message conn channel (force output_)
(dissoc opts :token :channel))))})) (dissoc opts :token :channel))))}))

View File

@ -1,6 +1,6 @@
(ns taoensso.timbre.appenders.3rd-party.zmq (ns taoensso.timbre.appenders.3rd-party.zmq
"ØMQ appender. Requires https://github.com/zeromq/cljzmq" "ØMQ appender. Requires https://github.com/zeromq/cljzmq"
{:author "Angus Fletcher"} {:author "Angus Fletcher (@angusiguess)"}
(:require [zeromq.zmq :as zmq] (:require [zeromq.zmq :as zmq]
[taoensso.timbre :as timbre])) [taoensso.timbre :as timbre]))
@ -27,8 +27,8 @@
:output-fn :inherit :output-fn :inherit
:fn :fn
(fn [data] (fn [data]
(let [{:keys [output-fn]} data (let [{:keys [output_]} data
output-str (output-fn data)] output-str (force output_)]
(loop [] (loop []
(zmq/poll poller 500) (zmq/poll poller 500)
(cond (cond