Add IRC appender

This commit is contained in:
Emlyn Corrin 2013-04-19 14:28:02 +01:00
parent cf0f41dd7d
commit 48f8329dbb
2 changed files with 74 additions and 1 deletions

View File

@ -152,6 +152,32 @@ And make sure emails are sent asynchronously:
(timbre/set-config! [:appenders :postal :async?] true)
```
### IRC Appender
To enable the standard [irclj](https://github.com/flatland/irclj)-based IRC appender, add the irclj dependency to your `project.clj`:
```clojure
[irclj "0.5.0-alpha2"]
```
And add the appender to your `ns` declaration:
```clojure
(:require [taoensso.timbre.appenders.irc :refer [irc-appender]])
```
Then adjust your Timbre config:
```clojure
(timbre/set-config! [:appenders :irc] irc-appender)
(timbre/set-config! [:shared-appender-config :irc]
{:host "irc.example.org"
:port 6667
:nick "logger"
:name "Logger"
:chan "#logs"})
```
### Custom Appenders
Writing a custom appender is dead-easy:
@ -177,7 +203,7 @@ See the `timbre/config` docstring for more information on appenders.
The usual recommendation for Clojure profiling is: use a good **JVM profiler** like [YourKit](http://www.yourkit.com/), [JProfiler](http://www.ej-technologies.com/products/jprofiler/overview.html), or [VisualVM](http://docs.oracle.com/javase/6/docs/technotes/guides/visualvm/index.html).
And these certainly do the job. But as with many Java tools, they can be a little hairy and often heavy-handed - especially when applied to Clojure. Timbre includes an alternative.
And these certainly do the job. But as with many Java tools, they can be a little hairy and often heavy-handed - especially when applied to Clojure. Timbre includes an alternative.
Let's add it to our app's `ns` declaration:

View File

@ -0,0 +1,47 @@
(ns taoensso.timbre.appenders.irc
"IRC appender for irclj.
Ref: https://github.com/flatland/irclj."
{:author "Emlyn Corrin"}
(:require [clojure.string :as str]
[irclj.core :as irclj]
[taoensso.timbre :as timbre]))
(def conn (atom nil))
(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)
conn))
(defn ensure-conn [conf]
(swap! conn #(or % (connect conf))))
(defn send-message [{:keys [message prefix chan] :as config}]
(let [conn (ensure-conn config)
lines (str/split message #"\n")]
(irclj/message conn chan prefix (first lines))
(doseq [line (rest lines)]
(irclj/message conn chan ">" line))))
(defn appender-fn [{:keys [ap-config prefix message]}]
(when-let [irc-config (:irc ap-config)]
(send-message
(assoc irc-config
:prefix (if-let [prefix-fn (:prefix-fn irc-config)]
(prefix-fn prefix)
prefix)
:message message))))
(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 :async? false
:max-message-per-msecs nil ; no rate limit by default
:fn appender-fn})