Skip to content

Commit

Permalink
[new] Postal appender: add :body-len opt
Browse files Browse the repository at this point in the history
  • Loading branch information
ptaoussanis committed Feb 27, 2023
1 parent 7d9eb7a commit 09c64dc
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/taoensso/timbre/appenders/postal.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
- Simplify whitespace
- Never exceed `max-subject-len` characters."

[{:keys [max-len]
:or {max-len 150}}
output-str]
[{:keys [max-len]} output-str]

(let [s (->
(re-find #"\A.*" output-str) ; 1st line
Expand All @@ -26,7 +24,7 @@

(if (and max-len (> (count s) ^long max-len))
(str (enc/get-substr-by-len s 0 (- ^long max-len 3)) "...")
s)))
(do s))))

(comment
(default-subject-fn {:max-len 8} "sdfghsjhfdg shj
Expand All @@ -35,20 +33,27 @@ sfsdf
sfsdf
sdf"))

(defn default-body-fn
"Given an `output-str`, returns an appropriate Postal email body."
[{:keys [max-len]} output-str]
(let [s output-str]
[{:type "text/plain; charset=utf-8"
:content
(if max-len
(enc/get-substr-by-len s 0 max-len)
(do s))}]))

(defn postal-appender
"Returns a Postal email appender.
(postal-appender
^{:host \"mail.isp.net\" :user \"jsmith\" :pass \"sekrat!!1\"}
{:from \"Bob's logger <[email protected]>\" :to \"[email protected]\"})"

[postal-config &
[{:keys [subject-len subject-fn body-fn]
[{:keys [subject-len body-len subject-fn body-fn]
:or {subject-len 150
subject-fn (partial default-subject-fn {:max-len subject-len})
body-fn
(fn [output-str]
[{:type "text/plain; charset=utf-8"
:content output-str}])}}]]
subject-fn (partial default-subject-fn {:max-len (enc/as-?int subject-len)})
body-fn (partial default-body-fn {:max-len (enc/as-?int body-len)})}}]]

{:enabled? true
:async? true ; Slow!
Expand Down

0 comments on commit 09c64dc

Please sign in to comment.