From 49c15532235b49111c05ca338a3af3cdc772ad4d Mon Sep 17 00:00:00 2001 From: niten Date: Wed, 15 Jun 2022 18:35:53 -0700 Subject: [PATCH] Add support for notification levels --- deps.edn | 2 +- src/bebot/logger.clj | 39 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/deps.edn b/deps.edn index d5e03c0..43dbbec 100644 --- a/deps.edn +++ b/deps.edn @@ -8,7 +8,7 @@ org.fudo/fudo-clojure { :git/url "https://git.fudo.org/fudo-public/fudo-clojure.git" - :sha "9775e301714b785c27b23664b0d89e379bdca27e" + :sha "c948aa7a8e0698fd11312832cf033d271e817e45" } } :aliases { diff --git a/src/bebot/logger.clj b/src/bebot/logger.clj index 87b5fe2..5540869 100644 --- a/src/bebot/logger.clj +++ b/src/bebot/logger.clj @@ -3,7 +3,7 @@ [bebot.api.channel :as chan] [bebot.client :refer [connect]] [fudo-clojure.result :refer [let-result unwrap success]] - [fudo-clojure.logging :as log])) + [fudo-clojure.logging :as log :refer [level-to-int]])) (defn make-logger [url access-token channel-id & {:keys [error-level logic-level] @@ -14,3 +14,40 @@ chan (client/open-channel! conn channel-id)] (success (log/log-to-function (partial chan/send-post! chan) error-level logic-level))))) + +(defn make-logger [url access-token channel-id user & + {:keys [error-level + logic-level + error-mention-level + logic-mention-level] + :or {error-level :error + logic-level :notify + error-mention-level :error + logic-mention-level :notify}}] + (let [emit (unwrap + (let-result [conn (connect url access-token) + channel (client/open-channel! conn channel-id)] + (success (fn [mention msg] + (if mention + (chan/send-post! channel (format "@%s %s" user msg)) + (chan/send-post! channel msg)))))) + above-level? (fn [lvl] (fn [tst] (>= (level-to-int tst) (level-to-int lvl)))) + emit-error? (above-level? error-level) + emit-logic? (above-level? logic-level) + mention-error? (above-level? error-mention-level) + mention-logic? (above-level? logic-mention-level) + error-stage (fn [lvl msg] + (when (emit-error? lvl) + (emit (mention-error? lvl) msg))) + logic-stage (fn [lvl msg] + (when (logic-error? lvl) + (emit (logic-error? lvl) msg)))] + (reify log/Logger + (debug! [_ msg] (error-stage :debug msg)) + (warn! [_ msg] (error-stage :warn msg)) + (error! [_ msg] (error-stage :error msg)) + (fatal! [_ msg] (error-stage :fatal msg)) + + (info! [_ msg] (logic-stage :info msg)) + (notify! [_ msg] (logic-stage :notify msg)) + (alert! [_ msg] (logic-stage :alert msg)))))