mirror of
https://github.com/status-im/timbre.git
synced 2025-01-15 22:34:17 +00:00
Update IRC appender to Timbre v3 style (@crisptrutski)
This commit is contained in:
parent
aa3d9e6d1c
commit
623342a2bb
@ -32,7 +32,7 @@
|
|||||||
}]
|
}]
|
||||||
:dev
|
:dev
|
||||||
[:1.6 :test
|
[:1.6 :test
|
||||||
{:dependencies []
|
{:dependencies [[irclj "0.5.0-alpha4"]]
|
||||||
:plugins [[lein-ancient "0.5.4"]
|
:plugins [[lein-ancient "0.5.4"]
|
||||||
[codox "0.6.7"]]}]}
|
[codox "0.6.7"]]}]}
|
||||||
|
|
||||||
|
@ -2,45 +2,74 @@
|
|||||||
"IRC appender. Depends on https://github.com/flatland/irclj."
|
"IRC appender. Depends on https://github.com/flatland/irclj."
|
||||||
{:author "Emlyn Corrin"}
|
{:author "Emlyn Corrin"}
|
||||||
(:require [clojure.string :as str]
|
(:require [clojure.string :as str]
|
||||||
[irclj.core :as irclj]
|
[irclj.core :as irc]
|
||||||
[taoensso.timbre :as timbre]))
|
[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]
|
(def default-appender-opts
|
||||||
:or {:port 6667}}]
|
{:async? true
|
||||||
(let [conn (irclj/connect host port nick
|
: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
|
:username user
|
||||||
:real-name name
|
:real-name name
|
||||||
:pass pass
|
:pass pass
|
||||||
:callbacks {})]
|
:callbacks {})]
|
||||||
(irclj/join conn chan)
|
(irc/join conn chan)
|
||||||
conn))
|
conn))
|
||||||
|
|
||||||
(defn ensure-conn [conf]
|
(defn- ensure-conn [conn conf]
|
||||||
(swap! conn #(or % (connect conf))))
|
(if-not @conn
|
||||||
|
(reset! conn @(connect conf))))
|
||||||
|
|
||||||
(defn send-message [{:keys [prefix throwable message chan] :as config}]
|
(defn- send-message [conn chan output]
|
||||||
(let [conn (ensure-conn config)
|
(let [[fst & rst] (str/split output #"\n")]
|
||||||
lines (-> (str message (timbre/stacktrace throwable "\n"))
|
(irc/message conn chan fst)
|
||||||
(str/split #"\n"))]
|
(doseq [line rst]
|
||||||
(irclj/message conn chan prefix (first lines))
|
(irc/message conn chan ">" line))))
|
||||||
(doseq [line (rest lines)]
|
|
||||||
(irclj/message conn chan ">" line))))
|
|
||||||
|
|
||||||
(defn appender-fn [{:keys [ap-config prefix throwable message]}]
|
(defn- make-appender-fn [irc-config conn]
|
||||||
(when-let [irc-config (:irc ap-config)]
|
(fn [{:keys [ap-config] :as args}]
|
||||||
(send-message
|
(when-let [irc-config (or irc-config (:irc ap-config))]
|
||||||
(assoc irc-config
|
(ensure-conn conn irc-config)
|
||||||
:prefix prefix
|
(let [fmt-fn (or (:fmt-output-fn irc-config)
|
||||||
:throwable throwable
|
default-fmt-output-fn)]
|
||||||
:message message))))
|
(send-message conn (:chan irc-config) (fmt-fn args))))))
|
||||||
|
|
||||||
(def irc-appender
|
;;; Public
|
||||||
{:doc (str "Sends IRC messages using irclj.\n"
|
|
||||||
"Needs :irc config map in :shared-appender-config, e.g.:
|
(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\"
|
{:host \"irc.example.org\" :port 6667 :nick \"logger\"
|
||||||
:name \"My Logger\" :chan \"#logs\"")
|
:name \"My Logger\" :chan \"#logs\"}"
|
||||||
:min-level :info :enabled? true
|
[& [appender-opts {:keys [irc-config]}]]
|
||||||
:prefix-fn (fn [{:keys [level]}] (-> level name str/upper-case))
|
(let [conn (atom nil)]
|
||||||
:fn appender-fn})
|
(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"))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user