/
logging.clj
51 lines (47 loc) · 1.91 KB
/
logging.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
(ns io.aviso.logging
"Provides functions that hook into clojure.tools.logging to make use of Pretty to format exceptions.
You must [add clojure.tools.logging as an explicit dependency](https://github.com/clojure/tools.logging) of your project."
{:added "0.1.15"}
(:require [clojure.tools.logging :as l]
[io.aviso.exception :as e]
[io.aviso.writer :as writer])
(:import [java.lang.Thread$UncaughtExceptionHandler]))
(defn install-pretty-logging
"Modifies clojure.tools.logging to use pretty exception logging."
([]
(alter-var-root
#'l/log*
(fn [default-impl]
(fn [logger level throwable message]
(default-impl logger
level
nil
(if throwable
(str message
writer/eol
(e/format-exception throwable))
message))))))
([frame-filter-fn]
(alter-var-root
#'l/log*
(fn [default-impl]
(fn [logger level throwable message]
(default-impl logger
level
nil
(if throwable
(str message
writer/eol
(e/format-exception throwable {:filter frame-filter-fn}))
message)))))))
(defn uncaught-exception-handler
"Creates a reified UncaughtExceptionHandler that uses clojure.tools.logging/error, rather than
simplying printing the exception, which is the default behavior."
[]
(reify Thread$UncaughtExceptionHandler
(uncaughtException [_ _ t]
(l/error t (or (.getMessage t) (-> t .getClass .getName))))))
(defn install-uncaught-exception-handler
"Installs a default UncaughtExceptionHandler. "
[]
(Thread/setDefaultUncaughtExceptionHandler (uncaught-exception-handler)))