BREAKING: Moved Postal appender to own ns and no longer automatically include Postal as dependency.

All future appenders that depend on an external library will follow this pattern, allowing the
flexibility to add more diverse appenders without the worry of bloating Timbre core or Timbre's
dependencies.

See updated README example for how to include standard email appender.
This commit is contained in:
Peter Taoussanis 2012-07-13 17:07:23 +07:00
parent 6807e2b6bd
commit cca1141c7f
4 changed files with 45 additions and 31 deletions

View File

@ -99,12 +99,11 @@ Configuring Timbre couldn't be simpler. Let's check out (some of) the defaults:
:appenders
{:standard-out { <...> }
:postal { <...> }}
<...> }
:shared-appender-config
{:timestamp-pattern "yyyy-MMM-dd HH:mm:ss ZZ"
:locale nil
:postal nil}}
:locale nil}}
```
Easily adjust the current logging level:
@ -127,14 +126,27 @@ Filter logging output by namespaces:
(timbre/set-config! [:ns-whitelist] ["some.library.core" "my-app.*"])
```
Enable the standard [Postal](https://github.com/drewr/postal)-based email appender:
### Email Appender
To enable the standard [Postal](https://github.com/drewr/postal)-based email appender, add the Postal dependency to your `project.clj`:
```clojure
[com.draines/postal "1.8.0"]
```
And add the appender to your `ns` declaration:
```clojure
(:require [taoensso.timbre.appenders (postal :as postal-appender)])
```
Then adjust your Timbre config:
```clojure
(timbre/set-config! [:appenders :postal] postal-appender/postal-appender)
(timbre/set-config! [:shared-appender-config :postal]
^{:host "mail.isp.net" :user "jsmith" :pass "sekrat!!1"}
{:from "me@draines.com" :to "foo@example.com"})
(timbre/set-config! [:appenders :postal :enabled?] true)
```
Rate-limit to one email per message per minute:
@ -180,9 +192,7 @@ And these certaily do the job. But as with many Java tools, they can be a little
Let's add it to our app's `ns` declaration:
```clojure
(ns my-app
(:use [taoensso.timbre :as timbre :only (trace debug info warn error fatal spy)]
[taoensso.timbre.profiling :as profiling :only (p profile)]))
(:use [taoensso.timbre.profiling :as profiling :only (p profile)])
```
Wrap forms that you'd like to profile with the `p` macro and give them a name:

View File

@ -3,8 +3,7 @@
:url "https://github.com/ptaoussanis/timbre"
:license {:name "Eclipse Public License"}
:dependencies [[org.clojure/clojure "1.3.0"]
[clj-stacktrace "0.2.4"]
[com.draines/postal "1.8.0"]]
[clj-stacktrace "0.2.4"]]
:profiles {:1.3 {:dependencies [[org.clojure/clojure "1.3.0"]]}
:1.4 {:dependencies [[org.clojure/clojure "1.4.0"]]}
:1.5 {:dependencies [[org.clojure/clojure "1.5.0-master-SNAPSHOT"]]}}

View File

@ -2,8 +2,7 @@
"Simple, flexible, all-Clojure logging. No XML!"
{:author "Peter Taoussanis"}
(:require [clojure.string :as str]
[clj-stacktrace.repl :as stacktrace]
[postal.core :as postal])
[clj-stacktrace.repl :as stacktrace])
(:import [java.util Date Locale]
[java.text SimpleDateFormat]))
@ -56,29 +55,13 @@
:max-message-per-msecs nil
:fn (fn [{:keys [error? more] :as args}]
(binding [*out* (if error? *err* *out*)]
(apply str-println (prefixed-message args) more)))}
:postal
{:doc (str "Sends an email using com.draines/postal.\n"
"Needs :postal config map in :shared-appender-config.")
:min-level :error :enabled? false :async? true
:max-message-per-msecs (* 60 60 2)
:fn (fn [{:keys [ap-config more] :as args}]
(when-let [postal-config (:postal ap-config)]
(postal/send-message
(assoc postal-config
:subject (prefixed-message args)
:body (if (seq more) (str/join " " more)
"<no additional arguments>")))))}}
(apply str-println (prefixed-message args) more)))}}
;; Will be given to all appenders via :ap-config key
:shared-appender-config
{:timestamp-pattern "yyyy-MMM-dd HH:mm:ss ZZ" ; SimpleDateFormat pattern
:locale nil ; A Locale object, or nil
;; A Postal message map, or nil.
;; ^{:host "mail.isp.net" :user "jsmith" :pass "sekrat!!1"}
;; {:from "Bob's logger <me@draines.com>" :to "foo@example.com"}
:postal nil}}))
}}))
(defn set-config! [[k & ks] val] (swap! config assoc-in (cons k ks) val))
(defn set-level! [level] (set-config! [:current-level] level))

View File

@ -0,0 +1,22 @@
(ns taoensso.timbre.appenders.postal
"Email appender for com.draines/postal.
Ref: https://github.com/drewr/postal."
{:author "Peter Taoussanis"}
(:require [clojure.string :as str]
[postal.core :as postal]
[taoensso.timbre :as timbre]))
(def postal-appender
{:doc (str "Sends an email using com.draines/postal.\n"
"Needs :postal config map in :shared-appender-config, e.g.:
^{:host \"mail.isp.net\" :user \"jsmith\" :pass \"sekrat!!1\"}
{:from \"Bob's logger <me@draines.com>\" :to \"foo@example.com\"}")
:min-level :error :enabled? true :async? true
:max-message-per-msecs (* 60 60 2)
:fn (fn [{:keys [ap-config more] :as args}]
(when-let [postal-config (:postal ap-config)]
(postal/send-message
(assoc postal-config
:subject (timbre/prefixed-message args)
:body (if (seq more) (str/join " " more)
"<no additional arguments>")))))})