Update IRC appender to Timbre v3 style (@crisptrutski)

This commit is contained in:
Chris Truter 2014-06-01 16:18:48 +02:00 committed by Peter Taoussanis
parent aa3d9e6d1c
commit 623342a2bb
2 changed files with 64 additions and 35 deletions

View File

@ -32,7 +32,7 @@
}]
:dev
[:1.6 :test
{:dependencies []
{:dependencies [[irclj "0.5.0-alpha4"]]
:plugins [[lein-ancient "0.5.4"]
[codox "0.6.7"]]}]}

View File

@ -2,45 +2,74 @@
"IRC appender. Depends on https://github.com/flatland/irclj."
{:author "Emlyn Corrin"}
(:require [clojure.string :as str]
[irclj.core :as irclj]
[irclj.core :as irc]
[taoensso.timbre :as timbre]))
(def conn (atom nil))
(defn default-fmt-output-fn
[{:keys [level throwable message]}]
(format "[%s] %s%s"
(-> level name (str/upper-case))
(or message "")
(or (timbre/stacktrace throwable "\n") "")))
(defn connect [{:keys [host port pass nick user name chan]
:or {:port 6667}}]
(let [conn (irclj/connect host port nick
:username user
:real-name name
:pass pass
:callbacks {})]
(irclj/join conn chan)
(def default-appender-opts
{:async? true
:enabled? true
:min-level :info})
(defn- connect [{:keys [host port pass nick user name chan]
:or {port 6667}}]
(let [conn (irc/connect host port nick
:username user
:real-name name
:pass pass
:callbacks {})]
(irc/join conn chan)
conn))
(defn ensure-conn [conf]
(swap! conn #(or % (connect conf))))
(defn- ensure-conn [conn conf]
(if-not @conn
(reset! conn @(connect conf))))
(defn send-message [{:keys [prefix throwable message chan] :as config}]
(let [conn (ensure-conn config)
lines (-> (str message (timbre/stacktrace throwable "\n"))
(str/split #"\n"))]
(irclj/message conn chan prefix (first lines))
(doseq [line (rest lines)]
(irclj/message conn chan ">" line))))
(defn- send-message [conn chan output]
(let [[fst & rst] (str/split output #"\n")]
(irc/message conn chan fst)
(doseq [line rst]
(irc/message conn chan ">" line))))
(defn appender-fn [{:keys [ap-config prefix throwable message]}]
(when-let [irc-config (:irc ap-config)]
(send-message
(assoc irc-config
:prefix prefix
:throwable throwable
:message message))))
(defn- make-appender-fn [irc-config conn]
(fn [{:keys [ap-config] :as args}]
(when-let [irc-config (or irc-config (:irc ap-config))]
(ensure-conn conn irc-config)
(let [fmt-fn (or (:fmt-output-fn irc-config)
default-fmt-output-fn)]
(send-message conn (:chan irc-config) (fmt-fn args))))))
(def irc-appender
{:doc (str "Sends IRC messages using irclj.\n"
"Needs :irc config map in :shared-appender-config, e.g.:
{:host \"irc.example.org\" :port 6667 :nick \"logger\"
:name \"My Logger\" :chan \"#logs\"")
:min-level :info :enabled? true
:prefix-fn (fn [{:keys [level]}] (-> level name str/upper-case))
:fn appender-fn})
;;; Public
(defn make-irc-appender
"Sends IRC messages using irc.
Needs :irc config map in :shared-appender-config, e.g.:
{:host \"irc.example.org\" :port 6667 :nick \"logger\"
:name \"My Logger\" :chan \"#logs\"}"
[& [appender-opts {:keys [irc-config]}]]
(let [conn (atom nil)]
(merge default-appender-opts
appender-opts
{:conn conn
:doc (:doc (meta #'make-irc-appender))
:fn (make-appender-fn irc-config conn)})))
(def irc-appender "DEPRECATED: Use `make-irc-appender` instead."
(make-irc-appender))
(comment
(timbre/set-config!
[:shared-appender-config :irc]
{:host "127.0.0.1"
:nick "lazylog"
:user "lazare"
:name "Lazylus Logus"
:chan "bob"})
(timbre/set-config! [:appenders :irc] (make-irc-appender))
(timbre/log :error "A multiple\nline message\nfor you"))