Skip to content

Commit 8f244e9

Browse files
committed
Add log-level option to with-logging
1 parent bae6d47 commit 8f244e9

2 files changed

Lines changed: 11 additions & 6 deletions

File tree

src/compojure/api/exception.clj

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,15 @@
6161
;; Logging
6262
;;
6363

64-
(defn with-logging [handler]
65-
(fn [^Exception e data req]
66-
(logging/log! :error e (.getMessage e))
67-
(handler e data req)))
64+
(defn with-logging
65+
"Wrap compojure-api exception-handler a function which will log the
66+
exception message and stack-trace with given log-level."
67+
([handler] (with-logging handler :error))
68+
([handler log-level]
69+
{:pre [(#{:trace :debug :info :warn :error :fatal} log-level)]}
70+
(fn [^Exception e data req]
71+
(logging/log! log-level e (.getMessage e))
72+
(handler e data req))))
6873

6974
;;
7075
;; Mappings from other Exception types to our base types

test/compojure/api/middleware_test.clj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,5 @@
7878
(without-err
7979
(fact "Logging can be added to a exception handler"
8080
(let [handler (-> (fn [_] (throw (ex-info "Error parsing request" {:type ::ex/request-parsing} (RuntimeException. "Kosh"))))
81-
(wrap-exceptions (assoc-in default-options [:handlers ::ex/request-parsing] (ex/with-logging ex/request-parsing-handler))))]
82-
(with-out-str (handler {})) => "ERROR Error parsing request\n"))))
81+
(wrap-exceptions (assoc-in default-options [:handlers ::ex/request-parsing] (ex/with-logging ex/request-parsing-handler :info))))]
82+
(with-out-str (handler {})) => "INFO Error parsing request\n"))))

0 commit comments

Comments
 (0)